繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM