Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling C and C++ files together using GCC

Tags:

c++

c

gcc

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?

like image 675
Šimon Tóth Avatar asked Mar 25 '11 09:03

Šimon Tóth


People also ask

How do I compile two files in gcc?

Talking about main.o "calling functions in" module.o is perfectly fine, but an .o file is not a source file, it's a compiled object file. If "put my source code in files with extension .o " actually meant "compile my source code into files with extension .o " then the situation would make a whole lot more sense.

Can gcc compile both C and C++?

gcc is used to compile C program. g++ can compile any . c or . cpp files but they will be treated as C++ files only.


2 Answers

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 
like image 22
afrojer Avatar answered Sep 22 '22 18:09

afrojer


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 
like image 185
Erik Avatar answered Sep 22 '22 18:09

Erik