Léon Rodenburg
Léon Rodenburg

Reputation: 4894

Extract multiple implementations into static libraries

I have a (still) small game engine in C++ built with Visual Studio. The game engine has support for DirectX and OpenGL, but that's not really of importance. The important part is that I have some specific implementations of abstract base classes (for example, a DXKernel and an OGLKernel as subclasses of the Kernel class). So, the DXKernel and OGLKernel class have a common interface with some platform-specific code. The Kernel class does have some implementations of itself, so it's not a pure interface and these methods should be inherited by the implementations.

Now, from what I understand, it's best to take the specific implementation out of the main project and create static libraries to link the main project with, depending on the platform I'm compiling for. So, if I'm compiling for OS X, I only want to include the OpenGL implementations. If I'm compiling for Windows, I want to include both the DirectX and OpenGL implementations.

The Visual Studio layout now looks like this:

Solution
    Project
        Header files
            Core
                DX
                    DXKernel.h    <-- DirectX implementation of abstract methods
                    ...
                OGL
                    OGLKernel.h   <-- OpenGL implementation of abstract methods
                    ...

                Kernel.h          <-- abstract class(es)
                ...
            ...
        Source files
            Core
                DX
                    DXKernel.cpp
                    ...
                OGL
                    OGLKernel.cpp
                    ...

                Kernel.cpp
                ...
            ...

If I create a new static library project in Visual Studio, and copy over the specific files to the new library, how can I get the library project to find the common headers (so for example Kernel.h)? Should I copy over the header files of the main project to the library (this creates problems as the headers are updated)? Should I change the include path on the library project?

It seems though that this setup creates some kind of a circular dependency between the main project and the libraries, and that shouldn't be the case as far as I know.

I hope you can help me out.

Upvotes: 2

Views: 210

Answers (1)

L&#233;on Rodenburg
L&#233;on Rodenburg

Reputation: 4894

Let me answer my own question. I have done some more research on the topic and found out that static libraries require that the used headers are in the main project, while the .cpp implementations of these headers are in the static library.

So, the source of the project now looks like this:

Solution
    Main Project
        Header files
            Core
                DX
                    DXKernel.h    <-- DirectX header
                    ...
                OGL
                    OGLKernel.h   <-- OpenGL header
                    ...

                Kernel.h          <-- abstract class
                ...
            ...
        Source files
            Core
                Kernel.cpp
                ...
            ...

    Static library project (DirectX)
        Source files
            Core
                DXKernel.cpp     <-- DirectX implementation
                ...

    Static library project (OpenGL)
        Source files
            Core
                OGLKernel.cpp    <-- OpenGL implementation
                ...

All the source now builds correctly and I can choose which library the compiler should link with (or both of course).

Thanks for watching.

Upvotes: 1

Related Questions