Preprocessor Commands in libraries

Recommended Posts

webwraith    239
Hi all, just your friendly neighbourhood bumbling idiot, I was wondering if preprocessor commands were left in source code that is compiled into a library. The reason I'm asking is that I'm trying to code a relatively small graphics library, but make it handle both OpenGL and DirectX, although not at the same time.[grin] to do this, I've included
#ifdef DIRECT3D_VERSION...#endif
to specify the code to run when Direct3D is included(via "d3d9.h") in the project. Only trouble is I want to make the code into a library, so will it still look for DIRECT3D_VERSION, if I include the library in a project,or will the compiler (MinGW as always) sort the functions at the library's compile-time?

Share on other sites
jpetrie    13132
If understand you properly, you cannot do what you want that way.

The preprocessor runs before the compiler. The typical usage of #ifdef ... #endif directives removes code (so the compiler does not see the code). Thus, the preprocessor directives cannot "remain in compiled code" in any way. They're gone before the code reaches the compiler; you cannot use the preprocessor to change the behavior of the compiled library code. In fact, attempting to do so can result in strange compile or link errors, or runtime errors.

You might be able to compile your code twice, once with the preprocessor defines set up for the D3D version, and again with the OpenGL version. This will produce two libraries; the user must link against the appropriate one and define the appropriate preprocessor defines. This is kind of clunky; there are better ways to resolve this issue that involve more actual abstraction of your rendering interface.

Share on other sites
webwraith    239
Yeah, I was rather worried when it compiled first time :P

I was looking for a way that recognized what form of graphics API was being used (OGL, if not DirectX) automatically, so the user would just include the DirectX header and library, and it would run on that API

[Edited by - webwraith on May 18, 2006 5:14:09 PM]

Share on other sites
webwraith    239
Right, I'm holding off on the DirectX side of things, but I'm still having problems. When I compile my library, everything seems to run fine, but when I include it and #include its header, the compiler (MinGW) doesn't seem to be able to find the wgl commands(in particular wglCreateContext() and wglMakeCurrent()).

I've tried everything I can think of(not much), including;

#include the windows.h header in the library, and in the source using the library(and both),
and
including the link libraries in both the source and my custom library(and both), but it doesn't want to know.Any ideas?

Share on other sites
Mike2343    1202
You're project still needs to link to the OpenGL libraries unless you use command line tools to merge the OpenGL32.lib into your lib file (waste of time honestly).

Share on other sites
webwraith    239
I've already tried that,but I still get the same problem

Share on other sites
Kwizatz    1392
Quote:
 Original post by webwraithRight, I'm holding off on the DirectX side of things, but I'm still having problems. When I compile my library, everything seems to run fine, but when I include it and #include its header, the compiler (MinGW) doesn't seem to be able to find the wgl commands(in particular wglCreateContext() and wglMakeCurrent()).I've tried everything I can think of(not much), including;#include the windows.h header in the library, and in the source using the library(and both),andincluding the link libraries in both the source and my custom library(and both), but it doesn't want to know.Any ideas?

You need to include glaux.h for those functions, glaux.h does not come in the MinGW distribution files, you'll have to grab it from the Platform SDK, or declare them yourself.

Share on other sites
webwraith    239
the wglxxx() commands are not in glaux.h, they are in wingdi.h, and that's the reason I'm getting frustrated with it, because it says they're not there, even when I explicitly include wingdi.h into the project