【问题标题】:cmake target_compile_options worked but add_compile_options did notcmake target_compile_options 工作,但 add_compile_options 没有
【发布时间】:2019-12-29 10:16:06
【问题描述】:

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

我的系统是 ubuntu 虚拟机: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 不是特定于目标的,因此它不接受目标名称。因为此命令不是特定于目标的,所以它也不接受像PRIVATE 这样的与隐私相关的关键字:此类参数被视为常规编译选项。您可以在documentation 中找到该命令的签名。

标签: c++ linux cmake


【解决方案1】:

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

COMPILE_OPTIONS 目录属性添加选项。

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

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


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

为当前目录及以下目录中的目标添加定义到编译器命令行(无论是在调用此命令之前还是之后添加)。

【讨论】:

  • 啊.. 好的,我现在明白了,谢谢(以及您的评论 - 当我看到链接答案中的省略号时,我认为这意味着使用相同的选项!:))
猜你喜欢
  • 2020-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-26
  • 2021-12-05
  • 1970-01-01
相关资源
最近更新 更多