【发布时间】: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 中找到该命令的签名。