Visual Studio: Selectively include header (external APIs, ...) to project

Started by
9 comments, last by Jan2go 9 years, 11 months ago

Hello,

I want the following: To include an external project with both headers, libs and possibly DLLs to my engine, but have it included so that in case that project is not installed on the users PC, building will not fail for him. E.g. the FBX model SDK, I don't want to require ever user to install this on his PC if he is not using the FBX file format. In case FBX is missing, the project should still compile, and simply disable the FBX-conversion-routines. Is any such thing possible in MSVC (using 2013 Ultime)?

Advertisement
There are two basic choices here. The simple and easy choice is to ship your program with all of the DLLs it requires, and this is usually preferable.

If you want to support running without a particular DLL, you need to late-bind it. The typical tools for this are LoadLibrary and GetProcAddress. See MSDN for details.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]


There are two basic choices here. The simple and easy choice is to ship your program with all of the DLLs it requires, and this is usually preferable.

If you want to support running without a particular DLL, you need to late-bind it. The typical tools for this are LoadLibrary and GetProcAddress. See MSDN for details.

The problem is not only in the DLLs, but rather in the source too. Is it common in larger projects to ship with all the required external headers too? I assume the second method you mentioned still requires eigther the headers or really load all the functions using GetProcAddress and using them through those address pointers, which appears dissappealing at first sight :/

If your code is dependent on other code, then yes, anyone else trying to compile it needs both your code and the other code.

There are ways around this, though. You could wrap the external code in a DLL of your own, and use that as a minimal interface to the external code instead of trying to GetProcAddress all the stuff in the other DLL. This is also good insulation against ABI differences (note that if you try to pass C++ objects, throw exceptions, or allocate/free memory across DLL boundaries, you're in for nasty bugs).

Another option is to build your code in such a way that it has to be generated before it can be compiled, and the generation step includes detecting missing dependencies and excising code that relies on them. A lot of open-source projects do this.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]


If your code is dependent on other code, then yes, anyone else trying to compile it needs both your code and the other code.

I was kind of hoping there was a magical visul-studio-setting to avoid that, but seems I'm out of luck :(


Another option is to build your code in such a way that it has to be generated before it can be compiled, and the generation step includes detecting missing dependencies and excising code that relies on them. A lot of open-source projects do this.

That sounds interesting, how is it practically realized? Any links/tutorials/explanations you can give me?

A common solution I see is just to wrap all your FBX/etc code in something like
#ifdef MYSDK_ENABLE_FBX
And then the client can choose whether to define that token for your project or not.


A common solution I see is just to wrap all your FBX/etc code in something like
#ifdef MYSDK_ENABLE_FBX
And then the client can choose whether to define that token for your project or not.

That would be acceptable, undefining all the non-used code would be no problem. I would prefer some automated way though - really no way to e.g. read out if a certain system variable like from FBX is set in the preprocessor, and then #define MYSDK_ENABLE_FBX? :D Also, am I not going to also get problems with missing .lib-files etc?

Another option would be to have separate configurations (i.e. Debug, Release, DebugNoFBX, ReleaseNoFBX) and then set the FBX specific files in your project not to compile under that configuration.

Be aware that this can easily get out of hand if you have more than one combination you want to support, so I'm really only mentioning it for the sake of completeness.

Another possible route would be just to put the optional functionality in a separate lib that the user can make use of if they want to.

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

Also, am I not going to also get problems with missing .lib-files etc?

You can specify the lib files to be linked using pragmas if you like (inside ifdefs)
e.g.
#pragma comment( lib, "foo.lib" )

eally no way to e.g. read out if a certain system variable like from FBX is set in the preprocessor, and then #define MYSDK_ENABLE_FBX?

There probably is!...but I've not had experience with it. On every project I've worked on, there's no part of the build process that could possibly vary based on the user's system/environment. They've always deliberately killed off such features to ensure that no matter which team member performs a build, the results will be the same. Doing professional QA is almost impossible otherwise wink.png
I can see that this feature would be very cute though when you're not working under such restrictions biggrin.png


That would be acceptable, undefining all the non-used code would be no problem. I would prefer some automated way though - really no way to e.g. read out if a certain system variable like from FBX is set in the preprocessor, and then #define MYSDK_ENABLE_FBX? biggrin.png Also, am I not going to also get problems with missing .lib-files etc?

There is a way, but it requires some work on your part.

I've done something similar, which is extendable enough to work for what you want.

VS has an option where you can set up a prebuild event. In the prebuild event, call a script file (i use Lua).

In that script file, query the environment variables if a certain variable exists, and if so, generate a file (for instance, when you install the directx sdk, it generates the env. var DXSDK_DIR or some such).

If no env. variables can be queried, then you can manually search the hard drive within the script and find if the installation exists. Whether or not searching a users hard drive is nice/morally ok, i don't know. You decide.

The general idea is that you can generate a header file that you include, and if you find what you want, generate a #define statement with your MACRO of choice that indicates that something exists.

devstropo.blogspot.com - Random stuff about my gamedev hobby

This topic is closed to new replies.

Advertisement