C / C ++ compiler mixed programming problems

Scenario 1: C program which calls the C ++ class interfaces

Makefile as follows:

If you use g ++ to compile it, no problem is too direct. If you attempt to compile with gcc, then an error

You can see a link error, due to the attempt to gcc main.c Functions and libpal_queue Error link in the link stage, because libpal_queue is pal_queue.cpp

Ed out, gcc met .cpp file also will follow the C ++ way to deal with, so the series out of the function name is modified, but main.o link when it comes to silly manner in accordance with c

Find function, so wrong. And then use g ++ and C can handle C ++, thus the link when there will not be any problems.

So the conclusion is, C calling C ++ (main.c call interface in a class in C ++), the use g ++ to compile links. (In fact, "the compilation stage, g ++ will invoke gcc, for c ++ code and the two are equivalent, but because gcc command can not the library coupling automatic and C ++ programs use, it is usually with g ++ to complete the link, for the sake of consistency, altogether compile / link all with g ++ , and this gives a false impression, as if the program can only be used g ++ cpp -like " )

g ++ will automatically connect C ++ standard library; connection with gcc C ++ programs can be, but people need to specify connection C ++ standard library, otherwise there will be undefined reference to `__gxx_personality_v / 0 'error and the like, gcc compiler c ++ program you need to add -lstdc ++ sample: gcc -lstdc ++ -o test test.cpp

Scenario 2: C ++ call C (UNITTEST.cpp the call interface of a C)

2.1 g ++ to compile (build links running OK)

2.2 Use gcc to compile, compile OK, link errors, for the same reason.

Details of the CPP said that in accordance with hello_int_char * hello to link _hello sign in, which is not on the problem yet!

3. 针对上面的情况,该怎么用extern "C" {}去解决这个问题呢?

说道这个,就不得不说说__cplusplus宏,“实际上,这个宏只是标志着编译器将会把代码按C还是C++语法来解释,如上所述,如果后缀为.c,并且采用gcc编译 器,则该宏就是未定义的,否则,就是已定义”。也就是说,如果我们使用g++,或者文件后缀是.cpp,那么这个宏都是被定义的。

=====================================================================================

4. 好吧,有点尴尬

我发现用g++去编,根本不受extern "C"的影响,有没有它都能正常编译通过并运行。

这也是个问题,说好的符号链接不上呢?

而用gcc去编,有没有extern "C"都不对

 

加上extern "C"后,好像好了一点,起码对hello的引用是对的了

但是剩下的两个问题怎么也搞不定了,有的人说用gcc -stdc++可以解决,我试了,没有用。

好吧,问题先放在这,后面打算一步步地去跟,看汇编符号,看链接过程到底是什么样的

如果有知道原因的朋友欢迎在下面留言,不胜感激。

@@@@@@@@@@@@@@@@@@@@@@@@@@@@

TBD before 2019/8/1

 

Guess you like

Origin www.cnblogs.com/Arnold-Zhang/p/11241757.html