8

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

David Levy
  • 430
  • 5
  • 18
  • An example of what part do you need? And Verbose confirm what I thought, no compilation options are given to g++ – David Levy Nov 09 '16 at 22:40
  • 1
    A minimal example that we could copy to our systems and try out. You did not include CMake code, where the problem most probably is located, cf. http://stackoverflow.com/help/mcve. – usr1234567 Nov 10 '16 at 07:47

3 Answers3

22

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

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149
2

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

usr1234567
  • 21,601
  • 16
  • 108
  • 128
  • This work indeed (it was a last try poorly executed before going home). But `add_compile_options` is a better way to add compiler option, so im accepting the other answer. – David Levy Nov 10 '16 at 14:19
  • No, the third argument is not ignored by `set()`. When you pass three or more arguments to `set()`, you end up creating a _list_. That's what the `...` part of `...` means in the CMake docs you linked to. CMake represents a list as a string with items separated by a semicolon, which is why the output in the second half of the original question contained a semicolon. All that said, the correct way to append to `CMAKE_CXX_FLAGS` is indeed as this answer recommends. – Craig Scott Nov 12 '16 at 22:14
  • @CraigScott You are right, thanks for hint, I corrected my answer. – usr1234567 Nov 12 '16 at 22:33
1

CMake documentation is ridiculous, they removed Adds options to the compiler command line for targets in the current directory and below that are added after this command is invoked. from add_compile_options() manual. Thanks for this hint Florian.

https://cmake.org/cmake/help/v3.21/command/add_compile_options.html

Pierwiastek
  • 134
  • 9