Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CMake:add_compile_options()发布/调试配置错误_Cmake_G++_Ubuntu 16.04 - Fatal编程技术网

CMake:add_compile_options()发布/调试配置错误

CMake:add_compile_options()发布/调试配置错误,cmake,g++,ubuntu-16.04,Cmake,G++,Ubuntu 16.04,这有什么问题吗 if(CMAKE_COMPILER_IS_GNUCXX OR MINGW OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") add_compile_options("$<$<CONFIG:RELEASE>:-W -Wall -O3 -pedantic>") add_compile_options("$<$<CONFIG:DEBUG>:-W -Wall -O0 -g -pedanti

这有什么问题吗

if(CMAKE_COMPILER_IS_GNUCXX OR MINGW OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
    add_compile_options("$<$<CONFIG:RELEASE>:-W -Wall -O3 -pedantic>")
    add_compile_options("$<$<CONFIG:DEBUG>:-W -Wall -O0 -g -pedantic>")
endif()

…但我想这不是故意的…

我已经尝试了你的例子,可以重现你的问题:

c++: error: unrecognized command line option ‘-W -Wall -O3 -pedantic’
add\u compile\u options()
命令将参数作为选项列表,因此必须使用
作为分隔符

以下内容在我的
gcc
/
make
环境中确实有效:

添加编译选项($)
添加编译选项($)
或者写为:

字符串(
附加选项
"$"
"$"
)
添加编译选项(“${opts}”)
参考资料

与CMAKE_CXX_FLAGS变量不同,CMAKE_CXX_FLAGS变量包含带有选项的单个字符串,用空格分隔,命令
add_compile_options
接受每个选项的分隔参数。也就是说,如果没有生成器表达式,正确的用法将是:

add_compile_options("-W" "-Wall" "-O3" "-pedantic")
因此,如果您想使用生成器表达式,您需要将其附加到每个选项中,就像您在第二次尝试中所做的那样

如果您有许多每个配置选项,并且您发现手动重复每个选项的生成器表达式tedios,那么您可以创建一个简单的函数,它可以为您实现这一点。像这个:

# add_compile_options_config(<CONFIG> <option> ...)
function(add_compile_options_config CONFIG)
    foreach(opt ${ARGN})
        add_compile_options("$<$<CONFIG:${CONFIG}>:${opt}>")
    endforeach()
endfunction()

如果您想在项目中的所有目标中添加编译器“-W”、“-O3”选项,但以配置特定的方式,考虑使用CMACHYCXXFRAWSARX变量:

string(APPEND CMAKE_CXX_FLAGS_RELEASE " -W -Wall -03 -pedantic")

您在示例中称之为
add\u compile\u options
,而不是新的
add\u compile\u options\u config
,这是故意的还是打字错误?这是一个打字错误,现已修复。谢谢。嗯……我很确定我已经试过了,但没用。请看Tsyvarev的答案。@juzzlin已经尝试过了,它确实适用于我的CMake版本3.10.2
add_compile_options_config(RELEASE "-W" "-Wall" "-O3" "-pedantic")
add_compile_options_config(DEBUG "-W" "-Wall" "-O0" "-g" "-pedantic")
string(APPEND CMAKE_CXX_FLAGS_RELEASE " -W -Wall -03 -pedantic")