41

I'm trying to compile C and C++ sources together using GCC.

gcc -std=c++0x test.cpp -std=c99 test.c -lstdc++

Now, this works fine, except that I get two warnings.

cc1plus: warning: command line option "-std=c99" is valid for C/ObjC but not for C++
cc1: warning: command line option "-std=c++0x" is valid for C++/ObjC++ but not for C

Therefore I can't use -Werror with this setup. Can these warnings be suppressed somehow?

1
  • 1
    What you think you're doing is very different from what you're doing. You are first setting the language standard to C++0x and then setting it to C99, which is equivalent to just using C99 for both files. However, compiling C++ in C99 mode is impossible, so for the .cpp file, the compiler will fall back to its default, which is C++98. You're therefore compiling the C file in C99, and the C++ file in C++98. While parsing options, GCC sees that you have incompatible files for either language version option, thus it ouputs warnings for each option. Commented Oct 1, 2013 at 15:17

7 Answers 7

65

Compile the files separately, link with g++

gcc -c -std=c99 -o file1.o file1.c
g++ -c -std=c++0x -o file2.o file2.cpp
g++ -o myapp file1.o file2.o
Sign up to request clarification or add additional context in comments.

4 Comments

Yeah, sure, but I need to compile it in one GCC call. I'm using an API for calling GCC and I can only add flags.
@Let_Me_Be: Then you'll need to either write a script/wrapper and call that, or compile your c as c++ without specifying C99
Hmm, I could actually precompile the C++ part (since that won't change) :-)
If you are afraid to link together .o from different compilers, you can use g++ -x c instead of gcc.
16

if anyone else is wondering the best way to do this in Android, it's this:

LOCAL_CFLAGS := -Werror
LOCAL_CONLYFLAGS := -std=gnu99
LOCAL_CPPFLAGS := -std=c++0x

Comments

7

gcc is the C compiler and g++ is the C++ compiler. You are mixing the two languages with different styles. Compile apart and then link:

gcc -std=c99 -c -o test.c.o test.c
g++ -std=c++0x -c -o test.cpp.o test.cpp
g++ -o executable test.cpp.o test.c.o

2 Comments

This solves my confusion. Could you explain briefly why g++ isn't designed to compile c too?
@VimNing Why should it? C is a different language than C++.
2

I ran into this problem too. I didn't find a way to compile c and c++ with a one liner but using autotools autoconf it will generate the proper configuration and Makefile for each .c and .cpp or .cc to compile them individually and then link them. https://www.gnu.org/software/automake/manual/html_node/Autotools-Introduction.html

Comments

2

Instead of using gcc ,use g++.

That is for both type of files, .cpp and .c files.

5 Comments

But won't g++ compile the C files as C++?
While giving the compilation instruction, if you specify explicitly as .c, then as per my knowledge, it may not take as .cpp. But please confirm this with someone whose knows correctly.
C source files compiled with g++ will be compiled as C++ code.
@LokiAstari yes I have just tested with gcc 6.3.0. But g++ can be forced to compile as C code with the -x c option.
@GabrielDevillers You can force the compiler to do anything if you know the correct arguments. You can compile files ending in ".fart" as C files if you want. But the point here is that the default behavior (with no flags) is slightly un-expected. Normally you would expect the compiler to respect the file name suffix (or warn you about it). But g++ does not; it treats both *.c and *.cpp as C++ source files. This can drastically change their meaning (as you know C and C++ are completely different languages).
1

This is very relevant for Android NDK. Luckily, there is an ugly workaround. To make all C files compiled as c99, and all CPP files as c++0x, add the following lines to Android.mk file:

LOCAL_CPPFLAGS += -std=c++0x
LOCAL_C99_FILES := $(filter %.c, $(LOCAL_SRC_FILES))
TARGET-process-src-files-tags += $(call add-src-files-target-cflags, $(LOCAL_C99_FILES), -std=c99)

This works in the latest NDK r8b with arm-linux-androideabi-4.6 toolchain, but I cannot guarantee that it will work in future versions, and I didn't test it with earlier versions.

Comments

0

Try including the cpp in the c or vice versa and then use g++ to compile, I think gnu will automatically compile it as a header file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.