C code。与C ++编译:未定义参考 [英] C code compiled with C++: undefined reference
问题描述
我有一个小程序,我可以用GCC和ICC编译没有任何困难,但我也很喜欢code与G ++和ICPC工作。我想补充一点:
I have a small program that I can compile with GCC and ICC without any difficulties, but I would also like the code to work with G++ and ICPC. I tried to add this:
#ifdef __cplusplus
extern "C" {
#endif
在开始和这
#ifdef __cplusplus
}
#endif
在所有头文件的结尾,但是我还是多个`未定义的引用......'的错误。
at the end of all the header files, but I still get several `undefined reference to "..."' errors.
推荐答案
我觉得你得到它错了... ...的外部C是禁用功能识别码;所以,如果你仅仅是为了头文件做到这一点,当你尝试链接你的错位对象code,声明的函数名称不匹配的目标文件中的函数名。
I think you're getting it wrong... The extern C is for disabling the function mangling; so if you do it just for the header files, when you try to link your mangled object code, the declared function names won't match with the function names in the object file.
总之,外部C不会,如果整个应用程序被编译,并与相同的C ++编译器链接添加任何便携性,它适用于混合C库C ++ code。
Anyway, the extern C won't add any portability if the whole application is being compiled and linked with the same C++ compiler, it's intended for mixing C libraries with C++ code.
如果你的code是在C和C ++的公共子集,你应该已经能与任何编译器来编译它,但我看不到这样做(除了最不意外的原理工作的原因,像C ++更严格一些事情)。
If your code is in the common subset of C and C++, you should be already able to compile it with either compiler, but I cannot see the reason to do that (besides working on the principle of least surprise, as C++ is more strict with some things).
这篇关于C code。与C ++编译:未定义参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!