dropbox / json11

A tiny JSON library for C++11.
MIT License
2.55k stars 613 forks source link

Hardcoded -std=c++11 causes issues with C files #70

Closed admsyn closed 8 years ago

admsyn commented 8 years ago

When using AppleClang 7.3 to build a project that has a dependency on json11 and contains a C file, the build errors out with:

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

..due to the hardcoded compile flag added in target_compile_options

This can be fixed by making use of CXX_STANDARD, which will allow cmake to add the correct c++11 flag only to appropriate files (PR incoming..)

Minimal reproduction example below:

build ▹ cat ../CMakeLists.txt
cmake_minimum_required(VERSION 3.2)
add_subdirectory(../json11 lib)
add_executable(test_prog test.c)
target_link_libraries(test_prog json11)

build ▹ cmake ..
-- The C compiler identification is AppleClang 7.3.0.7030031
-- The CXX compiler identification is AppleClang 7.3.0.7030031
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/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: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/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: /Users/adam/workspace/test_with_json11/build

build ▹ make
Scanning dependencies of target json11
[ 16%] Building CXX object lib/CMakeFiles/json11.dir/json11.cpp.o
[ 33%] Linking CXX static library libjson11.a
[ 33%] Built target json11
Scanning dependencies of target test_prog
[ 50%] Building C object CMakeFiles/test_prog.dir/test.c.o
error: invalid argument '-std=c++11' not allowed with 'C/ObjC'
make[2]: *** [CMakeFiles/test_prog.dir/test.c.o] Error 1
make[1]: *** [CMakeFiles/test_prog.dir/all] Error 2
make: *** [all] Error 2
build ▹