gcc与g++编译链接库文件兼容性
2016-02-23 19:25
274 查看
使用静态库的时候,gcc编译出来的静态链接库g++编译代码的时候有时候链接不了,而g++编译的静态链接库gcc在编译的时候有时候也使用不了。
下面举例说明:
编写库文件:
hello.h
hello.c
测试代码:
main.c
main.cc
test.c
test.cc
Makefile
结论:
1.gcc编译hello.c形成libhello.a
2.g++编译hello.cc形成libhello.a
3.gcc编译hello.c形成libhello.so
4.g++编译hello.cc形成libhello.so
建议:
使用g++编写静态链接库和动态链接库
下面举例说明:
编写库文件:
hello.h
#ifndef HELLO_H_ #define HELLO_H_ #include <stdio.h> void HelloFunc(); #endif /*HELLO_H_*/
hello.c
#include "hello.h" void HelloFunc() { printf("Hello World!\n"); }hello.cc
#include "hello.h" void HelloFunc() { printf("Hello World!\n"); }
测试代码:
main.c
#include <stdio.h> int main() { printf("Hello World!\n"); return 0; }
main.cc
#include <stdio.h> int main() { printf("Hello World!\n"); return 0; }
test.c
#include "hello.h" int main() { HelloFunc(); return 0; }
test.cc
#include "hello.h" int main() { HelloFunc(); return 0; }
Makefile
gcc -o Test test.cc -I.../include/ -L.../lib/ -lhello gcc -o Test test.c -I.../include/ -L.../lib/ -lhello g++ -o Test test.cc -I.../include/ -L.../lib/ -lhello g++ -o Test test.c -I.../include/ -L.../lib/ -lhello gcc main.c gcc main.cc g++ main.c g++ main.cc
结论:
1.gcc编译hello.c形成libhello.a
gcc | test.c | 正确 |
gcc | test.cc | 错误 |
g++ | test.c | 错误 |
g++ | test.cc | 错误 |
gcc | test.c | 错误 |
gcc | test.cc | 正确 |
g++ | test.c | 正确 |
g++ | test.cc | 正确 |
gcc | test.c | 正确 |
gcc | test.cc | 错误 |
g++ | test.c | 错误 |
g++ | test.cc | 错误 |
gcc | test.c | 错误 |
gcc | test.cc | 正确 |
g++ | test.c | 正确 |
g++ | test.cc | 正确 |
使用g++编写静态链接库和动态链接库
相关文章推荐
- 在Windows平台下使用安装GCC(图)
- 链接库动态链接库详细介绍
- linux使用gcc编译c语言共享库步骤
- C/C++编译器GCC下的常用编译命令总结
- GCC 编译c程序的方法及过程解析
- Glusterfs的编译选项 #pragma GCC poison system popen
- 【笔记】CentOS上源码安装GCC 4.8.2
- 【笔记】CentOS
- RHEL 4.7 (64bit) 环境安装 GCC 4.6 测试记录 (更新至gcc-4.6.1)
- GCC 5 即将发布
- [转载]Visual Studio, GCC, Clang, Intel 对 C++11 特性的支持一览表
- C++爱好者博客
- gcc工具的运用
- gdb的运用
- GCC内联汇编(1)Get started
- GCC内嵌汇编
- g++编译 参数 .
- RHEL5.3 gcc rpm包安装
- linux GCC 64位编程技巧