Fork me on GitHub

C++调用C方法

//1,编译静态库 libtest.a
gcc -c test.c -o test.o
ar rc libtest.a test.o

//2,编译main函数
g++ -o main main.cpp  -I./test -L./test -static -ltest 

test相关文件放在了当前的test目录下

[root@ ~/learn_code/C++_learn]$ tree
.
├── main
├── main.cpp
├── test
│   ├── libtest.a
│   ├── makefile
│   ├── test.c
│   ├── test.h
│   ├── test.i
│   ├── test.o
│   └── test.s
├── test.c
└── test.h

[root@ ~/learn_code/C++_learn/test]$ make libtest.a -B
gcc -c test.c -o test.o
ar rc libtest.a test.o

 

1,可行
 

 

2,更可以,推荐用这种方式
 

 

 
对于比较老的C库,可能当时写的没有考虑到声明extern
 
3,可行,推荐使用
 

 

4,下面这种方式报错

 

 解释下原因:根本原因是因为编译器在编译C++和C文件中的函数时,是区别对待的,也就是说同一个函数名,在C++和C文件中编译出来的名字不一样。

 
比如说对于mytest函数,对于C编译后为_mytest,对于C++编译之后名字为_mytest_。下面解析下上面的几种情况
对于第1中情况:
     C++文件include了test.h, 展开了其中的__cplusplus宏,所以对应  extern "C" void mytest(),编译后为:_mytest
     C语法是不支持extern "C"的,当然这里也不会展开__cplusplus宏,所以对应  void mytest(),编译后为:_mytest
 
对于第2中情况:
     更不用说了,同第1中情况
 
对于第3中情况:
     对于以前写的C库,可能大多数时候都是这种写法
     这个时候就得在包含它的C++文件中,必须显式的声明 extern "C"关键字。
 
对于第4种情况:
     C++文件include了test.h,对应  void mytest(),编译后为:_mytest_ (注意与第一种情况的区别)
     C语法是不支持extern "C"的,当然这里也不会展开__cplusplus宏,所以对应  void mytest(),编译后为:_mytest
     所以这里在生成main的可执行文件的时候会链接错误。     
1 [root@ ~/learn_code/C++_learn]$ g++ -o main main.cpp  -I./test -L./test -static -ltest
2 /tmp/cc3TGBKW.o: In function `main':
3 main.cpp:(.text+0x5): undefined reference to `mytest()'
4 collect2: ld returned 1 exit status

 

结论:

     1,在写C函数库的时候,尽量带上宏__cplusplus这一段声明,方便后面使用
     2,在写C++程序的时候,加入会调用C库,尽量带上宏__cplusplus这一段声明,肯定就不会报相关编译的错误了。
 

 

posted @ 2017-05-12 21:27  码农的逼格  阅读(3359)  评论(0编辑  收藏  举报