cmake target_compile_options有效,但add_compile_options无效

时间:2019-08-24 10:54:00

标签: c++ linux cmake

我有一个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代码,当使用标准警告级别进行编译时,它将产生大量警告。它的第三方代码,所以我不想听到它们。因此,我使用gcc / g ++ -w标志。

当我如上所述使用target_compile_options时,它工作正常(未看到警告),但是当我使用add_compile_options时,它对我不起作用(即,我看到所有错误,好像{{1 }}被应用)。我不确定我在这里做错了什么(但可能有事!)。

这是我的CMakeLists.txt文件供参考:

-w

add_compile_options的输出:

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

target_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;
                            ^~~~~~~~

1 个答案:

答案 0 :(得分:1)

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

  

将选项添加到COMPILE_OPTIONS目录属性。

(其他创建的目标使用目录的属性初始化其属性,但是先前创建的目标不受此目录属性的影响。)

由于您在add_library之前调用了add_compile_options,因此该库的选项不会更改。


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

  

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