#How to import a header file/unit that includes other headers?

1 messages · Page 1 of 1 (latest)

analog temple
#

Experimenting with modules in msvc compiler and cmake and can't figure out how to prevent importing headers from importing it multiple times for example if you

import <A.h>;
import <B.h>; //B.h includes A.h

I get multiple definition/already defined errors. Is there a way to import both and have the inclusion guards work correctly?

For reference I am trying google test

No errors

import <gmock/gmock-matchers.h>;

However,

import <gmock/gmock-matchers.h>; //this file includes "gtest.h"
import <gtest/gtest.h>;
``` results in
```Error LNK2005: "char const * const testing::internal::kDeathTestStyleFlag" (?kDeathTestStyleFlag@internal@testing@@3QBDB) already defined in gmock-matchers
...

This also works

#include <gtest/gtest.h>
#include <gmock/gmock-matchers.h>
``` however it says not to include headers in modules on cppreference so not sure what the best way to go about this is. Any help/guidance is appreciated, thanks.
thin wagonBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

mint haven
#

Msvc has a lot of issues with includes coming after imports right now

#

In the 17.10 release notes they said they're working on a long term fix

noble oriole
#

I know

mint haven
#

But for now you have to make sure imports come after all includes

#

Which is definitely not easy in these cases

analog temple
#

Gotcha, thank you!

#

!solved