views:

127

answers:

3

I know this might be a long shot, but here it goes. I have several active projects and each has sub project library which gets compiled when the main project compiles. These libraries are dynamic ones, but recently there was an issue that might arise a need for those libraries (most of them are shared between projects) to be static instead of dynamic.

Now, I am quite sure someone has devised a system where I could make a library which could be compiled wither as static into the project or dynamic one with something like a simple preprocessor directive or something. If not, I'll pipe dream away.

edit:

looks like CMake might be it, however, apart from building stuff I would also like to alleviate __declspec(dllimport) and __declspec(dllexport) from my code - so that I can automatically switch between static and dynamic. Although it is fairly easy to do with preprocessor macros, I thought maybe there might be some form of a system already in use by people?

A: 

If you want it to be cross platform, you'll have to use a tool like SCons or Make and setup different compiler/linker arguments based on whatever command variables you pass in. You'll have to do it for each platform and link-type combo you support.

In Visual Studio's Configuration Manager (you're not limited to just Release and Debug) and then you can have ReleaseStatic and ReleaseDynamic and any other configs you can dream up. Then you just set the proper compiler and linker switches for each configuration.

jeffamaphone
did you spot the gcc and cross-platform tags?
anon
A: 

Download the cURL source code and have a look at its setup. It can build libcurl for DLL or static in Windows, and static or shared object in Unix.

Zan Lynx
+2  A: 

I like to use CMake to avoid these type of problems.

JesperE
Perhaps you could explain how you use CMake to avoid these types of problems?
anon
In cmake you can build a library as either static or shared by only adding/changing one keyword to the ADD_LIBRARY(<libraryname>, SHARED/STATIC ....) command. There's nothing else to it. Well, you'd have to change the build system of the lib to cmake, which shouldn't be much of a hassle.
AndreasT
After getting used to CMake, I've actually started to forget the command line stuff needed to build libraries etc., both on Windows and Unix. It is a real relief.
JesperE