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

发布于 2024-10-10 11:56:50 字数 1495 浏览 2 评论 0原文

我希望能够使用 Cmockery 来模拟从 C++ 代码调用的 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

前两个命令行(编译)成功,但在最后一个之后我得到:

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

未定义的符号来自 run_tests.cpp 第 29 行:

return run_tests(tests);

run_tests() 函数在 cmockery.c 中定义。

阅读“将 C++ 代码与“gcc”(不含 g++)链接之后”,我尝试过:

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

但得到了相同的结果:

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

如何编译和链接 C++ 代码以便它找到 C 代码中的符号?

I want to be able to use Cmockery to mock C functions called from C++ code I'm testing. 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:

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:

return run_tests(tests);

The run_tests() function is defined in cmockery.c.

After reading "Linking C++ code with 'gcc' (without g++)", I tried:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寂寞笑我太脆弱 2024-10-17 11:56:50

我认为您可以通过在 cmockery.h 文件的内容周围添加以下内容来获得从 C++ 链接的想​​法:

在开头或附近:

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

在结尾处或附近:

#if defined(__cplusplus)
}
#endif

这样,在 C 源代码中使用标头将忽略extern "C" 声明的一部分,但是当标头包含在 C++ 构建中时,编译器将被正确告知该标头中声明的链接使用 C 语义。

对于快速的脏测试或者如果您不想修改标头,您可以尝试:

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

但我的偏好是将 extern "C" 块放在标头中(并且仅在所需的东西 - 可能需要一些分析)。

I think that you can get thinkgs to link from C++ by adding the following around the contents of the cmockery.h file:

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.

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).

扛刀软妹 2024-10-17 11:56:50

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

In your header files included by your C++ code, you need extern "C" declarations for all the functions that are compiled in C.

幼儿园老大 2024-10-17 11:56:50

当您包含 C++ 中的 C 头文件时,您是否用 extern "C" { .... } 包装了原型?如果不这样做,C++ 函数名称将在链接时被“破坏”。

when you include the C header files from C++, have you wrapped the prototypes with extern "C" { .... }? If you don't the C++ function name wil be 'mangled' at link time.

征棹 2024-10-17 11:56:50

正如 Karl 所说,需要 extern "C" { .. }

原因是:C++ 破坏了名称(添加有趣的字符),以便链接是类型安全的。 C 没有,因此在该语言中将 foo(int) 链接到 foo(double) 是可能的(但错误且令人尴尬)。

为了成功的互操作性,您需要告诉 C++ 编译器某些函数名称不要被破坏,以便链接成功。

As Karl said, extern "C" { .. } is needed.

The reason: C++ mangles the names (adds funny characters) so that linking is type safe. C doesn't, so in that language linking foo(int) to foo(double) is possible (but wrong and embarrassing).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文