gpt4 book ai didi

c++ - cmake target_compile_options 工作但 add_compile_options 没有

转载 作者:太空宇宙 更新时间:2023-11-04 09:59:17 24 4
gpt4 key购买 nike

我有 make (gnu make) 背景,正在为我的 c++ 项目学习 cmake。

我的系统是 ubuntu VM:Linux osboxes 4.15.0-46-generic#49-Ubuntu SMP Wed Feb 6 09:33:07 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

我对这个答案有点困惑here .它说我可以执行以下任一操作:

  • target_compile_options(${PROJ_NAME} PRIVATE -w)
  • add_compile_options(${PROJ_NAME} PRIVATE -w)

我正在编译 log4cpp 代码,当使用标准警告级别编译时会给出大量警告。它是第 3 方代码,所以我不想听到它们。因此我使用 gcc/g++ -w 标志。

当我如上所述使用 target_compile_options 时它工作正常(没有看到警告)但是当我使用 add_compile_options 时它对我不起作用(即我看到所有错误就像 -w应用)。我不确定我在这里做错了什么(但可能是什么!)。

这是我的 CMakeLists.txt 文件以供引用:

cmake_minimum_required(VERSION 3.10.2)

# Set the project name
set(PROJ_NAME log4cpp)
project (${PROJ_NAME})

# Set release build type
#set(CMAKE_BUILD_TYPE release)

# Use c++11 standard
set (CMAKE_CXX_STANDARD 11)

# Include path
include_directories(
inc
inc/log4cpp
)

# Include source files by wild card
file(GLOB SOURCES "src/log4cpp/*.cpp")

# The executable file
#add_executable(${PROJ_NAME} ${SOURCES})
#add_library(${PROJ_NAME} STATIC ${SOURCES})
add_library(${PROJ_NAME} SHARED ${SOURCES})

# Set Warning flags - disable
#target_compile_options(${PROJ_NAME} PRIVATE -w)
add_compile_options(${PROJ_NAME} PRIVATE -w)

# Need threads lib (need to be specified after ${PROJ_NAME} executable)
find_package(Threads REQUIRED)
target_link_libraries(${PROJ_NAME} Threads::Threads)

# From: http://derekmolloy.ie/hello-world-introductions-to-cmake/
# GOT TO: LISTING 5

add_compile_options 的输出:

[  2%] Building CXX object CMakeFiles/log4cpp.dir/src/log4cpp/AbortAppender.cpp.o /home/admin/dev/dl-cmake/log4cpp/src/log4cpp/AbortAppender.cpp:46:10: warning: ‘template<class> class std::auto_ptr’ is deprecated [-Wdeprecated-declarations]
std::auto_ptr<Appender> create_abort_appender(const FactoryParams& params)
^~~~~~~~ In file included from /usr/include/c++/7/memory:80:0,
from /home/admin/dev/dl-cmake/log4cpp/src/log4cpp/AbortAppender.cpp:14: /usr/include/c++/7/bits/unique_ptr.h:51:28: note: declared here template<typename> class auto_ptr;
^~~~~~~~ /home/admin/dev/dl-cmake/log4cpp/src/log4cpp/AbortAppender.cpp: In function ‘std::auto_ptr<log4cpp::Appender> log4cpp::create_abort_appender(const log4cpp::FactoryParams&)’: /home/admin/dev/dl-cmake/log4cpp/src/log4cpp/AbortAppender.cpp:50:20: warning: ‘template<class> class std::auto_ptr’ is deprecated [-Wdeprecated-declarations]
return std::auto_ptr<Appender>(new AbortAppender(name));
^~~~~~~~ In file included from /usr/include/c++/7/memory:80:0,
from /home/admin/dev/dl-cmake/log4cpp/src/log4cpp/AbortAppender.cpp:14: /usr/include/c++/7/bits/unique_ptr.h:51:28: note: declared here template<typename> class auto_ptr;
^~~~~~~~

target_compile_options 的输出:

[  2%] Building CXX object CMakeFiles/log4cpp.dir/src/log4cpp/AbortAppender.cpp.o
[ 4%] Building CXX object CMakeFiles/log4cpp.dir/src/log4cpp/Appender.cpp.o
[ 6%] Building CXX object CMakeFiles/log4cpp.dir/src/log4cpp/AppenderSkeleton.cpp.o
[ 8%] Building CXX object CMakeFiles/log4cpp.dir/src/log4cpp/AppendersFactory.cpp.o
[ 10%] Building CXX object CMakeFiles/log4cpp.dir/src/log4cpp/BasicConfigurator.cpp.o
[ 12%] Building CXX object CMakeFiles/log4cpp.dir/src/log4cpp/BasicLayout.cpp.o

最佳答案

命令 add_compile_options 仅影响目标,这些目标是在命令被调用后创建的。当前 command documentation说:

Adds options to the COMPILE_OPTIONS directory property.

(进一步创建的目标使用目录的属性初始化它们的属性,但之前创建的目标不受此目录属性的影响)。

因为您在 add_compile_options 之前调用了 add_library,所以这个库的选项没有改变。


但是命令 add_definitions 也会影响之前创建的目标。来自 its documentation :

Adds definitions to the compiler command line for targets in the current directory and below (whether added before or after this command is invoked).

关于c++ - cmake target_compile_options 工作但 add_compile_options 没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57637286/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com