如何在linux中找到要包含在库函数中的头文件

时间:2022-05-01 06:26:10

Given a function, let's say atoi, how can I find the header file I should include if I want to use this function ? I'm always get puzzled for that issue. If let me treat function like "atoi" as linux c api, I can put my question in another way as : Is a document for linux c api ?

给定一个函数,让我们说atoi,如果我想使用这个函数,如何找到我应该包含的头文件?我总是对这个问题感到困惑。如果让我把像“atoi”这样的函数视为linux c api,我可以用另一种方式提出我的问题:是linux c api的文档吗?

5 个解决方案

#1


12  

Man pages. Type man atoi (or, in general, man <function>) at your command prompt. It will give you usage information as well as a listing of which headers to include.

手册页。在命令提示符下键入man atoi(或者,通常是man )。它将为您提供使用信息以及要包含的标头列表。

Man pages also document programs and commands (find, grep, cd, etc.). Sometimes you may run into a case where a program has the same name as a C function (e.g. write). In that case, you need to direct man to look in the correct section of the manual, section 2 for system calls and section 3 for library functions. You do this by inserting the section number between "man" and the command name: man 2 write. If you do not know whether a given function is a system call or a library function, try both.

手册页还记录了程序和命令(find,grep,cd等)。有时您可能会遇到一个程序与C函数同名的情况(例如写入)。在这种情况下,您需要指示人员查看手册的正确部分,第2部分查看系统调用,第3部分查看库函数。您可以通过在“man”和命令名之间插入节号来完成此操作:man 2 write。如果您不知道给定的函数是系统调用还是库函数,请尝试两者。

You can learn more about manual pages by typing man man.

您可以通过输入man man来了解有关手册页的更多信息。

#2


2  

Or, you can search your system's /usr/include directory for occurrences of the function definition you're looking for. This is especially useful for embedded or stripped-down linux systems that are missing man pages.

或者,您可以在系统的/ usr / include目录中搜索您正在查找的函数定义的出现位置。这对于缺少手册页的嵌入式或精简版Linux系统尤其有用。

find /usr/include -name "*.h" -print | xargs grep "<function-you-are-looking-for>"

For example, if you do:

例如,如果你这样做:

find /usr/include -name "*.h" -print | xargs grep atoi

You'll get back something like this:

你会得到这样的东西:

/usr/include/stdlib.h:extern int atoi (__const char *__nptr)

The result contains both the header file name and the interface definition.

结果包含头文件名和接口定义。

  • Please note that your /usr/include directory might be elsewhere.
  • 请注意,您的/ usr / include目录可能位于其他位置。

#3


1  

Is a document for linux c api ?

Certainly. The documentation is available as man pages. Type man <function> in a terminal and enjoy. Which header file you need to include is usually shown at the top.

当然。该文档以手册页形式提供。在终端输入man 并享受。您需要包含哪个头文件通常显示在顶部。

#4


1  

If you are using ctags and the vim editor and you have set up ctags to scan /usr/include then ctrl-] while you're on the function you want to find takes you to the headerfile!

如果你正在使用ctags和vim编辑器并且你已经设置了ctags来扫描/ usr / include然后ctrl-]当你在你想要找到的函数上时会带你到头文件!

#5


0  

You can use the following also

您也可以使用以下内容

whereis <function name> 

It will give the path name for the function. Then open the path using vim editor. Then using the "vim" editor you can see the header file.

它将给出函数的路径名。然后使用vim编辑器打开路径。然后使用“vim”编辑器,您可以看到头文件。

Example

> whereis atoi 
   atoi: /usr/share/man/man3/atoi.3.gz

 > vim /usr/share/man/man3/atoi.3.gz

   ----------
   ----------
  .B #include <stdlib.h>

#1


12  

Man pages. Type man atoi (or, in general, man <function>) at your command prompt. It will give you usage information as well as a listing of which headers to include.

手册页。在命令提示符下键入man atoi(或者,通常是man )。它将为您提供使用信息以及要包含的标头列表。

Man pages also document programs and commands (find, grep, cd, etc.). Sometimes you may run into a case where a program has the same name as a C function (e.g. write). In that case, you need to direct man to look in the correct section of the manual, section 2 for system calls and section 3 for library functions. You do this by inserting the section number between "man" and the command name: man 2 write. If you do not know whether a given function is a system call or a library function, try both.

手册页还记录了程序和命令(find,grep,cd等)。有时您可能会遇到一个程序与C函数同名的情况(例如写入)。在这种情况下,您需要指示人员查看手册的正确部分,第2部分查看系统调用,第3部分查看库函数。您可以通过在“man”和命令名之间插入节号来完成此操作:man 2 write。如果您不知道给定的函数是系统调用还是库函数,请尝试两者。

You can learn more about manual pages by typing man man.

您可以通过输入man man来了解有关手册页的更多信息。

#2


2  

Or, you can search your system's /usr/include directory for occurrences of the function definition you're looking for. This is especially useful for embedded or stripped-down linux systems that are missing man pages.

或者,您可以在系统的/ usr / include目录中搜索您正在查找的函数定义的出现位置。这对于缺少手册页的嵌入式或精简版Linux系统尤其有用。

find /usr/include -name "*.h" -print | xargs grep "<function-you-are-looking-for>"

For example, if you do:

例如,如果你这样做:

find /usr/include -name "*.h" -print | xargs grep atoi

You'll get back something like this:

你会得到这样的东西:

/usr/include/stdlib.h:extern int atoi (__const char *__nptr)

The result contains both the header file name and the interface definition.

结果包含头文件名和接口定义。

  • Please note that your /usr/include directory might be elsewhere.
  • 请注意,您的/ usr / include目录可能位于其他位置。

#3


1  

Is a document for linux c api ?

Certainly. The documentation is available as man pages. Type man <function> in a terminal and enjoy. Which header file you need to include is usually shown at the top.

当然。该文档以手册页形式提供。在终端输入man 并享受。您需要包含哪个头文件通常显示在顶部。

#4


1  

If you are using ctags and the vim editor and you have set up ctags to scan /usr/include then ctrl-] while you're on the function you want to find takes you to the headerfile!

如果你正在使用ctags和vim编辑器并且你已经设置了ctags来扫描/ usr / include然后ctrl-]当你在你想要找到的函数上时会带你到头文件!

#5


0  

You can use the following also

您也可以使用以下内容

whereis <function name> 

It will give the path name for the function. Then open the path using vim editor. Then using the "vim" editor you can see the header file.

它将给出函数的路径名。然后使用vim编辑器打开路径。然后使用“vim”编辑器,您可以看到头文件。

Example

> whereis atoi 
   atoi: /usr/share/man/man3/atoi.3.gz

 > vim /usr/share/man/man3/atoi.3.gz

   ----------
   ----------
  .B #include <stdlib.h>