包含 Linux 头文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3705709/
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-04 23:09:58  来源:igfitidea点击:

Include Linux header file

c++linux

提问by ashley

How could I include a linux header file? I read from the web that the header file is in /usr/include. However, the header file I need is not in that directory; it is in /usr/src/kernels/2.6.32.../include/linux/.

我怎么能包含一个linux头文件?我从网上读到头文件在/usr/include. 但是,我需要的头文件不在那个目录中;它在/usr/src/kernels/2.6.32.../include/linux/.

I tried to include the file using the full path. However, the file itself includes other header files as well. Hence, the compiler couldn't find the other header files when I compile.

我尝试使用完整路径包含该文件。但是,文件本身也包含其他头文件。因此,编译器在我编译时找不到其他头文件。

How could I include the header file in my program and compile the program?

如何在程序中包含头文件并编译程序?

回答by Alex Martelli

You may add to your gcccommand line -I(for "includes") options which specify other directories to search for include files (besides or actually even instead of the normal ones like /usr/include, though it's so long since I last needed the "instead of" that I don't recall how that's done == man gccshould tell you in 5 minutes if you need to find out;-).

您可以添加到您的gcc命令行-I(对于“包含”)选项,这些选项指定其他目录来搜索包含文件(除了或实际上甚至代替正常的,例如/usr/include,尽管自从我上次需要“代替”我已经很久了不记得这是怎么做的 ==man gcc如果您需要找出答案,应该在 5 分钟内告诉您;-)。

回答by Matt Joiner

If you are on Ubuntu, install libcpufreq-dev. This will give you the cpufreq.hheader at /usr/include/cpufreq.h, which you can include from your code with #include <cpufreq.h>.

如果您使用的是 Ubuntu,请安装libcpufreq-dev. 这将为您提供位于 的cpufreq.h标题/usr/include/cpufreq.h,您可以使用#include <cpufreq.h>.

回答by loxxy

By default, gcc searches the following directories for header files:

默认情况下,gcc 在以下目录中搜索头文件:

/usr/local/include/
/usr/include/

and the following directories for libraries:

以及以下库目录:

/usr/local/lib/
/usr/lib/

The compiler options -I and -L add new directories to the beginning of the include path and library search path respectively.

编译器选项 -I 和 -L 分别将新目录添加到包含路径和库搜索路径的开头。