0

When I compile a set of c++ source files using nested namespaces directly with clang++ -std=c++1z -Wall -Wextra I get no warnings.

Trying to compile the same source files with CMake, I get the following output which seems perfectly happy:

-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is Clang 6.0.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to:

But when I run make I get this, everywhere:

warning: nested namespace
  definition is a C++17 extension; define each namespace separately [-Wc++17-extensions]

I've got the following line in the top-level CMakeLists.txt:

set (CMAKE_CXX_FLAGS_INIT "-Wall -Wextra -g -std=c++1z")

Is this just a problem with Make / should I try suppressing this or am I doing something wrong with CMake?

CMake version: 3.15.3

clang version: 6.0.0-1ubuntu2

Target: x86_64-pc-linux-gnu

raspy
  • 3,995
  • 1
  • 14
  • 18
Adam Coville
  • 141
  • 7
  • 4
    Use `VERBOSE=1` option for `make` to see **actual** command line used for compile the sources. Check that this command line contains expected compiler options. – Tsyvarev Oct 15 '19 at 07:41
  • /usr/bin/c++ -Dcetus_NL_EXPORTS -I/home/acoville/Desktop/cetus/NL/src -fPIC -o CMakeFiles/cetus_NL.dir/haversine.cpp.o -c /cetus/NL/src/haversine.cpp – Adam Coville Oct 15 '19 at 08:50
  • Looks like a call to my default c++ compiler, but without the flags I wanted. Is that what you're talking about? – Adam Coville Oct 15 '19 at 08:50
  • 1
    Yes, it seems you are setting compiler flags in a wrong way. Note, that not only `set` command is important, but its position relative to the `project()` call is important too. Make sure you have seen other questions on this topic, e.g. [that one](https://stackoverflow.com/questions/11783932/how-do-i-add-a-linker-or-compile-flag-in-a-cmake-file). – Tsyvarev Oct 15 '19 at 10:22
  • 1
    See https://stackoverflow.com/a/42842447/1028434 about setting language features. Otherwise set the variable as a cache entry before the `project()` command, or include it as part of a tool-chain file, or as -D on the command line. – fdk1342 Oct 15 '19 at 11:25
  • Thank you I'll try this tomorrow – Adam Coville Oct 15 '19 at 11:59
  • I found that this did the trick. Thanks very much for your help https://cmake.org/cmake/help/v3.0/command/add_compile_options.html#command:add_compile_options – Adam Coville Oct 15 '19 at 22:24
  • 1
    `CMAKE_CXX_FLAGS_INIT` is used to initialize the first time you run cmake. Don't use `_INIT`, they are not for that. Do `target_compile_features(${TARGET_NAME} PRIVATE cxx_std_17)`. In old-old-older cmake you would do `set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1z")` – KamilCuk Oct 17 '19 at 15:21

0 Answers0