免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2333 | 回复: 8
打印 上一主题 下一主题

苦恼,C和CPP文件编译出来的OBJ是不一样的? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-11-07 15:23 |只看该作者 |倒序浏览
用c程序死活链接不上去,cp成cpp文件就好使了,我观察到两个.o是不一样D,但是不知道个中问题。达人告诉我为什么啊~~


系统环境:AIX4.2.3

//test.cpp
#include "classA.h"
int main()
{
  classA a;
  a.fun1();
  return 0;
}

//classA.h
class classA
{
public:
  classA(){};
  ~classA(){};

  int fun1(){return 0;};
  int fun2();
}

//classA.cpp
#include "classA.h"
#include "mytool.h"
int classA::fun2()
{
  return happy();// happy define in mytool.h
}

//mytool.h
int happy();

//mytool.c
#include "mytool.h"
int happy()
{return 0};

//makefile
CC= xlC

all: test

test : mytool.o classA.o test.o
        $(CC) test.o classA.o mytool.o -o test

test.o : test.cpp classA.h
        $(CC) -c test.cpp -o test.o

classA.o : classA.cpp classA.h
        $(CC) -c classA.cpp -o classA.o

mytool.o : mytool.c mytool.h
        $(CC) -c mytool.c -o mytool.o

$>;make
...
ld: 0711-317 ERROR: Undefined symbol: .happy()
...

论坛徽章:
0
2 [报告]
发表于 2003-11-07 18:01 |只看该作者

苦恼,C和CPP文件编译出来的OBJ是不一样的?

c里面有class???

论坛徽章:
0
3 [报告]
发表于 2003-11-10 09:07 |只看该作者

苦恼,C和CPP文件编译出来的OBJ是不一样的?

sorry 我是指mytool.c ->; mytool.cpp

论坛徽章:
0
4 [报告]
发表于 2003-11-10 10:32 |只看该作者

苦恼,C和CPP文件编译出来的OBJ是不一样的?

c++兼容c,但c不兼容c++,就是这样。

论坛徽章:
0
5 [报告]
发表于 2003-11-10 17:32 |只看该作者

苦恼,C和CPP文件编译出来的OBJ是不一样的?

不对吧,那也应该是mytool.c编译出来的.o可以被C++程序链接啊,现在刚好相反。

论坛徽章:
0
6 [报告]
发表于 2003-11-10 17:46 |只看该作者

苦恼,C和CPP文件编译出来的OBJ是不一样的?

肯定不一样。

论坛徽章:
0
7 [报告]
发表于 2003-11-10 18:45 |只看该作者

苦恼,C和CPP文件编译出来的OBJ是不一样的?

你应该这样用。
//mytool.h
#ifdef __cplusplus
extern "C" {
#endif

extern int happy();

#ifdef __cplusplus
}
#endif

//mytool.c
//#include "mytool.h"  /*这里注释掉 */
int happy()
{return 0};

论坛徽章:
0
8 [报告]
发表于 2003-11-11 11:01 |只看该作者

苦恼,C和CPP文件编译出来的OBJ是不一样的?

简单修改mytool.h声明部分
extern "C" int happy();

论坛徽章:
0
9 [报告]
发表于 2003-11-12 13:47 |只看该作者

苦恼,C和CPP文件编译出来的OBJ是不一样的?

是不一样,为了支持C++的重载overload。
在重载中,C++的成员函数有相同的函数名字,不同的参数表。C++编译器会扩展原有的函数名。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP