3

I'm trying to cross compile some c++ library for QNX neutrino using cmake. In CMakeLists.txt file I specified CMAKE_CXX_STANDARD 14 required, but the resulting compiler command line does not contain the -std=c++14 option.

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

I've tried using target compile features:

target_compile_features(my_library PRIVATE cxx_std_14)

but that gives me the following error:

CMake Error at CMakeLists.txt:53 (target_compile_features):
    target_compile_features no known features for CXX compiler

    "QCC"

    version 5.4.0.

When I'm using check_cxx_compiler_flag feature, it seems to recognize the option:

include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-std=c++14 CXX14_SUPPORT)
if(CXX14_SUPPORT)
  message("c++14 support found")
else()
  message("c++14 unsupported")
endif()

This outputs message

c++14 support found

Running qcc manually it accepts the -std=c++14 option just fine and the code using std::make_unique compiles just fine.

Also using the native compiler (Ubuntu 18.04, gcc) everything work fine with cmake generated makefiles. make VERBOSE=1 displays the following command line (I removed some directories):

/usr/local/bin/c++  -Dshm_transfer_EXPORTS -I...  -fPIC   -std=gnu++14 -o CMakeFiles/shm_transfer.dir/src/SharedMemoryTransfer.cpp.o -c .../SharedMemoryTransfer.cpp

as opposed to the command line using qcc toolchain:

.../qnx700/host/linux/x86_64/usr/bin/qcc -lang-c++ -Vgcc_ntox86_64 -lang-c++ -Dshm_transfer_EXPORTS -I...  -fPIC   -o CMakeFiles/shm_transfer.dir/src/SharedMemoryTransfer.cpp.o -c .../SharedMemoryTransfer.cpp

I would have expected the cmake command to recognize that qcc supports the -std=c++14 option and generates the corresponding command lines because of the CMAKE_CXX_STANDARD setting.

joggerwolf
  • 59
  • 1
  • 6

2 Answers2

4

Use

set_property(TARGET ${PROJECT_NAME} PROPERTY LINKER_LANGUAGE CXX)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 14)

. Using this you can stick the compiler setting to the target, while global flags are dis encouraged and can be overwritten by other cmake consumers. This the reason I assume why the deprecated set(CMAKE_CXX_STANDARD 14) did not help you: I can not see your full CMakeLists.txt and bet you have many sub folders and other targets, which could reset the CMAKE_CXX_STANDARD them selfs. Also make sure of the ordering of the CMake commands.

And you can replace ${PROJECT_NAME} with my_library if you want.

Superlokkus
  • 4,731
  • 1
  • 25
  • 57
  • While this is good advice - how does it answer OP's question? I believe it doesn't. – einpoklum Aug 22 '19 at 11:30
  • @einpoklum I added some hints why I think it does. – Superlokkus Aug 22 '19 at 11:36
  • Thanks, @Superlokkus. This solves the problem. In fact I would want to use the setting for all local projects, i.e. even toolchain independent and for all targets. Do you know whether I can make this setting target independent in the top-level cmake file? – joggerwolf Aug 22 '19 at 12:01
  • @joggerwolf I am almost certain that setting `CMAKE_CXX_STANDARD` and the other 2 as you did are the best you can do, but you can not really be sure, especially since the dependencies could add compiler flags manually or by using preprocessor directives. Its also not feasible in my perspective, dependencies can do too much bad stuff, and you have to be able to thrust them or patch them. – Superlokkus Aug 22 '19 at 12:14
  • 1
    @Superlokkus: sorry, I had to reopen this. It seems I still had the workaround manually setting -std=gnu++14 active when I tried the set_property. Now, having everything cleaned up and all CMakeLists.txt updated, I'm back where it started: the command line does not contain the option for c++ 14 support :-( – joggerwolf Aug 22 '19 at 12:44
  • Then it might be a cmake bug, but I am not certain, without a better picture of your cmakelists – Superlokkus Aug 22 '19 at 12:54
  • I updated following this instruction and it worked. Point to be noted, it works in conjunction with set command (At least for me). – rafee May 05 '20 at 17:50
0
add_compile_options(-std=gnu++14)

Add this to your project level CMakeLists.txt file, not in toolchain.

Shailendra Yadav
  • 1,822
  • 1
  • 12
  • 16