为什么g ++不与我创建的动态库链接?

laz*_*low 6 c++ g++ dynamic-linking ld

我一直在尝试制作一些都依赖于同一个库的应用程序,动态库是我的第一个想法:所以我开始编写"库":

/* 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;
}
Run Code Online (Sandbox Code Playgroud)

我然后编译"图书馆"

g++ -shared -fPIC FileSystem.cpp -o ThinFS.o
Run Code Online (Sandbox Code Playgroud)

然后我迅速写了一个使用Library的文件:

#include "ThinFS.h"
int main() {
    FileSystem::create_container("foo");
    return (42);
}
Run Code Online (Sandbox Code Playgroud)

然后我尝试用它编译

g++ main.cpp -L. -lThinFS
Run Code Online (Sandbox Code Playgroud)

但它不会编译时出现以下错误:

/usr/bin/ld: cannot find -lThinFS
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

我想我错过了一些非常明显的东西,请帮帮我:)

Chr*_*odd 11

-lfoo在当前库路径中查找名为libfoo.a(静态)或libfoo.so(共享)的库,因此要创建库,需要使用g++ -shared -fPIC FileSystem.cpp -o libThinFS.so


jer*_*uan 6

您可以使用

g++ main.cpp -L. -l:ThinFS 
Run Code Online (Sandbox Code Playgroud)

使用"冒号"将原样使用库名称,而不需要前缀"lib"