all:test.cpp
g++ -o test test.cpp -I/bbox/anaconda/include/python2.7 -L/bbox/anaconda/lib -lpython2.7 -I. -I/bbox/anaconda/lib/python2.7/site-packages/numpy/core/include

 

编译范例

 

 

 

 

 

test.h

编译范例
1 #ifndef _TEST_H_
2 #define _TEST_H_
3
4 void TestA();
5 void TestB();
6
7 #endif
编译范例



test_a.cpp

编译范例
1 #include <stdio.h>
2 #include "test.h"
3
4 void TestA()
5 {
6 printf("TestA func\n");
7 }
编译范例



test_b.cpp

编译范例
1 #include <stdio.h>
2 #include "test.h"
3
4 void TestB()
5 {
6 printf("TestB func\n");
7 }
编译范例



生成so文件的命令

g++ test_a.cpp test_b.cpp -fPIC -shared -o libtest.so

生成.a文件的命令

1 gcc -c test_a.cpp
2 gcc -c test_b.cpp
3 ar -r libtest.a test_a.o test_b.o




test.cpp

编译范例
1 #include "test.h"
2
3 int main()
4 {
5 TestA();
6 TestB();
7
8 return 0;
9 }
编译范例



采用动态库编译命令

g++ test.cpp -o test -L. -ltest



执行

export LD_LIBRARY_PATH=./
./test

执行结果如下。

编译范例

 

采用静态库编译命令

g++ -static -o test -L. -ltest test.cpp

执行效果

编译范例


静态库的嵌套调用,有时候我想做一个自己的静态库,它里面要调用其他静态库里面的函数,经过试验

这个好像用ar -r不行,所以就在链接的时候需要两个库文件都包含,同时要有这一个头文件才行。。。

相关文章:

  • 2021-05-15
  • 2022-12-23
  • 2021-10-14
  • 2021-12-23
  • 2021-07-01
  • 2021-11-11
  • 2021-09-03
猜你喜欢
  • 2021-06-27
  • 2021-10-21
  • 2022-12-23
  • 2021-09-15
  • 2022-12-23
相关资源
相似解决方案