通过g ++编译将.so文件链接到.cpp文件 [英] Link .so file to .cpp file via g++ compiling

查看:442
本文介绍了通过g ++编译将.so文件链接到.cpp文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让一个库在我的c ++项目中工作,并且对于不习惯使用c ++的人没有明确的说明如何做到这一点

i am trying to get a library to work in my c++ project and there is no clear instructions of how to do this for people who are not used to c++

以下链接是我最近到达的

它声明以下内容

-L/path/to/my/library/folder -ldllname

还有以下线程指出以下内容

gcc yourfile.cpp -lblah

现在从我能看到的命令是-l + filename,例如我的文件名是directory/libtest.so它将是-ldirectory/libtest.so,这是正确的,有人可以澄清

now from what i can see the command is -l + filename, for example my filename is directory/libtest.so it would be -ldirectory/libtest.so, is this correct, could someone clarify

我当前正在使用以下命令来编译我的maincpp.cpp文件,但是想包含一个名为例如./directory/libtest.so

i am currently using the following command to compile my maincpp.cpp file, would like to however include a .so file called for example ./directory/libtest.so

g++ -fPIC -o libgetmacip.so -shared -I $JAVA_HOME/include -I $JAVA_HOME/include/linux maincpp.cpp cpptoinclude.cpp

推荐答案

现在从我能看到的命令是-l + filename,例如我的文件名是directory/libtest.so,它将是-ldirectory/libtest.so

now from what i can see the command is -l + filename, for example my filename is directory/libtest.so it would be -ldirectory/libtest.so

不,那是不正确的.它应该是-Ldirectory -ltest,即您使用-L将目录添加到链接器将在其中查找库的搜索路径,并说出要链接到-l的库,但要链接到libtest.solibtest.a您说的-ltest不带lib前缀或文件扩展名.

No, that's not correct. It should be -Ldirectory -ltest i.e. you use -L to add a directory to the search paths where the linker will look for libraries, and you say which libraries to link to with -l, but to link to libtest.so or libtest.a you say -ltest without the lib prefix or the file extension.

可以通过显式命名文件而没有-L-l选项(即仅directory/libtest.so)来链接,但是对于动态库,这几乎总是错误的做法,例如它将确切的路径嵌入可执行文件中,因此程序运行时,同一库必须位于同一位置.通常,您希望按名称(而不是路径)链接到它,以便可以在运行时从任何位置使用具有该名称的库.

You can link by naming the file explicitly, without -L or -l options, i.e. just directory/libtest.so, but for dynamic libraries that is almost always the wrong thing to do, as it embeds that exact path into the executable, so the same library must be in the same place when the program runs. You typically want to link to it by name (not path) so that the library with that name can be used from any location at run-time.

这篇关于通过g ++编译将.so文件链接到.cpp文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆