CMAKE如何使用不同的标志编译静态/对象库 [英] CMAKE How to compile static/object library with different flags

查看:115
本文介绍了CMAKE如何使用不同的标志编译静态/对象库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:2个可执行文件,1个核心库,内部包含if/defs

Problem: 2 executables, 1 core library with if/defs inside

UPD2

每个可执行文件都设置自己的预处理器标志首先,创建库的目标

Each executable sets up own preprocessor flags At the beginning, target for library is created

add_library(common_library STATIC ${__SOURCES} ${__HEADERS})

common_library 代码(它是静态库,自己的cmake文件)具有预处理器条件

common_library code (which is static library, own cmake file) has preprocessor conditions

#if defined(MY_DEFINE_1)
// specific code #1 
#elif defined (MY_DEFINE_2)
// specific code #2 
#else
// error
#endif

我在使用CMake进行准备这样的配置时遇到问题.库看不到定义.第一个可执行文件(自己的cmake):

I have problems with CMake to prepare such a configuration. Library doesn't see defines. First executable (own cmake):

target_compile_definitions(common_library PRIVATE -DMY_DEFINE_1)
add_executable(BINARY_1  ${bin1_sources}   )
add_dependencies(BINARY_1 common_library)

第二个可执行文件(自己的cmake):

Second executable (own cmake):

target_compile_definitions(common_library PRIVATE -DMY_DEFINE_2)
add_executable(BINARY_2  ${bin2_sources}   )
add_dependencies(BINARY_2 common_library)

但是 common_library 只能构建一次,有2个定义.应该为每个二进制文件分别构建它.

But common_library is built only once, with 2 defines. It should be built separately for each binary.

推荐答案

由于要更改库中的编译器标志,因此需要多次构建库.您现在正在做的是多次更改库中的标志,这就是为什么您在构建中看到两组编译器定义的原因.

Since you're trying to change the compiler flags on the library, you'll need to build the library multiple times. What you're doing right now is changing the flags on the library multiple times, which is why you're seeing both sets of compiler definitions in the build.

解决问题的最简单方法是只创建两个库条目,每个条目都有自己的标志.这是我能想到的最小的例子:

The simplest way to solve your problem is to just create two library entries, each with their own flags. Here's the smallest example I can think of:

project("sample"
    LANGUAGES
        C
)

add_library(lib1 STATIC foo.c)
target_compile_definitions(lib1
    PRIVATE -DCOND=1
)
add_library(lib2 STATIC foo.c)
target_compile_definitions(lib2
      PRIVATE -DCOND=0
)

我的 foo.c 看起来像这样:

#if COND
#warning "case 1"
#else
#warning "case 2"
#endif

void foo() { }

构建看起来像这样:

$ make
Scanning dependencies of target lib1
[ 25%] Building C object CMakeFiles/lib1.dir/foo.o
/tmp/so/static-lib/foo.c:2:2: warning: #warning "case 1" [-Wcpp]
 #warning "case 1"
  ^~~~~~~
[ 50%] Linking C static library liblib1.a
[ 50%] Built target lib1
Scanning dependencies of target lib2
[ 75%] Building C object CMakeFiles/lib2.dir/foo.o
/tmp/so/static-lib/foo.c:4:2: warning: #warning "case 2" [-Wcpp]
 #warning "case 2"
  ^~~~~~~
[100%] Linking C static library liblib2.a
[100%] Built target lib2

如果大多数库代码是相同的,则可以通过使用三个库(例如,真正通用的代码,然后仅包含条件编译内容的库)来限制编译时间.

If most of the library code is the same, you can limit the amount of compilation time by using three libraries (e.g., truly common code, then libraries that only contain conditional compilation stuff).

这篇关于CMAKE如何使用不同的标志编译静态/对象库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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