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

查看:169
本文介绍了我可以混合在1 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>

在我所有的重命名 *。ç文件 *。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`


OK,因为它说,C ++链接器,这是否意味着目标文件从C文件不能链接到项目(默认)?


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

推荐答案

的externC是方法之一。另一种方法,假设你的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++.

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

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