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.
c开发者_高级运维c1plus: 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?
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
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
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
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.
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
Instead of using gcc ,use g++.
That is for both type of files, .cpp and .c files.
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.
精彩评论