37

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?

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
  • 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. – Damon Oct 01 '13 at 15:17

7 Answers7

60

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
Erik
  • 88,732
  • 13
  • 198
  • 189
15

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
afrojer
  • 151
  • 1
  • 3
5

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
orlp
  • 112,504
  • 36
  • 218
  • 315
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.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
1

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

Adam Mendoza
  • 5,419
  • 2
  • 25
  • 31
1

Instead of using gcc ,use g++.

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

abunickabhi
  • 558
  • 2
  • 9
  • 31
Chaithra
  • 1,130
  • 3
  • 14
  • 22
  • 4
    But won't `g++` compile the `C` files as `C++`? – Šimon Tóth Mar 25 '11 at 09:36
  • 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. – Chaithra Mar 25 '11 at 09:50
  • 8
    C source files compiled with g++ will be compiled as C++ code. – Martin York Mar 25 '11 at 10:24
  • 1
    @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. – Gabriel Devillers Dec 17 '17 at 12:23
  • 2
    @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). – Martin York Dec 17 '17 at 18:05
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.