如何编译和链接C ++代码与编译的C代码? [英] How do I compile and link C++ code with compiled C code?

查看:196
本文介绍了如何编译和链接C ++代码与编译的C代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用 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

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

 未定义的符号:
_run_tests(UnitTest引用自:
_main in run_tests.o
ld:符号(s)未找到
collect2:ld返回1退出状态

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

  return run_tests(tests); 

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



阅读将C ++代码与'gcc'链接(无

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

但是得到了相同的结果:

 未定义的符号:
_run_tests(UnitTest const *,unsigned long),引用来自:
run_tests.o中的_main
ld:symbol (s)not found
collect2:ld返回1退出状态

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

我认为你可以通过添加thinkg来连接C ++以下是围绕cmockery.h文件的内容:



开始或接近开始:

  #if defined(__ cplusplu s)
externC{
#endif

在或接近end:

  #if defined(__ cplusplus)
}
#endif


$ b

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



对于快速n-dirty测试或者如果你不想修改标题,你可以试试:

  extern C{
#includecmockery.h
}

但是我的首选项是在标题中放置 externC块(并且只包含需要的东西 - 可能需要一些分析)。


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?

解决方案

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

这篇关于如何编译和链接C ++代码与编译的C代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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