Microchip
直播中

李丽

7年用户 304经验值
私信 关注
[问答]

CPP和C共存的诀窍是什么?

CPP和C共存的诀窍是什么?我有一个使用数学函数的单独的CPP库。主C程序使用CPP LIB。对于主项目的标准编译失败了许多对CPP数学函数的未定义的引用,例如,如果我添加了LIXXCPP。CPP文库是否有一种方法既可以包含CPP LIB,也可以直接从CPP LIB中使用,并且只使用主LIB?使用XC32 V1.44 PIC32 MX2064 DAH176

以上来自于百度翻译


      以下为原文

    What is the trick to make CPP and C coexist?

I have a separate CPP library that uses math functions.
The main C program uses the cpp lib.

A standard compile for the main project fails with many undefined references to cpp math functions, like `_Flog`

If I add libxcpp.a to the main project, it compiles and runs until it tries to use fputc in the cpp library.  Is there a way to include both, or direct the cpp lib to only use from the cpp lib, and the main to only use the main lib?

Using XC32 V1.44 PIC32MX2064DAH176

回帖(3)

宋宾

2019-1-22 15:06:21
为了得到CPP和C一起交谈,你必须知道C++的名字。C++NordMangle更改所有符号的名称,包括命名空间、类、参数等。C++编译器所涉及的每一个名称都将使其文本被篡改,以符合规则。即使纯C函数不在命名空间中,也不包含在类内。(HTTPS://E.WiKiTo.Org/WiKi/NaveSuffFigter以获得更多的细节)因此,用C编译器编译的库将有不同于C++的期望名称,并且您将获得许多未知符号。但是C++有一种方式告诉计算机某些函数声明是C样式而不是C++ STY。将要被C和C++文件使用的头文件通常有一对下面的模式的IF-IFDES:如果定义的话,(γ-CPLUS)外部的“C”{πNeNFF & lt;函数声明& gt;如果定义的话(α-cPLUS PLUS)},告诉C++编译器在这两个IF之间的所有函数声明定义是用C编译器编译的,而不是对名称进行篡改。一个C编译器将忽略那些在你的情况下我希望你编译的库中没有一个Y-CPLUSSPLUS锅炉PLAT,C++编译器正在修改名字。当链接器进入库时,它无法匹配名称。

以上来自于百度翻译


      以下为原文

    To get CPP and C to talk together you have to know about C++ name mangling. 
C++ name mangling changes the names of all symbols to include namespaces, classes, parameters, etc.  Every name the C++ compiler comes across will have its text mangled to match the mangling rules.  Even pure C functions that are not in a namespace or contained inside a class. ([url=https://en.wikipedia.org/wiki/Name_mangling]https://en.wikipedia.org/wiki/Name_mangling[/url] for more details)
So a library that is compiled with the C compiler will have different names than C++ expects, and you will get a lot of unknown symbols.
But C++ has a way to tell the computer that certain function declarations are C style and not C++ style.
 
Header files that are to be used by both C and C++ files usually have a pair of #ifdefs of the following pattern:
 
#if defined(__cplusplus)
extern "C" {
#endif
 

 
#if defined(__cplusplus)
}
#endif
 
That tells the C++ compiler that all function declarations between those two #if defines are compiled with a C compiler and to not mangle the names.  A C compiler will ignore those sections
 
In your case I would expect that the library you're compiling does not have the __cplusplus boiler plat and the C++ compiler is mangling the names.  And when the linker takes in the library, it can't match up the names.
举报

刘涛

2019-1-22 15:16:17
但是你是说C++的LIB调用应该调用C LIBS吗?我希望C++应该调用LIBXC++LIBS,和C标准LIBS。但是,它显然不链接到外部LIBS,直到它被添加到我的C项目中。

以上来自于百度翻译


      以下为原文

    But are you saying the C++ lib calls should be calling to the C libs?  I would expect that C++ should call the libxc++ libs, and C the standard libs.
 
The library compiles fine by itself. However, it obviously isn't linking to the external libs until it gets added to my C project.
举报

宋宾

2019-1-22 15:24:47
我不能同意你的具体申请。我可以说的是:C++库可以调用C库,但是C++支持重载。因此,它可以支持:int pOw(int,int);双PoW(double,double);在C编译的库中,应该用多次定义的符号来获得错误,链接器不知道该选择哪一个(或者它总是选择第一个定义,然后你会得到奇怪的行为)。在一个C++编译的库中,名字将被修改,C++编译器/链接器将知道根据参数使用哪一个。在大多数情况下,在嵌入式系统中,当你在混合C++C++时,你希望使用C库,除非你需要像上面提到的那样重载。这样一来,你的C代码和C++代码都将使用相同的符号,并且由于名称的篡改和使用Flash空间,你不会有重复的目标代码。(注:一些编译器/链接器将找到重复的对象代码,并将所有的东西指向一个副本,并删除其他副本。我不知道XC32是否这样做。从你所写的看来,你将需要C和C++版本的代码,因为该库使用C++数学函数。

以上来自于百度翻译


      以下为原文

    I can't say for your specific application.  What I can say is:
 
C++ libraries can call C libraries.
 
But C++ supports overloading.  So it can support for example:

  • int pow(int, int);
  • double pow(double, double);
In a C compiled library you should get errors with symbols defined multiple times, and the linker wouldn't know which to choose (or it'll always choose the first one defined and then you'll get strange behavior).  In a C++ compiled library the names will be mangled and the C++ compiler/linker will know which one to use based on the parameters.
 
In most cases on an embedded system when you're mixing C & C++ you want to use the C libraries unless you need something like overloading as I stated above.  This way both your C code and your C++ code will use the same symbol and you won't have object code duplicated because of the name mangling and using up flash space.
 
(One note: some compilers/linkers will find the duplicated object code and point everything to one copy and eliminate the others.  I don't know if XC32 does this).
 
From what you wrote it looks like you will need both the C and C++ versions of the code because that library uses the C++ math functions.
 
 
举报

更多回帖

发帖
×
20
完善资料,
赚取积分