首页 > 贝壳 > 杯具带来的教训-关于C和C++代码的混合链接

杯具带来的教训-关于C和C++代码的混合链接

2012年8月29日 发表评论 阅读评论

====我是分割线,只是想说明下面的都是废话,大家可以直接跳到下一个分割线====

这几天需要把Parsec(普林斯顿出品的一组多程序程序benchmark,C++编写,貌似是鼎鼎有名的李凯教授带的组出的)的一些程序移植到某模拟器上,由于模拟器没有OS,很多系统调用都是用硬件模拟。因此,parsec的pthread库需要替换成硬件提供的系统调用。在该模拟器上,之前已经有用汇编代码写好的系统调用库以及编程成了相应的静态链接库 .a文件。因此,理论上,只需要换掉pthread头文件,换成.a对应的头文件,并把对应的pthread函数换成硬件模拟的系统调用函数即可。

花了好大力气把所有的头文件以及pthread函数换掉,并且把Makefile的编译器换成交叉编译器,LDFLAGS里的多线程库换成了模拟器系统调用的.a库(通过 -Ldir -llib 替换,其中dir是.a文件所在目录,lib是.a文件的文件名去掉前缀lib和后缀.a的剩余部分)。一切就绪,敲下make,看着一个个.cpp变成了.o,我心里暗喜。可是,当所有.cpp变成.o后,杯具出现了,屏幕刷出一堆“undefined reference to xxxx”,其中xxx就是模拟器系统调用函数名。

奇了怪了,怎么会找不到reference呢,我第一感觉是LDFLAGS写错了,可是检查了几遍,发现没错。并且用同样的LDFLAGS链接splash2中的FFT程序都可以。因此,不太可能试着问题,并且为了确认,我加了-v选项,把链接的细节打出来了,发现没有任何问题。

排除了一种可能,但是问题并没有解决,我只好放狗搜。搜到的答案都是说由于库的链接顺序,会导致这问题,并且通假如某参数,链接时进行repeat搜索,可以避免该问题(当然,repeat的代码就是降低链接速度)。于是,我遍历了各种影响链接顺序的可能,包括LDFLAGS本身的顺序,以及源代码中包含头文件的顺序,可是,杯具依然。

两天过去了。。。还是一无所获,期间只好跑去机房调另外一个东西。昨天,在走投无路的情况下,狠下决心,用c语言重写parsec的这个benchmark。但是,决心容易下,真写谈何容易,看了半天,才把算法大致看明白。再看代码的过程中,我突然想,要不直接把模拟器的系统调用的汇编码嵌入到源代码中,不用.a形式静态链接,但是,由于这些汇编码的寄存器都是按mips标准命名用的,直接放进去,可能交叉编译器无法识别。于是,只好作罢,这时,突然想,为什么FFT可以,这个不可以,莫非是因为C++和C语言链接的方式不一样。想到这,突然有种豁然开朗的感觉,因为模模糊糊记得C函数的符号表和C++函数的符号表是不一致的。

====杯具终于完了,废话也终于完了,下面进入正题,貌似有点头重脚轻的感觉====

在c语言中,全局函数foo(int)在符号表中是_foo,而c++中,为了支持函数重载,会编译成类似_foo_int的。这就导致了前述xxx的系统调用会编译成_xxx_yyy(yyy是参数类型)的形式存在.o中。难怪链接器会找不到reference。

因此,下一步就是如何让C++代码能链接到c代码中了。于是继续放狗搜“c c++ link”,搜到了伟大的太阳的这篇文章:

Mixing C and C++ Code in the Same Program

这篇文章说道,其实可以通过extern关键词声明函数是c函数还是c++函数。当然,也可以直接extern某个头文件,将整个头文件里的函数都声明是啥函数。比如,下面代码:

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

就降sb.h 里面的函数都声明是c函数。这样,在c ++代码中,碰到sb.h中的函数,就会当成c函数来对待。

对于我碰到的问题,可以前述extern 头文件来解决。但是,对于大部分头文件来说,我们并不知道这个头文件里的函数是c函数还是c++函数。这就要求头文件编写者做些工作,一般来说,现在的系统库里面都能看到下面的语句:

#ifdef __cplusplus
extern "C"{
#endif

//some function declaration 

#ifdef __cplusplus
             }
#endif

上面的代码就是为了让库兼容c和c++.因为使用g++编译器的时候,实际上是带了__cplusplus的声明,因此整个头文件的函数都会被额外的声明是c函数。之后g++会特殊处理,从而保证链接不出错。

最后,再说几句废话,博客荒废已久,本来以为八月必然交白卷的,想不到最后还是憋出来一篇。。。



本文作者: Sigma    在新浪微博关注SigmaSigmaWeibo    RSS订阅本博客
本文链接: http://www.sigma.me/2012/08/29/c_vs_cpp_symbol_and_link.html
本博客采用知识共享署名—非商业性-禁止演绎使用3.0协议进行许可,转载请保留作者和原文链接。

分类: 贝壳 标签: , ,

  1. 2019年1月9日18:55 | #1

    I intended to send you that little bit of observation in order to say thanks a lot over again for your pretty guidelines you’ve documented above. This has been unbelievably open-handed of people like you to provide openly what some people might have sold as an ebook to make some dough for themselves, especially seeing that you could have done it if you wanted. Those good ideas as well acted as a easy way to be certain that many people have a similar fervor really like my very own to understand somewhat more around this condition. I’m sure there are many more enjoyable instances in the future for people who read through your site.

  2. 2019年1月10日14:20 | #2

    Thanks so much for providing individuals with remarkably marvellous possiblity to read in detail from this web site. It is often so pleasing and also full of fun for me and my office acquaintances to search your blog really thrice weekly to learn the newest items you have. And lastly, I’m just certainly fulfilled for the dazzling ideas served by you. Certain 4 ideas in this posting are without a doubt the finest we’ve had.

  3. 2019年1月11日09:21 | #3

    I just wanted to post a word to say thanks to you for some of the awesome solutions you are posting at this site. My extended internet look up has finally been honored with excellent information to exchange with my co-workers. I ‘d admit that many of us website visitors are very endowed to be in a really good place with many outstanding people with helpful plans. I feel very much privileged to have encountered your entire weblog and look forward to really more fun moments reading here. Thanks once more for all the details.

  4. 2019年1月12日05:14 | #4

    I really wanted to type a word in order to thank you for the fabulous secrets you are giving on this website. My prolonged internet lookup has at the end been paid with reliable know-how to talk about with my family. I ‘d admit that most of us site visitors actually are unequivocally endowed to be in a useful network with very many marvellous professionals with valuable methods. I feel really grateful to have seen the website and look forward to tons of more excellent times reading here. Thanks again for a lot of things.

  5. 2019年1月12日10:56 | #5

    I wish to show some appreciation to you just for rescuing me from such a situation. As a result of looking through the search engines and coming across solutions which were not beneficial, I was thinking my entire life was done. Being alive without the solutions to the difficulties you’ve resolved by means of your report is a critical case, and the kind which might have negatively damaged my entire career if I hadn’t noticed your web site. The skills and kindness in controlling all areas was vital. I don’t know what I would have done if I had not come upon such a point like this. I’m able to at this moment look forward to my future. Thanks a lot very much for this high quality and result oriented guide. I will not be reluctant to propose your site to any individual who requires guidelines about this subject matter.

  6. 2019年1月13日01:15 | #6

    My spouse and i have been so fortunate Albert could complete his homework by way of the precious recommendations he gained from your own web pages. It is now and again perplexing to simply always be making a gift of steps which usually some others may have been making money from. So we fully understand we have got you to give thanks to because of that. All of the illustrations you’ve made, the easy blog menu, the friendships you help promote – it’s got everything extraordinary, and it’s leading our son and the family reckon that that subject matter is interesting, which is certainly seriously serious. Thanks for all!

  7. 2019年1月13日18:35 | #7

    I wanted to write you a very little remark to give thanks again over the precious tricks you have contributed on this website. It has been open-handed of you to grant unhampered what a lot of people could have advertised as an ebook to generate some cash for themselves, notably considering that you could possibly have tried it if you ever considered necessary. These smart ideas also worked to become good way to comprehend many people have a similar interest just as my personal own to realize significantly more in respect of this matter. I am sure there are some more fun sessions up front for people who read carefully your website.

  8. 2019年1月14日11:56 | #8

    I have to voice my affection for your kindness in support of persons who have the need for guidance on this important subject. Your very own commitment to getting the message up and down was amazingly significant and has always encouraged men and women much like me to reach their aims. Your amazing useful help signifies a lot to me and still more to my office colleagues. With thanks; from each one of us.

  9. 2019年1月14日19:34 | #9

    I am just writing to make you understand what a magnificent experience my cousin’s girl undergone reading through your web page. She came to understand many things, not to mention how it is like to have an awesome helping character to get the rest without difficulty comprehend specified problematic things. You actually did more than readers’ expected results. Thanks for presenting these important, safe, informative and as well as easy tips on your topic to Mary.

  10. 2019年1月14日22:49 | #10

    I want to express some appreciation to you for rescuing me from this type of circumstance. Just after exploring throughout the the web and seeing views that were not productive, I was thinking my life was gone. Living devoid of the answers to the problems you’ve sorted out by way of this short post is a crucial case, as well as the kind that might have in a wrong way affected my career if I hadn’t discovered your blog. That knowledge and kindness in taking care of the whole lot was invaluable. I’m not sure what I would have done if I had not come upon such a point like this. I am able to now relish my future. Thanks very much for this high quality and effective guide. I will not hesitate to recommend your site to anybody who should receive guidance on this subject.

  11. 2019年1月15日07:17 | #11

    Thank you for all of the hard work on this web site. Gloria takes pleasure in going through internet research and it is easy to understand why. We all hear all relating to the lively form you make helpful thoughts on this web blog and as well as attract participation from other people on the issue then our girl is understanding a whole lot. Have fun with the remaining portion of the new year. You have been carrying out a really good job.

  12. 2019年1月15日15:49 | #12

    I would like to show some thanks to the writer just for bailing me out of such a crisis. Right after checking throughout the online world and coming across opinions which are not helpful, I believed my life was done. Existing devoid of the solutions to the problems you have fixed as a result of the guide is a crucial case, as well as the kind that would have badly damaged my entire career if I hadn’t encountered the blog. Your personal knowledge and kindness in dealing with every part was helpful. I’m not sure what I would have done if I had not discovered such a thing like this. I can now look forward to my future. Thanks a lot so much for your high quality and results-oriented guide. I will not think twice to propose the sites to any individual who needs and wants guidelines on this area.

  13. 2019年1月16日01:45 | #13

    I want to show some appreciation to the writer just for rescuing me from this particular matter. As a result of researching through the world-wide-web and obtaining things which were not pleasant, I believed my life was over. Being alive minus the strategies to the issues you have resolved as a result of the post is a serious case, and the kind which may have negatively damaged my entire career if I had not noticed your blog post. Your own personal ability and kindness in playing with almost everything was crucial. I am not sure what I would have done if I hadn’t come across such a point like this. I can also at this point relish my future. Thanks a lot so much for your high quality and sensible help. I won’t hesitate to refer your web page to any individual who needs direction about this situation.

  14. 2019年1月16日09:11 | #14

    I together with my friends ended up reviewing the great tips and hints located on your web blog and so then I had a terrible feeling I had not expressed respect to the web site owner for those tips. Most of the ladies were certainly passionate to see all of them and have undoubtedly been using them. Thanks for simply being really kind and for utilizing some extraordinary subject matter most people are really needing to learn about. Our sincere apologies for not expressing gratitude to sooner.

  15. 2019年1月17日12:26 | #26

    I really wanted to write a simple word to appreciate you for some of the lovely ideas you are sharing here. My incredibly long internet look up has at the end of the day been recognized with pleasant facts and techniques to exchange with my partners. I would assert that we readers actually are undoubtedly endowed to exist in a very good network with so many outstanding professionals with useful secrets. I feel very much fortunate to have encountered the web pages and look forward to so many more brilliant minutes reading here. Thanks a lot once more for a lot of things.

  16. 2019年1月17日14:31 | #27

    I have to get across my passion for your kind-heartedness supporting individuals that really need assistance with your topic. Your very own dedication to passing the solution throughout turned out to be amazingly productive and has really empowered regular people just like me to get to their endeavors. This useful suggestions means a whole lot to me and extremely more to my office colleagues. Many thanks; from all of us.

  17. 2019年1月17日21:59 | #28

    I precisely wished to appreciate you all over again. I’m not certain what I would’ve carried out without the thoughts shared by you on such topic. Entirely was a frightful circumstance in my view, nevertheless seeing a new specialised approach you treated it took me to weep with contentment. I am just happy for your advice and trust you really know what an amazing job you are always putting in instructing people today with the aid of your web blog. Probably you’ve never met any of us.

  18. 2019年1月18日06:36 | #29

    Thanks for every one of your efforts on this web page. My aunt delights in doing investigation and it’s simple to grasp why. Many of us know all about the dynamic manner you offer reliable tips and tricks through your web blog and as well increase response from other individuals on that concept so our favorite daughter is starting to learn a great deal. Have fun with the remaining portion of the year. You are always performing a really good job.

  19. 2019年1月18日13:55 | #30

    My wife and i ended up being comfortable when Emmanuel managed to deal with his web research via the precious recommendations he got out of your web pages. It’s not at all simplistic to simply find yourself giving away information and facts that many many others may have been trying to sell. And we all realize we have got the blog owner to be grateful to because of that. The entire illustrations you made, the straightforward website navigation, the friendships you can make it possible to foster – it’s got all great, and it’s making our son in addition to the family understand this matter is fun, which is incredibly mandatory. Many thanks for all the pieces!

  20. 2019年1月18日23:08 | #31

    I wanted to construct a simple word to be able to express gratitude to you for all the pleasant suggestions you are showing at this site. My extensive internet look up has now been rewarded with brilliant strategies to share with my visitors. I would assert that most of us website visitors are unequivocally endowed to exist in a decent website with very many special individuals with helpful tactics. I feel very grateful to have used the web pages and look forward to really more entertaining minutes reading here. Thanks once more for all the details.

  21. 2019年1月19日05:27 | #32

    I and also my friends have been digesting the great pointers on your site while unexpectedly got an awful suspicion I never expressed respect to the website owner for those techniques. All of the men are actually for this reason warmed to read through them and have in truth been taking pleasure in those things. Thank you for being very helpful and also for choosing variety of useful topics most people are really eager to learn about. My very own honest regret for not expressing appreciation to you earlier.

  22. 2019年1月19日14:14 | #33

    I together with my guys were found to be examining the best suggestions found on your web page while immediately developed a terrible feeling I never expressed respect to the web blog owner for them. All the women had been for that reason thrilled to learn all of them and have pretty much been loving these things. Thank you for getting simply kind and also for finding this sort of great information millions of individuals are really eager to be informed on. Our honest regret for not expressing gratitude to earlier.

  23. 2019年1月20日00:29 | #34

    Needed to create you a tiny note to finally thank you very much the moment again for the fantastic advice you have provided on this website. This is quite wonderfully generous of you in giving easily precisely what numerous people might have offered for an e-book in order to make some dough on their own, certainly considering the fact that you might have tried it in case you desired. The secrets additionally served to be a great way to recognize that the rest have the identical desire just like my personal own to see very much more on the subject of this issue. I am sure there are lots of more enjoyable opportunities up front for individuals that read carefully your blog.

  24. 2019年1月20日07:26 | #35

    I would like to show thanks to the writer for rescuing me from such a matter. Just after scouting throughout the online world and getting basics which are not beneficial, I believed my life was over. Living devoid of the strategies to the problems you have resolved by means of this write-up is a crucial case, as well as the kind that would have in a wrong way damaged my entire career if I hadn’t noticed the website. The natural talent and kindness in handling the whole lot was precious. I am not sure what I would have done if I hadn’t come upon such a stuff like this. It’s possible to at this moment relish my future. Thanks so much for the impressive and result oriented help. I won’t hesitate to refer your site to any person who would like guidance about this subject matter.

  25. 2019年1月20日08:56 | #36

    My spouse and i felt really fulfilled when Edward could finish up his basic research by way of the ideas he had from your weblog. It’s not at all simplistic just to continually be handing out tips the rest have been making money from. And we also understand we have you to appreciate for this. These illustrations you have made, the straightforward site menu, the friendships you will help to instill – it’s got everything impressive, and it’s letting our son in addition to us understand this situation is cool, and that is tremendously mandatory. Many thanks for all!

  26. 2019年1月20日15:55 | #37

    Thanks for all of your hard work on this web site. My mum enjoys participating in investigations and it’s easy to see why. Many of us hear all regarding the compelling method you render functional tips by means of your web site and in addition inspire contribution from website visitors on the situation and my child is always becoming educated a whole lot. Enjoy the rest of the new year. Your conducting a terrific job.

  27. 2019年1月21日08:12 | #38

    I am just writing to let you be aware of what a impressive experience my friend’s daughter went through visiting your site. She mastered a wide variety of issues, not to mention what it’s like to have a very effective giving character to make other folks easily gain knowledge of various impossible topics. You really exceeded readers’ expectations. Thank you for producing those practical, safe, informative and in addition easy tips on your topic to Ethel.

  28. 2019年1月22日01:36 | #39

    I wanted to write down a simple note to say thanks to you for all the lovely points you are showing on this website. My time-consuming internet look up has now been paid with really good facts to exchange with my two friends. I ‘d admit that many of us site visitors actually are truly lucky to be in a remarkable website with very many special individuals with very helpful principles. I feel extremely fortunate to have encountered your web pages and look forward to some more fun minutes reading here. Thanks a lot once again for everything.

  29. 2019年1月22日17:54 | #40

    I and also my buddies have been studying the good helpful tips found on the website then instantly developed a terrible suspicion I never expressed respect to the blog owner for those tips. My guys ended up totally joyful to see them and now have certainly been loving these things. Many thanks for being considerably considerate as well as for pick out such perfect subjects most people are really desirous to be informed on. My honest apologies for not saying thanks to sooner.

  1. 本文目前尚无任何 trackbacks 和 pingbacks.

无觅相关文章插件,快速提升流量