问题描述
我一直试图做一些应用程序,它们都依赖于同一个库,动态库是我第一个想法:所以我开始写库:
/ * ThinFS.h * /
class FileSystem {
public:
static void create_container(string file_name); //创建一个新容器
};
/ * ThinFS.cpp * /
#includeThinFS.h
void FileSystem :: create_container(string file_name){
cout<像我要创建一个名为<<< file_name.c_str()<< endl;
}
然后编译Library
g ++ -shared -fPIC FileSystem.cpp -o ThinFS.o
然后我快速写了一个使用库的文件:
#includeThinFS.h
int main(){
FileSystem :: create_container(foo);
return(42);
}
然后我尝试用
g ++ main.cpp -L。 -lThinFS
但它不会编译出以下错误:
/ usr / bin / ld:找不到-lThinFS
/ pre>
collect2:ld返回1退出状态
我想我缺少很明显的东西,请帮助我:)
解决方案
-lfoo
查找名为libfoo.a
(static)或libfoo.so
(共享)在当前库路径中,因此要创建库,需要使用g ++ -shared -fPIC FileSystem.cpp -o libThinFS。因此
I've been trying to make some applications which all rely on the same library, and dynamic libraries were my first thought: So I began writing the "Library":
/* 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; }
I then compile the "Library"
g++ -shared -fPIC FileSystem.cpp -o ThinFS.o
I then quickly wrote a file that uses the Library:
#include "ThinFS.h" int main() { FileSystem::create_container("foo"); return (42); }
I then tried to compile that with
g++ main.cpp -L. -lThinFS
But it won't compile with the following error:
/usr/bin/ld: cannot find -lThinFS collect2: ld returned 1 exit status
I think I'm missing something very obvious, please help me :)
解决方案
-lfoo
looks for a library calledlibfoo.a
(static) orlibfoo.so
(shared) in the current library path, so to create the library, you need to useg++ -shared -fPIC FileSystem.cpp -o libThinFS.so
这篇关于为什么g ++不能与我创建的动态库链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!