Compiling C and C++ files together using GCC
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.
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