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

2019-02-23 02:50发布

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

1条回答
Juvenile、少年°
2楼-- · 2019-02-23 03:53

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.

查看更多
登录 后发表回答