每个目标不同的CMAKE_BUILD_TYPE [英] Different CMAKE_BUILD_TYPE per target

查看:61
本文介绍了每个目标不同的CMAKE_BUILD_TYPE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个非常大的项目,我正在从使用自定义Makefile转到使用cmake的过程中,但是我仍然缺少通过Makefile实现的功能.

I am working on a really large project, which I'm in the process of moving from using custom Makefiles to using cmake instead, but I'm still missing a functionality that was implemented with the Makefiles.

该项目有许多子目录,每个子目录都被编译成一个静态库,然后链接到最终的可执行文件中.

The project has many sub-directories, each one of which is compiled into a static library, and then linked into the final executable.

这是一个小例子

src/
  lib1/
  lib2/
  lib3/
  main.cpp
  CMakeLists.txt

和CMakeLists.txt中的内容可能是这样的:

and in CMakeLists.txt might be something like this:

add_subdirectory(lib1)
add_subdirectory(lib2)
add_subdirectory(lib3)
add_executable(test main.cpp)
target_link_libraries(test PUBLIC lib1 lib2 lib3)

我想调试最终的可执行文件,但是,我不想构建所有带有调试符号且没有优化的静态库,因为这样调试会变得太慢.

I want to debug the final executable, but I don't want to build all static libraries with debug symbols and no optimizations, because then the debugging becomes too slow.

所以我想用 CMAKE_BUILD_TYPE = Release lib1 lib3 CMAKE_BUILD_TYPE构建 lib2 =调试.

So I want to build lib2 with CMAKE_BUILD_TYPE=Release and lib1 and lib3 with CMAKE_BUILD_TYPE=Debug.

请记住,实际上不是10个图书馆,而是3个图书馆,我希望能够为每个图书馆以及同时为多个图书馆做到这一点.

Please bear in mind that instead of three libraries, there are actually ~10, and I want to be able to do that for each one of them, and for a number of them at the same time.

有没有一种方法可以从主要的 CMakeLists.txt 中进行?

Is there a way to do that from the main CMakeLists.txt?

我希望通过命令行可以实现以下目的:

What I would prefer would be something that would make this possible from the command line:

cmake -DDEBUG_LIBS={lib1,lib3} /path/to/src
cmake --build .

推荐答案

不要设置 CMAKE_BUILD_TYPE 保持空白或进行自定义设置,以准确设置所需的基本标志.然后为所需的库添加其他调试和优化.我建议创建每个库目标都调用的函数(或宏),以检查其是否显示在 DEBUG_LIBS 中,然后使用正确的值调用target_compile_options.但是您应该将其设置为 -DDEBUG_LIBS = lib1; lib3 ,以便列表处理有效.

Don't set CMAKE_BUILD_TYPE keep it blank or make a custom one where you set exactly what you want the base flags to be. Then add the additional debug and optimizations for the libraries that you want. I would suggest creating a function (or macro) that each library target calls that checks to see if it shows up in DEBUG_LIBS and then call target_compile_options with the correct values. But you should set is as -DDEBUG_LIBS=lib1;lib3 so that list handling works.

function(check_debug libname)
  if(${libname} IN_LIST DEBUG_LIBS)
    target_compile_options(${libname} PRIVATE -g -O0)
  endif()
end_function()

这篇关于每个目标不同的CMAKE_BUILD_TYPE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆