简体   繁体   English

使用 CMake 生成 DLL 配置类型

[英]Generate DLL configuration type with CMake

I'm using CMake to generate my Visual Studio solutions.我正在使用 CMake 来生成我的 Visual Studio 解决方案。 Right now, CMake generates two configuration Release and Debug , under a single project.现在,CMake 在一个项目下生成两个配置ReleaseDebug Both configs builds a win32 (.exe) application.两个配置都构建了一个 win32 (.exe) 应用程序。

This works great, but I would also like to generate a third configuration, that builds a DLL instead.这很好用,但我也想生成第三个配置,它会构建一个 DLL。 I'm aware that in CMake we can use add_library(LibraryName SHARED [files]) to generate a separate a project that creates builds a DLL target, but that is not what I want.我知道在 CMake 中,我们可以使用add_library(LibraryName SHARED [files])生成一个单独的项目,该项目创建一个 DLL 目标,但这不是我想要的。 Instead, I would like to generate a DLL configuration in visual studio, along side Debug and Release .相反,我想在 Visual Studio 中生成一个DLL配置,旁边是DebugRelease

I can get the configuration by adding set(CMAKE_CONFIGURATION_TYPES Release Debug DLL) in CMakeList, but I'm not sure how to go about actually configuring it.我可以通过在 CMakeList 中添加set(CMAKE_CONFIGURATION_TYPES Release Debug DLL)来获取配置,但我不确定如何 go 来实际配置它。 How do I make this custom configuration actually build a DLL?如何使此自定义配置实际构建 DLL? If possible, I would also like to customize the output name and directory of this configuration, just like how I'm able to with CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE etc.如果可能的话,我还想自定义此配置的 output 名称和目录,就像我可以使用CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE等一样。

Is this possible?这可能吗?

Instead of add_library(LibraryName SHARED [files]) you can keep the add_library(LibraryName [files]) without STATIC or SHARED and then you can run CMake with -DBUILD_SHARED_LIBS:BOOL=OFF or -DBUILD_SHARED_LIBS:BOOL=ON to build static or shared libraries respectively. Instead of add_library(LibraryName SHARED [files]) you can keep the add_library(LibraryName [files]) without STATIC or SHARED and then you can run CMake with -DBUILD_SHARED_LIBS:BOOL=OFF or -DBUILD_SHARED_LIBS:BOOL=ON to build static or shared图书馆分别。

But that would require you to run CMake twice and compile twice.但这需要您运行 CMake 两次并编译两次。

To build both static and shared you can replace this in CMakeLists.txt :要构建 static 和共享,您可以在CMakeLists.txt中替换它:

add_library(LibraryName [files])

with:和:

add_library(LibraryName STATIC [files])
add_library(LibraryName_shared SHARED [files])
set_target_properties(LibraryName_shared PROPERTIES OUTPUT_NAME LibraryName)

You may also have to duplicate other lines (eg target_link_libraries and install ) with the _shared target.您可能还必须使用_shared目标复制其他行(例如target_link_librariesinstall )。

I use this method a lot to build static and shared libraries in one go for libraries whose CMakeLists.txt wasn't designed to do so.我经常使用这种方法来构建 static 和一个 go 中的共享库,用于CMakeLists.txt不适合这样做的库。

As you're adding a separate target this way you should also be able to specify a separate output name and directory for it.当您以这种方式添加单独的目标时,您还应该能够为其指定单独的 output 名称和目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM