conflicts with new declaration with 'C' linkage错误修改
2015-08-05 17:29
603 查看
在c++程序中调用C编写的库,源代码如下
一共有三个文件ms.h ms.c main.cpp 其中ms.h和ms.c编为一个动态库
/*ms.h 代码*/
21 typedef struct sip
22 {
23 int fd ;
24 }VSIP;
25
26 struct sip testlink[10];
27
28
29 int dprint(int pa);
/*ms.c代码*/
20 #include "stdio.h"
21 #include "stdlib.h"
22 #include "string.h"
23 #include "ms.h"
24
25 int dprint(int pa)
26 {
27 testlink[0].fd = pa;
28 printf("test int = %d\n",testlink[0].fd);
29 return 0;
30 }
31
编译为一个动态库:
gcc -shared -fPIC -o libtest.so ms.c
/*main.cpp*/
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include "ms.h"
21
22 extern "C"
23 {
24 int dprint(int pa);
25 extern struct sip testlink[];
26 }
27
28
29
30 int main()
31 {
32 int p = 100;
33 dprint(p);
34 return 0;
35 }
36
编译:
g++ -o main -ltest -L. main.cpp
报错如下:
ms.h:29: error: previous declaration of 'int dprint(int)' with 'C++' linkage
main.cpp:24: error: conflicts with new declaration with 'C' linkage
ms.h:26: error: previous declaration of 'sip testlink [10]' with 'C++' linkage
main.cpp:25: error: conflicts with new declaration with 'C' linkage
错误分析:
在main.cpp中如果已经包含了ms.h就不必再用extern “C”去声明了。
如果不包含ms.h则用extern “c”声明即可
【yasi】另外注意:
extern "C" {
...
}
extern "C" { ... }上面的一对大括号中,不要出现C++的语句,比如
#include <string>
using namespace std;
相关文章推荐
- android常用工具 --- 简单的FragmentAdapter
- 电脑显卡4种接口类型:VGA、DVI、HDMI、DP
- plsql
- JavaScript的深拷贝的实现
- leetcode算法思路总结
- 盒子模型
- java 24小时和12小时格式解析
- HDOJ 4707 Pet(并查集)
- Filter 入门
- java 反射机制读取属性值
- python log
- linux chmod命令和chown命令
- HDU 2612 Find a way bfs
- c#高级编程第七版 学习笔记 第三章 对象和类型
- iOS 开发 解决EXC_BAD_ACCESS错误的一种方法--NSZombieEnabled(转)
- 虚拟机的优化工具——VMware tools
- javascript实现多栏闭合展开式广告位菜单效果实例
- C++文件读写详解(ofstream,ifstream,fstream)
- Num 19: 最大公约数.最小公倍数.素数的判断
- dedecms后台管理员密码重置和修改