简体   繁体   English

如何使用编译的C代码编译和链接C ++代码?

[英]How do I compile and link C++ code with compiled C code?

I want to be able to use Cmockery to mock C functions called from C++ code I'm testing. 我希望能够使用Cmockery来模拟从我正在测试的C ++代码调用的C函数。 As a step towards that, I've renamed the Cmockery example run_tests.c to run_tests.cpp, and am attempting to compile and link it with cmockery.c: 作为向前迈出的一步,我将Cmockery示例run_tests.c重命名为run_tests.cpp,并尝试使用cmockery.c进行编译和链接:

g++ -m32 -DHAVE_CONFIG_H -DPIC -I ../cmockery-0.1.2 -I /usr/include/malloc -c run_tests.cpp -o obj/run_tests.o
gcc -m32 -DHAVE_CONFIG_H -DPIC -Wno-format -I ../cmockery-0.1.2 -I /usr/include/malloc -c ../cmockery-0.1.2/cmockery.c -o obj/cmockery.o
g++  -m32 -o run_tests obj/run_tests.o obj/cmockery.o

The first two command lines (to compile) are successful, but after the last I get: 前两个命令行(编译)是成功的,但在最后我得到:

Undefined symbols:
  "_run_tests(UnitTest const*, unsigned long)", referenced from:
      _main in run_tests.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

That undefined symbol is from line 29 of run_tests.cpp: 该未定义的符号来自run_tests.cpp的第29行:

return run_tests(tests);

The run_tests() function is defined in cmockery.c. run_tests()函数在cmockery.c中定义。

After reading " Linking C++ code with 'gcc' (without g++) ", I tried: 在阅读“ 使用'gcc'(没有g ++)链接C ++代码 ”之后,我尝试了:

gcc -lstdc++ -m32 -o run_tests obj/run_tests.o obj/cmockery.o

But got the same result: 但得到了相同的结果:

Undefined symbols:
  "_run_tests(UnitTest const*, unsigned long)", referenced from:
      _main in run_tests.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

How do I compile and link C++ code so it finds the symbols in C code? 如何编译和链接C ++代码,以便在C代码中找到符号?

I think that you can get thinkgs to link from C++ by adding the following around the contents of the cmockery.h file: 我认为你可以通过在cmockery.h文件的内容周围添加以下内容来使thinkgs从C ++链接:

At or near the beginning: 在开头或附近:

#if defined(__cplusplus)
extern "C" {
#endif

At or near the end: 在结尾处或附近:

#if defined(__cplusplus)
}
#endif

That way, use of the header in C sources will ignore the extern "C" part of the declaration, but when the header is include in C++ builds, the compiler will be properly told that the linkage for the declarations in that header use C semantics. 这样,在C源代码中使用头文件将忽略声明的extern "C"部分,但是当在C ++构建中包含头部时,编译器将被正确地告知该头部中的声明的链接使用C语义。

For a quick-n-dirty test or if you'd rather not modify the header, you can try: 对于快速肮脏的测试,或者如果您不想修改标题,可以尝试:

extern "C" {
#include "cmockery.h"
}

but my preference would be to put the extern "C" block in the header (and only around the stuff that's required - that might need a bit of analysis). 但我的偏好是将extern "C"块放在标题中(并且只在需要的东西周围 - 这可能需要一些分析)。

在C ++代码包含的头文件中,需要对用C编译的所有函数进行extern "C"声明。

when you include the C header files from C++, have you wrapped the prototypes with extern "C" { .... } ? 当你从C ++中包含C头文件时,你是否用extern "C" { .... }包装了原型? If you don't the C++ function name wil be 'mangled' at link time. 如果不这样做,C ++函数名称将在链接时被“损坏”。

As Karl said, extern "C" { .. } is needed. 正如卡尔所说,需要extern "C" { .. }

The reason: C++ mangles the names (adds funny characters) so that linking is type safe. 原因:C ++破坏名称(添加有趣的字符),以便链接是类型安全的。 C doesn't, so in that language linking foo(int) to foo(double) is possible (but wrong and embarrassing). C没有,因此在语言中将foo(int)链接到foo(double)是可能的(但是错误和令人尴尬)。

For successful interoperability, you need to tell the C++ compiler that some function names are not to be mangled, in order for linking to succeed. 为了成功实现互操作性,您需要告诉C ++编译器一些函数名称不会被破坏,以便链接成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Objective-C项目中的Xcode中编译+链接C ++代码? - How do I compile + link C++ code in Xcode in an Objective-C project? 如何为使用 Clang LLVM 编译的 C++ 代码生成图形代码配置文件报告? - How do I produce a graphical code profile report for C++ code compiled with Clang LLVM? 如何在Visual Studio 2010中查看C ++函数的已编译机器代码或字节代码? - How do I view the compiled machine code or byte code for a C++ function in Visual Studio 2010? 编译python代码并将其链接到C ++程序? - Compile python code and link it to C++ program? 如何使用 CMake 编译并链接 Go 代码到 c++ - How to compile and link Go code into c++ with CMake 如何防止使用C ++或本机编译代码进行I / O访问 - How to Prevent I/O Access in C++ or Native Compiled Code 如何在 VS - Code (Windows) 中同时编译和运行我的 C++ 代码 - How do I compile and run my c++ code at the same time in VS - Code (Windows) 你如何在 alpine linux 上运行和编译 C++ 代码? - How do you run and compile C++ code on alpine linux? 如何将此代码转换为C ++? - How do I convert this code to c++? 如何手动编译使用C ++的Cython代码? - How can I manually compile Cython code that uses C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM