Using C++11 in CMake while still compiling some files as C.

517 views
Skip to first unread message

Petter Strandmark

unread,
Aug 11, 2014, 12:58:05 PM8/11/14
to emscripte...@googlegroups.com
I have uploaded a minimum example at https://github.com/PetterS/emscripten_cmake which uses CMake to build a program.

It works fine (on Windows) as it is. However, I am using the CMake command

 add_definitions("-std=c++11")

to enable C++11. This is a problem if I also have some C files I want to compile in my project. I have tried

 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++11")

but that fails with the message

 FAILED: c:\Program\Emscripten\emscripten\1.21.0\em++.bat    --std=c++11 -DNDEBUG -O2 -MMD -MT CMakeFiles\my_program.dir\
my_program.cpp.o -MF "CMakeFiles/my_program.dir/my_program.cpp.o.d" -o CMakeFiles\my_program.dir\my_program.cpp.o -c c:\
Users\Petter\Dropbox\Programming\emscripten-test\my_program.cpp
c:\Users\Petter\Dropbox\Programming\emscripten-test\my_program.cpp:10:2: warning: 'auto' type specifier is a C++11 exten
sion [-Wc++11-extensions]
        auto my_numbers = {1, 2, 3};
        ^
c:\Users\Petter\Dropbox\Programming\emscripten-test\my_program.cpp:10:20: error: cannot deduce type of initializer list
because std::initializer_list was not found; include <initializer_list>
        auto my_numbers = {1, 2, 3};
                          ^

If I use the former add_definitions-approach, compiling C files fails with

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

So I am stuck. For normal CMake projects, I use the CXX_FLAGS-approach to set the flag for C++ only.

Any thoughts on how I might solve my problem? 

Petter Strandmark

unread,
Aug 11, 2014, 1:20:57 PM8/11/14
to emscripte...@googlegroups.com
Never mind, I just saw my silly typo. Two dashes (--) instead of one (-). :-)

Jukka Jylänki

unread,
Aug 11, 2014, 1:45:06 PM8/11/14
to emscripte...@googlegroups.com
Heh, I've done that way too many times as well! Perhaps we should treat --std=xxx with a double-dash as a fatal error, instead of silently ignoring? :)


--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages