有没有办法自动使库静态或动态?

时间:2022-03-24 21:33:48

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?

看起来像CMake可能是它,但是,除了构建东西,我还想从我的代码中缓解__declspec(dllimport)和__declspec(dllexport) - 这样我就可以自动在静态和动态之间切换。虽然使用预处理器宏相当容易,但我想也许人们已经在使用某种形式的系统?

3 个解决方案

#1


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

我喜欢使用CMake来避免这些类型的问题。

#2


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.

如果您希望它是跨平台的,您将不得不使用像SCons或Make这样的工具,并根据您传入的任何命令变量设置不同的编译器/链接器参数。您必须为每个平台和链接执行此操作 - 你支持的类型组合。

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.

在Visual Studio的配置管理器中(您不仅限于发布和调试),然后您可以拥有ReleaseStatic和ReleaseDynamic以及您可以想到的任何其他配置。然后,您只需为每个配置设置正确的编译器和链接器开关。

#3


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.

下载cURL源代码并查看其设置。它可以在Windows中为DLL或静态构建libcurl,在Unix中构建静态或共享对象。

#1


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

我喜欢使用CMake来避免这些类型的问题。

#2


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.

如果您希望它是跨平台的,您将不得不使用像SCons或Make这样的工具,并根据您传入的任何命令变量设置不同的编译器/链接器参数。您必须为每个平台和链接执行此操作 - 你支持的类型组合。

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.

在Visual Studio的配置管理器中(您不仅限于发布和调试),然后您可以拥有ReleaseStatic和ReleaseDynamic以及您可以想到的任何其他配置。然后,您只需为每个配置设置正确的编译器和链接器开关。

#3


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.

下载cURL源代码并查看其设置。它可以在Windows中为DLL或静态构建libcurl,在Unix中构建静态或共享对象。