0

so I'm working on upgrading the architecture of application to a more modular design. It's a C++ application which uses an underlying monolithic c-library if we are building for a specific target, otherwise it uses a different library back-end.

Currently the application front-end code, the GUI code, is directly tied to the monolithic c-library, which only works on one target. What has been done is the monolithic c-library has been put into a "module" which exposes a common C++ interface for the GUI part of the application to consume. This way we can change build targets and the GUI code doesn't change. The previously existing GUI code uses structures defined within the C-library, which are common to all possible c-libraries.

Inside the C++ module I've defined sort-of proxy types which look like so:

//ModuleTypes.hpp typedef CPP_TYPE C_LEGACY_TYPE; 

and the GUI code simply uses CPP_TYPE in-place of the previously existing C_LEGACY_TYPE.

Elsewhere in the module project, Module.hpp looks like:

//Module.hpp #include "C_LEGACY_TYPES.hpp" ... #include "ModuleTypes.hpp" 

So when the C++ module gets compiled into a static lib, Module.hpp includes C_LEGACY_TYPES.hpp before ModuleTypes.hpp (so ModuleTypes.hpp knows what C_LEGACY_TYPE is), and the static library compiles just fine.

The problem is in the GUI module, where for example:

//Gui1.hpp class Gui1 { void Method1(CPP_TYPE value); }; 

Since CPP_TYPE cannot be forward declared, at the top I add #include "ModuleTypes.hpp" to Gui1.hpp.

//Gui1.hpp #include "ModuleTypes.hpp" class Gui1 { void Method1(CPP_TYPE value); }; 

When I go to compile the GUI module, it errors because it can't figure out what C_LEGACY_TYPE is. The only way to solve this is to #include "C_LEGACY_TYPE.hpp" inside ModuleTypes.hpp. This however leads to GUI having to know where the legacy C headers are when it gets compiled.

I would like GUI.hpp to #include "ModuleTypes.hpp" but use the definition of C_LEGACY_TYPE from the static library, and not have to #include "C_LEGACY_TYPES.h" inside "ModuleTypes.hpp."

I understand why the error is happening, I'm looking for an alternative solution. How do I get the GUI module to be able to use the types defines in ModuleTypes.hpp without having to include the legacy headers when compiling the GUI module?

With the architecture change we're migrating the C++11 so I'm open to any suggestions. VC2010 is what we're building with.

0

1 Answer 1

3

You're doing the right thing by separating the back-end C library behind a common interface layer (wrapper class), so you can easily replace the back-end with something else later.

However, you also need to ensure that the types used in the interface to the C library are similarly isolated within the wrapper class. Since your GUI layer needs to include the C_LEGACY_TYPE.hpp header file, I suspect your types are not being isolated as they should be.

This may mean that you need to do some type conversion inside the wrapper class, to convert from types the GUI knows about to the types used by the C library. The interface of the wrapper class (ie. the common interface) must not contain any of the types specific to the back-end C library.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.