Compiling C and C++ files together using GCC

后端 未结 6 907
[愿得一人]
[愿得一人] 2020-12-07 16:38

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

相关标签:
6条回答
  • 2020-12-07 17:17

    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
    
    0 讨论(0)
  • 2020-12-07 17:18

    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.

    0 讨论(0)
  • 2020-12-07 17:35

    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

    0 讨论(0)
  • 2020-12-07 17:37

    Instead of using gcc ,use g++.

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

    0 讨论(0)
  • 2020-12-07 17:39

    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
    
    0 讨论(0)
  • 2020-12-07 17:40

    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
    
    0 讨论(0)
提交回复
热议问题