[英]Why doesn't g++ link with the dynamic library I create?
我一直在嘗試制作一些都依賴於同一個庫的應用程序,動態庫是我的第一個想法:所以我開始編寫“庫”:
/* ThinFS.h */
class FileSystem {
public:
static void create_container(string file_name); //Creates a new container
};
/* ThinFS.cpp */
#include "ThinFS.h"
void FileSystem::create_container(string file_name) {
cout<<"Seems like I am going to create a new file called "<<file_name.c_str()<<endl;
}
我然后編譯“圖書館”
g++ -shared -fPIC FileSystem.cpp -o ThinFS.o
然后我迅速寫了一個使用Library的文件:
#include "ThinFS.h"
int main() {
FileSystem::create_container("foo");
return (42);
}
然后我嘗試用它編譯
g++ main.cpp -L. -lThinFS
但它不會編譯時出現以下錯誤:
/usr/bin/ld: cannot find -lThinFS
collect2: ld returned 1 exit status
我想我錯過了一些非常明顯的東西,請幫幫我:)
-lfoo
在當前庫路徑中查找名為libfoo.a
(static)或libfoo.so
(shared)的庫,因此要創建庫,需要使用g++ -shared -fPIC FileSystem.cpp -o libThinFS.so
您可以使用
g++ main.cpp -L. -l:ThinFS
使用“冒號”將原樣使用庫名稱,而不需要前綴“lib”
輸出文件的名稱應該是libThinFS.so ,例如
g++ -shared -fPIC FileSystem.cpp -o libThinFS.so
g++ -shared -fPIC FileSystem.cpp
的結果不是目標文件,因此它不應該以.o
結尾。 此外,共享庫應命名為libXXX.so
。 重命名庫,它將工作。
看看這篇文章。
http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html
關於如何構建不同類型庫的良好資源。 它還描述了如何以及在何處使用它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.