Chinaunix

标题: C调用C++函数出错。。 [打印本页]

作者: 曹朋Code    时间: 2012-09-11 15:15
标题: C调用C++函数出错。。
a.c是一个纯粹的C文件
/////////////////////////////////////
#include <b.h>
.........

if(fun()){ -------->报错
........
}
////////////////////////////////////
///////////////b.h 文件////////////
//b.h
#ifdef __cplusplus
extern "C" {
#endif
#include<....>
  bool fun();
..............
#ifdef __cplusplus
}
#endif
////////////////////////////////////
///////////b.cpp文件//////////////////
//b.cpp
#inlcude <....>
using namespace XXXX;
bool fun(){
  ClassA a; ----->这里用到了一些类,
  a.x.....
}
////////////////////////////////////


现在编译报错:
XXXXX/a.c error: undefined refernce to ‘fun’
求指导。。
作者: folklore    时间: 2012-09-11 15:43

  1. ///////////b.cpp文件//////////////////
  2. //b.cpp
  3. #inlcude <....>
  4. using namespace XXXX;
  5. bool fun(){
  6.    ClassA a; ----->这里用到了一些类,
  7.    a.x.....
  8. }
  9. ////////////////////////////////////

复制代码
extern "C" bool fun(){
作者: sonicling    时间: 2012-09-11 15:44
.cpp 的 bool fun(){ .. } 函数实现也要用extern "C" 修饰,或者用 extern "C" { }括起来。
作者: 曹朋Code    时间: 2012-09-11 15:50
回复 2# folklore


    不行。。。
作者: 曹朋Code    时间: 2012-09-11 15:52
回复 3# sonicling


    不行。。
作者: justmao945    时间: 2012-09-11 16:30
没一起链接?
作者: edgar51774    时间: 2012-09-11 17:27
folklore 发表于 2012-09-11 15:43
extern "C" bool fun(){

...............




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2