首页 技术 正文
技术 2022年11月7日
0 收藏 467 点赞 1,023 浏览 1332 个字

C++调用C方法C++调用C方法C++调用C方法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,可行C++调用C方法 C++调用C方法

2,更可以,推荐用这种方式C++调用C方法 C++调用C方法

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

4,下面这种方式报错C++调用C方法

C++调用C方法

解释下原因:根本原因是因为编译器在编译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的可执行文件的时候会链接错误。     

 [root@ ~/learn_code/C++_learn]$ g++ -o main main.cpp  -I./test -L./test -static -ltest
/tmp/cc3TGBKW.o: In function `main':
main.cpp:(.text+0x5): undefined reference to `mytest()'
collect2: ld returned exit status

结论:

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289