简体   繁体   English

无法使用G ++导入共享库

[英]Cannot import shared library with g++

i made a shared library, linked it to a main program, but this main program cannot see any of the shared library methods, nor import it's headers. 我制作了一个共享库,将其链接到主程序,但是该主程序无法看到任何共享库方法,也无法导入其标头。 This is what i did: 这就是我所做的:

  1. I compiled each cpp file of the library in position independent code 我用位置无关的代码编译了库的每个cpp文件
 g++ -c -fPIC -o objname.o objname.cpp -I"HeadersFolder" 
  1. I created the shader library, and it is correctly created with the right name in my main directory 我创建了着色器库,并在主目录中使用正确的名称正确创建了该着色器库
 g++ -shared -o libmylib.so obj1.o obj2.o etc.. etc.. 
  1. I'm trying to compile a simple main with: 我正在尝试使用以下命令编译一个简单的main:
 g++ main.cpp -L. -lmylib 

Now in this main i imported one of the lib headers, something like: 现在在这个主目录中,我导入了一个lib标头之一,如下所示:

#include <Header.hpp>

And g++ tells me no such file or directory. 而且g ++告诉我没有这样的文件或目录。

"Now in this main i imported one of the lib headers, something like: ..." “现在在这个主目录中,我导入了一个lib头文件,类似于:...”

You also need to add the -I option to compile main.cpp in this case: 在这种情况下,还需要添加-I选项来编译main.cpp

g++ main.cpp -I"HeadersFolder" -L. -lmylib

Also you should use 你也应该用

#include "Header.hpp" // Note the quotes "

the angle brackets ( <> ) are for including system headers (that could possibly collide with your own when the preprocessor evaluates them). 尖括号( <> )用于包含系统标题(在预处理程序对其进行评估时,它们可能会与您自己的标题冲突)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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