Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cmake not applying compile option using add_compile_options

Tags:

g++

cmake

im trying to use cmake with the following compile options using add_compile_options : add_compile_options(-pipe -O2 -std=c++98 -W -Wall -pedantic)

But it does not seem to be actually used during the compilation.

make -n | grep pedantic does not return anything

Just for information, my cmake command and what it returns :

cmake -G"Unix Makefiles" -DARCH:STRING=x86 -Bosef -H. -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/g++
-- The C compiler identification is GNU 4.8.3
-- The CXX compiler identification is GNU 4.8.3
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/g++
-- Check for working CXX compiler: /usr/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- cmake run for linux x86
-- Configuring done
-- Generating done
-- Build files have been written to: /home/davidlevy/WS/LC4/main/osef

What can I do to actually have the options applied?

PS : I noticed they were not applied because make does not output a single warning

EDIT : If instead I use

set (
  CMAKE_CXX_FLAGS
  "${CMAKE_CXX_FLAGS} "
  "${CMAKE_CXX_FLAGS} -pipe -O2 -std=c++98 -W -Wall -pedantic"
)

Make does /usr/bin/g++ ; -pipe -O2 -std=c++98 -W -Wall -pedantic -I ENDOFTHECOMMAND I dont know where does this ; comes from

like image 839
David Levy Avatar asked Dec 14 '22 02:12

David Levy


2 Answers

add_compile_options() does append to the COMPILE_OPTIONS directory property and is taken as default for all COMPILE_OPTIONS properties of targets coming after the add_compile_options() command.

In difference to CMAKE_CXX_FLAGS which applies to all targets in the current CMakeLists.txt.

So make sure add_compile_options() command is before the add_library()/add_executable in question.

From add_compile_options() documentation:

Adds options to the compiler command line for targets in the current directory and below that are added after this command is invoked.

References

  • Is Cmake set variable recursive?
like image 85
Florian Avatar answered Jun 05 '23 10:06

Florian


Instead of

set (
  CMAKE_CXX_FLAGS
  "${CMAKE_CXX_FLAGS} "
  "${CMAKE_CXX_FLAGS} -pipe -O2 -std=c++98 -W -Wall -pedantic"
)

it should be

set(
  CMAKE_CXX_FLAGS
  "${CMAKE_CXX_FLAGS} -pipe -O2 -std=c++98 -W -Wall -pedantic"
)

Because the synthax you want to use is set(variable value). In your case you set CMAKE_CXXFLAGS to a list of the second (empty string) and third argument (flags added by you).

Documentation: https://cmake.org/cmake/help/v3.7/command/set.html

like image 25
usr1234567 Avatar answered Jun 05 '23 10:06

usr1234567