code_fodder
code_fodder

Reputation: 16341

cmake target_compile_options worked but add_compile_options did not

I am have a make (gnu make) background and am learning cmake for my c++ projects.

My system is a 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

I am a bit confused by this answer here. It says that I can do either of:

I am compiling log4cpp code which when compiled with standard warning levels gives a load of warning. Its 3rd party code so I don't want to hear about them. Therefore I use gcc/g++ -w flag.

When I used the target_compile_options as above it works fine (no warning seen) but when I used the add_compile_options it did not work for me (i.e. I see all the errors as though -w is not applied). I am not sure what I am doing wrong here (but probably something!).

Here is my CMakeLists.txt file for reference:

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

output from 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;
                            ^~~~~~~~

output from 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

Upvotes: 3

Views: 5722

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 65928

Command add_compile_options affects only on the targets, which are created after the command is invoked. Current command documentation says:

Adds options to the COMPILE_OPTIONS directory property.

(Further created targets initialize their property with the directory's one, but previously created targets are unaffected by this directory property).

Because you call add_library before the add_compile_options, this library's options are not changed.


But the command add_definitions affects on previously created targets too. From 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).

Upvotes: 4

Related Questions