完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
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个回答
|
|
为了得到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. |
|
|
|
但是你是说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. |
|
|
|
我不能同意你的具体申请。我可以说的是: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:
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. |
|
|
|
只有小组成员才能发言,加入小组>>
5248 浏览 9 评论
2036 浏览 8 评论
1956 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3218 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2264 浏览 5 评论
788浏览 1评论
678浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
608浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
685浏览 0评论
582浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-27 22:32 , Processed in 1.239349 second(s), Total 80, Slave 64 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号