C++ 包含库 [英] C++ include libraries
问题描述
好的,已经有一段时间了,我遇到了#includes 问题
所以我在做
#include "someheader.h"
但它给了我
致命错误:someheader.h:没有那个文件或目录
我猜你会说这是一个系统范围的库.我正在运行 arch linux 并且我从 repo 安装了库,我认为 .h 文件在/usr/include 中.
我可以将所有头文件复制到我的代码所在的文件夹中,但这将是一个黑客.
执行此操作的正确"方法是什么?
我说 .h 文件在/usr/include 中是不正确的,我的意思是库文件夹在那里因此,Emile Cormier 的回答在一定程度上奏效了.现在的问题是头文件中有一些包含,从我尝试访问的方法看来,这些包含没有发生它给了我的错误
对 Namespace::Class::method() 的未定义引用
好吧,最后的答案是:
#include
并用
编译g++ code.cpp -llibrary_name
有时,库的头文件安装在 /usr/include/library_name
中,因此您必须像这样包含:
#include
使用您的文件管理器(或控制台命令)在您的系统上定位头文件,并查看是否应该在头文件名前加上目录名.
您收到的 undefined reference
错误是链接器错误.您收到此错误是因为您没有在 libsynaptics 中与您的程序一起链接,因此链接器找不到实现"您正在使用的 libsynaptics 函数.
如果您使用 GCC 从命令行编译,则必须添加 -lsynaptics
选项以链接 libsynaptics 库.如果您使用的是 IDE,则必须找到可以指定要链接到的库并添加突触的位置.如果您使用的是 makefile,则必须修改链接器标志列表,以便添加 -lsynaptics
.
还需要添加用于搜索路径的 -L
标志,以便链接器可以找到库,除非它安装在标准链接器搜索路径之一中.>
请参阅有关使用 GCC 链接到库的教程.
Ok, so it's been a while, and i'm having problems with #includes
So I'm doing
#include "someheader.h"
but it's giving me
fatal error: someheader.h: No such file or directory
It's a system wide library I guess you could say. I'm running arch linux and I installed the library from the repo, and I think the .h files are in /usr/include.
I could just copy all the header files into the folder my code is in but that would be a hack.
What is the "right" way to do this?
Edit: I wasn't correct by saying the .h files were in /usr/include, what I meant was that the library folder was in there So, Emile Cormier's answer worked to a certain extent. The problem now is that there are some include in the header file and it seems from the methods I'm trying to access that those includes are not happening it's giving my the error
undefined reference to Namespace::Class::method()
Edit: Ok so the final answer is:
#include <library_name/someheader.h>
And compile with
g++ code.cpp -llibrary_name
Sometimes, header files for a library are installed in /usr/include/library_name
, so you have to include like this:
#include <library_name/someheader.h>
Use your file manager (or console commands) to locate the header file on your system and see if you should prefix the header's filename with a directory name.
The undefined reference
error you're getting is a linker error. You're getting this error because you're not linking in libsynaptics along with your program, thus the linker cannot find the "implementation" of the libsynaptics functions you're using.
If you're compiling from the command-line with GCC, you must add the -lsynaptics
option to link in the libsynaptics library. If you're using an IDE, you must find the place where you can specify libraries to link to and add synaptics. If you're using a makefile, you have to modify your list of linker flags so that it adds -lsynaptics
.
Also the -L <path_to_library>
flag for the search path needs to be added, so the linker can find the library, unless it's installed in one of the standard linker search paths.
See this tutorial on linking to libraries with GCC.
这篇关于C++ 包含库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!