內的共享庫,我想一個共享庫,但G ++抱怨中使用的共享庫...使用共享庫
這是我的測試樣品的外觀:
.
├── extA
│ ├── a.cpp
│ ├── a.h
│ └── libA.so
├── extB
│ ├── b.cpp
│ ├── b.h
│ └── libB.so
└── main.cpp
啊:
#pragma once
int f(void);
a.cpp:
#include "a.h"
int f(void) {
return 42;
}
b.h:
#pragma once
#include "a.h"
int g(void);
b.cpp:
#include "b.h"
int g(void) {
return f();
}
main.cpp中:
#include <cstdlib>
#include <iostream>
#include "b.h"
int main(int, char **) {
std::cout << g() << std::endl;
return EXIT_SUCCESS;
}
我敢肯定,我的編譯方式是錯誤的。這是我做的:
g++ -c -fpic a.cpp
g++ -shared -o libA.so a.o
創建libA.so,
g++ -c -fpic -I../extA b.cpp -L../extA -lA
g++ -shared -o libB.so b.o
創建libB.so(我認爲這種方式是不正確),最後:
g++ -IexB main.cpp -LextB -lB -o main
的最後G ++ - 調用導致錯誤信息:
In file included from main.cpp:4:0:
extB/b.h:3:15: fatal error: a.h: No such file or directory
#include "a.h"
^
compilation terminated.
有人可以告訴我,編譯/創建libB.so的正確方法是什麼?