我可以在C ++项目中混合使用c和cpp文件吗? [英] Can I mix c and cpp files in a C++ project?

查看:837
本文介绍了我可以在C ++项目中混合使用c和cpp文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,其中混有c文件和cpp文件.链接器向我抛出错误:

I have a project that had c files and cpp files mixed in it. The linker throws errors at me:

undefined reference to <all my functions in the c files>

将所有*.c文件重命名为*.cpp后,错误消失了.所以我不能混合使用这些文件,或者这是一些编译器/链接器选项问题吗?

After I rename all *.c files to *.cpp the errors vanish. So can't I mix those files or is this some compiler/linker options problem?

我的选项:

GCC C ++编译器:

-I/usr/include/glib-2.0 -I/usr/include/gtk-2.0 -I"/myPath" -O0 -g3   \
    -Wall -c -fmessage-length=0 `pkg-config --cflags glib-2.0 gtk+-2.0

GCC C编译器:

-I/usr/include/glib-2.0 -I/usr/include/gtk-2.0 -I"/myPath" -O0 -g3   \
    -Wall -c -fmessage-length=0 -std=c99 `pkg-config --cflags glib-2.0 gtk+-2.0`

GCC C ++链接器:

-L/usr/include/glib-2.0/glib `pkg-config --libs gtk+-2.0`


因为它说的是C ++链接程序,这是否意味着不能将c文件中的目标文件链接到项目中(默认情况下)?


Since it says C++ Linker, does that mean object files from c files cannot be linked into the project (by default)?

推荐答案

extern "C"是一种方法.另一种方法是假设您的C文件属于(几乎无所不在)种类,并且已经可以作为C ++进行编译,只是强迫编译器将它们视为C ++(这为它们提供了适当的名称处理功能,以便与您的C ++文件一起使用).对于GCC,这是-x c++.

extern "C" is one approach. The other approach, assuming your C files are of the (almost ubiquitous) sort which are already compilable as C++, is simply to coerce the compiler into treating them as C++ (which gives them the appropriate name mangling to work with your C++ files). For GCC, this is -x c++.

这篇关于我可以在C ++项目中混合使用c和cpp文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆