[CMake] CMake, VS, Nsight Tegra, NDK, mixed C/C++ - applies C++ options to C source

Scott Eberline seberline at telltalegames.com
Wed Dec 14 19:44:55 EST 2016


We have a large project library of mostly C++, mixed with a handful of
C. The C++ compilation commands look correct and work fine. But C
files are passed to clang++.exe -x c, rather than clang.exe. Which
should work, except that CMAKE_CXX_FLAGS is applied while
CMAKE_C_FLAGS is ignored. CMAKE_CXX_FLAGS includes options not
supported for C, so the build fails with messages like,

TRACKEDTOOLTASKWRAPPER`1 : error : invalid argument '-std=c++11' not
allowed with 'C/ObjC'

I've tried overriding the C++ options with C-compatible options, for
example set_source_files_properties(${C_SOURCES} PROPERTIES
COMPILE_FLAGS "-std=c99"). But I've found that per-file COMPILE_FLAGS
precede CMAKE_CXX_FLAGS on the resulting compiler command line, so the
C++ flags still get priority.

I've searched CMake list archives, Stack Overflow, Nvidia's
development tool forums etc. but not found any solution.

(Caveat - I'm trying to use NDK r13b which is not yet supported by
Nsight Tegra. So far, this just involves falsifying the NDK's revision
in its source.properties file, and creating a couple of directory
symbolic links so LLVM libc++ headers can be found where they lived in
NDK r12b. If falsifying the NDK version eventually causes problems,
I'll reconsider it, but for now it seems promising.)

CMake 3.7.1
Visual Studio 2015 Update 3
Nsight Tegra Visual Studio Edition 3.4
NDK r13b

cmake.exe ^
    -DANDROID_PLATFORM=android-24 ^
    -DANDROID_STL=c++_static ^
    -DCMAKE_C_FLAGS="-std=c99" ^
    -DCMAKE_C_STANDARD=99 ^
    -DCMAKE_C_STANDARD_REQUIRED=ON ^
    -DCMAKE_CXX_STANDARD=14 ^
    -DCMAKE_CXX_STANDARD_REQUIRED=ON ^
    -DCMAKE_TOOLCHAIN_FILE=C:\Android\NDK-r13b\build\cmake\android.toolchain.cmake
^
    -G"Visual Studio 14 2015" ^
    -T"DefaultClang" ^

The generated CMakeCCompiler.cmake looks mostly correct, in particular
CMAKE_C_COMPILER and CMAKE_C_SOURCE_FILE_EXTENSIONS, although
CMAKE_<LANG>_IMPLICIT_LINK_LIBRARIES and
CMAKE_<LANG>_IMPLICIT_LINK_DIRECTORIES are set for android-9 and GNU
libstdc++ rather than the values specified in ANDROID_PLATFORM and
ANDROID_STL. As an aside, it seems ANDROID_PLATFORM, ANDROID_STL,
CMAKE_C_STANDARD and CMAKE_CXX_STANDARD are completely ignored in the
generated VS project. I assume that's a topic for a different thread.

Thanks for taking the time to read; any advice is appreciated.


More information about the CMake mailing list