Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt 5.7 adding -std=gnu++11 to my compiler flags, clobbering -std=c++14

I am setting the following flags in my CMakeLists.txt

set(CMAKE_CXX_FLAGS "-std=c++14 -g -O0")

I am then using find_package to locate Qt5Test

find_package(Qt5Test REQUIRED)

I am then creating a Model Test library

add_library          (modeltest STATIC ${SRCS})
target_link_libraries(modeltest Qt5::Test)

For some reason I'm getting -fPIC -std=gnu++11 added to my compiler flags

CMakeFiles/modeltest.dir/flags.make:CXX_FLAGS = -std=c++14 -g -O0 -fPIC -std=gnu++11

This is clobbering my -std=c++14 flag, causing all the c++14 features in my program to end up as compiler errors:

error: ‘foo’ function uses ‘auto’ type specifier without trailing return type
constexpr auto foo()
                   ^
note: deduced return type only available with -std=c++14 or -std=gnu++14
  • Is there a way to fix this?
  • I'm using the latest version of Qt 5.7 downloaded from their site today
like image 839
Steve Lorimer Avatar asked Nov 24 '16 22:11

Steve Lorimer


1 Answers

Do not set CMAKE_CXX_FLAGS explicitly.
Use this instead:

set(CMAKE_CXX_STANDARD 14)

This will definitely set the standard to be used for each target.


As suggested in the comments, the following should be considered too:

set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

See this link for further details.
Thanks to @CraigScott for that pointed it out.


As mentioned in the comments by @wasthishelpful, properties CXX_STANDARD, CXX_STANDARD_REQUIRED and CXX_EXTENSIONS can also be used for a per-target configuration.
See the links above for further details.

like image 181
skypjack Avatar answered Oct 19 '22 02:10

skypjack