我尝试使用add_compile_options将cmake与以下编译选项一起使用:add_compile_options(-pipe -O2 -std=c++98 -W -Wall -pedantic)
但在编译过程中似乎并没有实际使用它。
make -n | grep pedantic
不会返回任何内容
仅供参考,我的cmake命令及其返回的内容:
cmake -G"Unix Makefiles" -DARCH:STRING=x86 -Bosef -H. -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/g++
-- The C compiler identification is GNU 4.8.3
-- The CXX compiler identification is GNU 4.8.3
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/g++
-- Check for working CXX compiler: /usr/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- cmake run for linux x86
-- Configuring done
-- Generating done
-- Build files have been written to: /home/davidlevy/WS/LC4/main/osef
如何实际应用这些选项?
PS:我注意到它们没有被应用,因为make没有输出单个警告
编辑:如果我使用
set (
CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} "
"${CMAKE_CXX_FLAGS} -pipe -O2 -std=c++98 -W -Wall -pedantic"
)
Make does /usr/bin/g++ ; -pipe -O2 -std=c++98 -W -Wall -pedantic -I ENDOFTHECOMMAND
我不知道这是哪里的;
答案 0 :(得分:3)
add_compile_options()
会附加到COMPILE_OPTIONS
目录属性,并且作为 COMPILE_OPTIONS
命令之后目标的所有add_compile_options()
属性的默认值
与适用于当前CMAKE_CXX_FLAGS
中的所有目标的CMakeLists.txt
不同。
因此,在相关的add_compile_options()
/ add_library()
之前,请确保add_executable
命令为。
来自add_compile_options()
文档:
为当前目录及以下目标的编译器命令行添加选项,>>在调用此命令后添加。
<强>参考强>
答案 1 :(得分:1)
而不是
set (
CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} "
"${CMAKE_CXX_FLAGS} -pipe -O2 -std=c++98 -W -Wall -pedantic"
)
应该是
set(
CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -pipe -O2 -std=c++98 -W -Wall -pedantic"
)
因为您要使用的synthax是set(variable value)
。在您的情况下,您将CMAKE_CXXFLAGS
设置为第二个(空字符串)和第三个参数(由您添加的标志)的列表。
答案 2 :(得分:0)
CMake 文档很荒谬,他们删除了
Adds options to the compiler command line for targets in the current directory and below that are added after this command is invoked.
来自 add_compile_options()
手册。感谢弗洛里安的提示。
https://cmake.org/cmake/help/v3.21/command/add_compile_options.html