C++ 包含库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9755261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:16:07  来源:igfitidea点击:

C++ include libraries

c++headerarchlinux

提问by Jay

Ok, so it's been a while, and i'm having problems with #includes

好的,已经有一段时间了,我在使用 #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.

我想你可以说这是一个系统范围的库。我正在运行 arch linux 并且我从 repo 安装了库,我认为 .h 文件在 /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

编辑:我说 .h 文件在 /usr/include 中是不正确的,我的意思是库文件夹在那里所以,Emile Cormier 的回答在一定程度上有效。现在的问题是头文件中有一些包含,从我尝试访问的方法看来,这些包含没有发生,这给了我错误

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

回答by nos

You'd use #include <someheader.h>for header files in system locations.

您将#include <someheader.h>用于系统位置中的头文件。

#include "someheader.h"would try to include the file someheader.h in the directory of your .c file.

#include "someheader.h"会尝试将文件 someheader.h 包含在 .c 文件的目录中。

In addition to including the header file, you also need to link in the library, which is done with the -l argument:

除了包含头文件之外,还需要在库中进行链接,这是通过 -l 参数完成的:

g++ -Wall youprogram.cpp -lname_of_library

Not doing so is the reason for the "undefined reference .. " linker errors.

不这样做是“未定义引用..”链接器错误的原因。

回答by Emile Cormier

Sometimes, header files for a library are installed in /usr/include/library_name, so you have to include like this:

有时,库的头文件安装在 中/usr/include/library_name,因此您必须像这样包含:

#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 referenceerror 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.

undefined reference您收到的错误是链接器错误。您收到此错误是因为您没有在 libsynaptics 中与您的程序一起链接,因此链接器找不到您正在使用的 libsynaptics 函数的“实现”。

If you're compiling from the command-line with GCC, you must add the -lsynapticsoption 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.

如果您使用 GCC 从命令行编译,则必须添加-lsynaptics选项以在 libsynaptics 库中进行链接。如果您使用的是 IDE,则必须找到可以指定要链接到的库并添加突触的位置。如果您使用的是 makefile,则必须修改链接器标志列表,以便将-lsynaptics.

See this tutorialon linking to libraries with GCC.

请参阅有关使用 GCC 链接到库的教程

回答by pmr

The quick fix is to do use:

快速修复是使用:

#include <someheader.h>

assuming that someheader.his in the standard include locations (to find it use the command locate someheader.hin a shell. If it is in /usr/includeit is in a standard location. If it is in a subdirectory of /usr/includeyou only need to add the part of the directory up to /usr/includein the #includedirective (e.g. #include <fancy_lib/someheader.h>)

假设它someheader.h在标准包含位置(要找到它,请使用locate someheader.hshell 中的命令。如果它/usr/include在标准位置。如果它在一个子目录中,/usr/include您只需要添加目录的一部分到/usr/includein该#include指令(例如#include <fancy_lib/someheader.h>

However, this is only half of the story. You also will need to set up your build system in a way that locates the given library and adds its include path (the path under which it's header files are stored) to the compiler command (for gccthat is -I/path/to/header). That way you can also build with different versions by configuring them in your build system. If the library is not header-only you will also have to add it to the linker dependencies. How this is achieved in your build system is best found out by consulting its documentation.

然而,这只是故事的一半。您还需要以定位给定库的方式设置构建系统,并将其包含路径(存储它的头文件的路径)添加到编译器命令(gcc-I/path/to/header)。这样,您还可以通过在构建系统中配置不同版本来构建它们。如果库不是仅标头,则还必须将其添加到链接器依赖项中。最好通过查阅其文档来了解如何在您的构建系统中实现这一点。