gcc与g++编译链接库文件兼容性

来源:互联网 发布:台湾艺人知乎 编辑:程序博客网 时间:2024/06/10 16:43

使用静态库的时候,gcc编译出来的静态链接库g++编译代码的时候有时候链接不了,而g++编译的静态链接库gcc在编译的时候有时候也使用不了。

下面举例说明:

编写库文件:

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/ -lhellogcc -o Test test.c  -I.../include/ -L.../lib/ -lhellog++ -o Test test.cc -I.../include/ -L.../lib/ -lhellog++ -o Test test.c  -I.../include/ -L.../lib/ -lhellogcc main.cgcc main.ccg++ main.cg++ main.cc

结论:

1.gcc编译hello.c形成libhello.a

gcctest.c正确gcctest.cc错误g++test.c错误g++test.cc错误

2.g++编译hello.cc形成libhello.a

gcctest.c错误gcctest.cc正确g++test.c正确g++test.cc正确

3.gcc编译hello.c形成libhello.so

gcctest.c正确gcctest.cc错误g++test.c错误g++test.cc错误

4.g++编译hello.cc形成libhello.so

gcctest.c错误gcctest.cc正确g++test.c正确g++test.cc正确

建议:

使用g++编写静态链接库和动态链接库





0 0
原创粉丝点击