使用cmake传递复合编译器选项 [英] Pass compound compiler options using cmake

查看:227
本文介绍了使用cmake传递复合编译器选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用cmake的add_compile_options将"compound"选项传递给编译器.

I am trying to pass "compound" options to the compiler using cmake's add_compile_options.

也就是说,涉及两个(或多个)标志的选项必须按特定顺序传递,并且即使已将这些标志传递给编译器,也不能忽略这些标志.

That is, options involving two (or more) flags that must be passed in a particular order and where none of the flags can be ommitted even if they have already be passed to the compiler.

一个例子是必须通过clang传递给llvm的所有llvm选项,例如-mllvm -ABC -mllvm -XYZ -mllvm ....

An example would be all the llvm options that must be passed through clang to llvm, e.g., -mllvm -ABC -mllvm -XYZ -mllvm ....

我宁愿不使用CMake的反模式set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mllvm -ABC"),而是使用诸如check_cxx_compiler_flag(flag available) + add_compile_option(flag)之类的可移植内容.

I would prefer not to use CMake's antipattern set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mllvm -ABC") but use something portable like check_cxx_compiler_flag(flag available) + add_compile_option(flag).

但是,add_compile_options(-mllvm -XYZ -mllvm -ABC)失败,因为-mllvm选项被缓存并且仅被传递一次,这样,传递-mllvm -XYZ -ABC会导致错误.

However, add_compile_options(-mllvm -XYZ -mllvm -ABC) fails because the -mllvm option gets cached and is only passed once, such that -mllvm -XYZ -ABC is passed resulting in an error.

此外,使用引号"也无济于事,因为引号也会被传递(即,它们没有被删除),这也会导致错误.

Furthermore, using quotes "" doesn't help since then the quotes are also passed (i.e. they are not stripped), which also results in an error.

通过CMake传递这些选项的惯用方式是什么?

What's the idiomatic way of passing these options with CMake?

推荐答案

此处观察到的不是CMake保留引号.

What you observe here is not CMake preserving quotes.

CMake具有特殊类型的字符串-列表.列表是一个字符串,其中的元素用";"分隔.编写somefunction(A B C)时,实际上是在构造字符串"A;B;C",它也是3个元素的列表.但是,当您编写somefunction("A B C")时,您正在传递一个纯字符串.

CMake has special kind of strings - a list. A list is a string, in which elements are delimited with ";". When you write somefunction(A B C) you actually constructing a string "A;B;C", which is also a list of 3 elements. But when you write somefunction("A B C") you are passing a plain string.

现在,当您编写add_compile_options("-mllvm -XYZ;-mllvm -ABC")时,CMake在某种意义上做了正确的事情-它向编译器传递了两个命令行参数"-mllvm -XYZ"-mllvm -ABC".问题是clang将这些参数作为两个也就是说,clang main()中的argv[i]指向"-mllvm -XYZ"字符串,clang将其视为单个标志.

Now, when you write add_compile_options("-mllvm -XYZ;-mllvm -ABC"), CMake does correct thing (in a sense) - it passes compiler two command-line arguments, "-mllvm -XYZ" and "-mllvm -ABC". The problem is that clang gets these arguments as two too. That is, argv[i] in clang's main() points to "-mllvm -XYZ" string and clang treats it as single flag.

我唯一看到的解决方案是使用CMAKE_{C,CXX}_FLAGS设置这些标志.正如@Tsyvarev所说,完全可以使用此变量.

The only solution i can see is to use CMAKE_{C,CXX}_FLAGS to set these flags. As @Tsyvarev said, it's perfectly ok to use this variable.

这篇关于使用cmake传递复合编译器选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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