LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2358|回复: 17

怎么把C和C++的目标程序链接到一起?

[复制链接]
发表于 2006-4-13 14:36:56 | 显示全部楼层 |阅读模式
main函数用C写,调用一个C++文件里的函数,这样的程序怎么编译?
发表于 2006-4-13 17:29:09 | 显示全部楼层
C++ 文件的函数得extern "C" ,然后用 g++编译成.o 文件。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-4-14 19:34:18 | 显示全部楼层
多谢弥敦路九号,是不是两个目标文件只能用g++链接起来,不能用gcc?
回复 支持 反对

使用道具 举报

发表于 2006-4-14 23:20:36 | 显示全部楼层
当然,C毕竟是C++的一个子集
回复 支持 反对

使用道具 举报

发表于 2006-4-16 12:36:03 | 显示全部楼层
Post by libra_kevin
多谢弥敦路九号,是不是两个目标文件只能用g++链接起来,不能用gcc?


我原来这么用过,可以通过:


  1. /*me.h*/
  2. #ifdef __cplusplus
  3. extern "C" {
  4. #endif

  5. void receive(void);
  6. void send(void);

  7. #ifdef __cplusplus
  8. }
  9. #endif

  10. /*me.c*/
  11. #include "me.h"

  12. void receive(void)
  13. {
  14. /*...*/
  15. }

  16. void send(void)
  17. {
  18. /*...*/
  19. }

  20. /*test.cpp*/
  21. #include "me.h"
  22. int  main()
  23. {
  24. receive();
  25. send();
  26. return 0;
  27. }

复制代码



build sequence:


  1. gcc -c  me.c
  2. g++ -c test.cpp
  3. g++ -o a.out me.o test.o
复制代码


or

  1. gcc -c me.c
  2. gcc -c test.cpp
  3. gcc  -o a.out me.o test.o -lstdc++
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-4-16 22:43:29 | 显示全部楼层
谢谢Illidan,你的方法也很好用.

不过有些地方不大明白:
#ifdef __cplusplus 是做什么用的?在哪里有它的define?还是编译器自己define的?

还有就是gcc怎么也能编译C++的代码了?
回复 支持 反对

使用道具 举报

发表于 2006-4-16 23:23:39 | 显示全部楼层
就是说如果用g++编译才加extern "C"
否则,用gcc编译的话,不加extern "C",加了好像会报语法错误
回复 支持 反对

使用道具 举报

发表于 2006-4-17 14:09:04 | 显示全部楼层
me.h的头文件

  1. #include <cstdio>

  2. extern "C" void CppPrintf(void);
复制代码

me.cpp文件

  1. #include <iostream>
  2. #include "me.h"

  3. using namespace std;

  4. void CppPrintf(void)
  5. {
  6.      cout << "Hello\n";
  7. }
复制代码

test.c文件

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "me.h"         //新增加的。用gcc编译时不需要此头文件,但用g++编译时却要此文件


  4. int main(void)
  5. {
  6.     CppPrintf();

  7.     return 0;
  8. }
复制代码

第一种编译:不需要再test2.c中包含头文件me.h

  1. [root@root GUI]# gcc -c me.cpp
  2. [root@root GUI]# gcc -c test2.c
  3. [root@root GUI]# gcc -o test2  test2.o me.o -lstdc++
  4. [root@root GUI]#
  5. [root@root GUI]# ./test2
  6. Hello

复制代码

第二种编译:没有在test2.c中新增的me.h文件时的编译

  1. [root@root GUI]# g++ -c me.cpp
  2. [root@root GUI]# g++ -c test2.c
  3. test2.c: In function `int main()':
  4. test2.c:7: error: `CppPrintf' undeclared (first use this function)
  5. test2.c:7: error: (Each undeclared identifier is reported only once for each function it appears in.)
复制代码

可以看出gcc可以编译.cpp而g++也可以编译.c。但是用g++编译.c时要加头文件,什么原因?
再用g++编译
第三种编译:在test2.c中包含了me.h的头文件

  1. [root@root GUI]# g++ -c me.cpp
  2. [root@root GUI]# g++ -c test2.c
  3. [root@root GUI]# g++ -o test2 test2.o me.o -lstdc++
  4. [root@root GUI]#
  5. [root@root GUI]# ./test2
  6. Hello

复制代码
回复 支持 反对

使用道具 举报

发表于 2006-4-17 14:18:51 | 显示全部楼层
楼上的main.c忘写函数声明了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-4-17 15:11:21 | 显示全部楼层
Post by sybaselu
me.h的头文件

  1. #include <cstdio>

  2. extern "C" void CppPrintf(void);
复制代码

me.cpp文件

  1. #include <iostream>
  2. #include "me.h"

  3. using namespace std;

  4. void CppPrintf(void)
  5. {
  6.      cout << "Hello\n";
  7. }
复制代码

test.c文件

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "me.h"         //新增加的。用gcc编译时不需要此头文件,但用g++编译时却要此文件


  4. int main(void)
  5. {
  6.     CppPrintf();

  7.     return 0;
  8. }
复制代码

第一种编译:不需要再test2.c中包含头文件me.h

  1. [root@root GUI]# gcc -c me.cpp
  2. [root@root GUI]# gcc -c test2.c
  3. [root@root GUI]# gcc -o test2  test2.o me.o -lstdc++
  4. [root@root GUI]#
  5. [root@root GUI]# ./test2
  6. Hello

复制代码

第二种编译:没有在test2.c中新增的me.h文件时的编译

  1. [root@root GUI]# g++ -c me.cpp
  2. [root@root GUI]# g++ -c test2.c
  3. test2.c: In function `int main()':
  4. test2.c:7: error: `CppPrintf' undeclared (first use this function)
  5. test2.c:7: error: (Each undeclared identifier is reported only once for each function it appears in.)
复制代码

可以看出gcc可以编译.cpp而g++也可以编译.c。但是用g++编译.c时要加头文件,什么原因?
再用g++编译
第三种编译:在test2.c中包含了me.h的头文件

  1. [root@root GUI]# g++ -c me.cpp
  2. [root@root GUI]# g++ -c test2.c
  3. [root@root GUI]# g++ -o test2 test2.o me.o -lstdc++
  4. [root@root GUI]#
  5. [root@root GUI]# ./test2
  6. Hello

复制代码


我试了另一种编译: test.c中不包含me.h,用gcc编译test.c,g++编译me.cpp,最后用g++链接

  1. kevin@libra:~/workspace/try$ gcc -c test.c
  2. kevin@libra:~/workspace/try$ g++ -c me.cpp
  3. kevin@libra:~/workspace/try$ g++ -o test test.o me.o
  4. kevin@libra:~/workspace/try$ ./test
  5. Hello
复制代码


我现在感觉有点晕了,哪位能推荐一本讲这些方面的书? 或者在哪能找到相关资料?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表