conflicts with new declaration with 'C' linkage错误修改

转自:http://blog.163.com/dengjingniurou@126/blog/static/53989196200973125219682/


在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" {
...
}
上面的一对大括号中,不要出现C++的语句,比如

#include <string>
using namespace std;

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值