C++:如何添加外部库
我正在尝试将 SVL 添加到我的项目中。
如果没有它,我会收到数百个错误(未定义的参考...)。添加 -lSVL 后,所有错误都消失了,但 gcc 说:“找不到 -lSVL”。其他一切(SDL、SDL_TTF、SDL_Mixer...)都工作正常。
I'm trying to add SVL to my project.
Without it I'm getting hundreds of errors (undefined reference...). After adding -lSVL all errors are gone, but gcc says: "cannot find -lSVL". Everything else (SDL, SDL_TTF, SDL_Mixer...) works fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该告知 gcc libsvl.a 的安装路径,例如:
另外,如果您在 Linux 下工作,请注意大小写(“SVL”与“svl”不同)。
You should inform gcc of the path where libsvl.a is installed, for example:
Also, pay attention to the case if you work under Linux ("SVL" is different from "svl").
添加外部库分为两个部分;您需要告诉编译器[1]在哪里可以找到API的描述(即头文件)并且您需要告诉链接器 在哪里可以找到 API 的实现(即库文件)。
标头的可能位置列表由包含路径给出,对于传统编译器,该路径是通过
-I
选项添加的。它需要一个目录名称来添加;该目录是编译器查找头文件的额外位置。库的可能位置列表由链接路径给出。它就像包含路径一样,但使用
-L
添加到其中。请注意,您还可以(至少通常)直接在命令行上提供库的完整路径,但并不特别推荐这样做,因为它往往会在可执行文件中嵌入比实际需要更多的信息。MSVC 的语法与 IIRC 非常相似。
如果您使用的是 IDE,您可能必须在项目选项中设置这些内容,但只要您记住需要设置包含路径和库路径,您就能够找到解决方法。
[1]严格来说,您是在告诉预处理器,但预处理器的输出实际上总是直接直接进入编译器。
There are two parts to adding an external library; you need to tell the compiler[1] where to find the description of the API (i.e., the header files) and you need to tell the linker where to find the implementation of the API (i.e., the library file(s)).
The list of possible locations of the headers is given by the include path, which with a traditional compiler is added to with the
-I
option. It takes a directory name to add; the directory is one extra place the compiler will look for header files.The list of possible locations of the library is given by the link path. It's just like the include path, but is added to with
-L
. Note that you can also (at least normally) give the full path to the library directly on the command line, but this isn't particularly recommended because it tends to embed more information in the executable than is really needed.The syntax for MSVC is recognizably similar IIRC.
If you're using an IDE, you'll probably have to set these things in the project options, but as long as you remember that you need to set both include and library paths, you'll be able to find your way through.
[1]Strictly, you're telling the preprocessor, but the preprocessor's output is virtually always directed straight into the compiler proper.