使用 GCC 一起编译 C 和 C++ 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5430688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:09:19  来源:igfitidea点击:

Compiling C and C++ files together using GCC

c++cgcc

提问by ?imon Tóth

I'm trying to compile C and C++ sources together using GCC.

我正在尝试使用 GCC 一起编译 C 和 C++ 源代码。

gcc -std=c++0x test.cpp -std=c99 test.c -lstdc++

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 -Werrorwith this setup. Can these warnings be suppressed somehow?

因此我无法使用-Werror此设置。可以以某种方式抑制这些警告吗?

回答by Erik

Compile the files separately, link with g++

分别编译文件,用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

回答by afrojer

if anyone else is wondering the best way to do this in Android, it's this:

如果其他人想知道在 Android 中执行此操作的最佳方法,那就是:

LOCAL_CFLAGS := -Werror
LOCAL_CONLYFLAGS := -std=gnu99
LOCAL_CPPFLAGS := -std=c++0x

回答by orlp

gccis the C compiler and g++is the C++ compiler. You are mixing the two languages with different styles. Compile apart and then link:

gcc是 C 编译器,g++是 C++ 编译器。您正在混合两种风格不同的语言。编译分开然后链接:

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

回答by Alex Cohn

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:

这与 Android NDK 非常相关。幸运的是,有一个丑陋的解决方法。要将所有 C 文件编译为c99,并将所有 CPP文件编译为c++0x,请将以下几行添加到 Android.mk 文件中:

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.

这在带有 arm-linux-androideabi-4.6 工具链的最新 NDK r8b 中有效,但我不能保证它可以在未来版本中使用,我没有用早期版本对其进行测试。

回答by Chaithra

Instead of using gcc ,use g++.

而不是使用 gcc ,使用 g++ 。

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

这适用于 .cpp 和 .c 文件这两种类型的文件。

回答by Adam Mendoza

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

我也遇到了这个问题。我没有找到一种使用单行代码编译 c 和 c++ 的方法,但是使用 autotools autoconf 它将为每个 .c 和 .cpp 或 .cc 生成正确的配置和 Makefile 以单独编译它们然后链接它们。 https://www.gnu.org/software/automake/manual/html_node/Autotools-Introduction.html