为什么没有G ++与动态库链接创建?
-
18-09-2019 - |
题
我一直试图做一些应用,这都依赖于相同的库,和动态库是我的第一个想法:所以我开始写的“库”:
/* 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
我然后迅速写道,使用库文件:
#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
(静态)或libfoo.so
(共享),因此创建库,则需要使用g++ -shared -fPIC FileSystem.cpp -o libThinFS.so
其他提示
可以使用
g++ main.cpp -L. -l:ThinFS
在使用“冒号”将使用的库名称,因为它是,而是需要的前缀“LIB”
输出文件的名称应的 libThinFS.so 下,e.g。
g++ -shared -fPIC FileSystem.cpp -o libThinFS.so
g++ -shared -fPIC FileSystem.cpp
的结果是不是一个对象文件,所以它不应该与.o
结束。此外,共享库应该命名为libXXX.so
。重命名库和它的工作。
看看这篇文章。
http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html
关于如何建立不同类型的图书馆的一个很好的资源。还介绍了如何以及在何处使用它们。
不隶属于 StackOverflow