如何将.h和.o文件添加到gcc

时间:2021-08-25 13:16:43

I'm trying to figure out how to add header and object files to the standard library so that I can use my custom files as easy as the standard library.

我正在试图弄清楚如何将标题库和目标文件添加到标准库中,以便我可以像使用标准库一样轻松使用我的自定义文件。

Currently I have to type the path to the header file in the .c file and then link the object file path when compiling.

目前,我必须在.c文件中键入头文件的路径,然后在编译时链接目标文件路径。

I would like to just be able to add:

我想能够添加:

#include <mystdlib.h>

and not worry about linking the object file like I do when I reference the stdio.h header file.

并且不用担心像我在引用stdio.h头文件时那样链接目标文件。

I have searched around, but I fear I'm not using the proper terminology as I don't seem to find the results I need. Am I the first to want to do this, or it is just impossible, and therefore people don't even try?

我一直在寻找,但我担心我没有使用正确的术语,因为我似乎没有找到我需要的结果。我是第一个想要这样做的人,或者这是不可能的,因此人们甚至不尝试?

1 个解决方案

#1


3  

gcc uses environment variables C_INCLUDE_PATH and LIBRARY_PATH to look for header and library files. Setting them somewhere (eg., your bash_profile) should achieve what you describe:

gcc使用环境变量C_INCLUDE_PATH和LIBRARY_PATH来查找头文件和库文件。将它们设置在某处(例如,你的bash_profile)应该实现你所描述的:

export C_INCLUDE_PATH="$HOME/include"
export LIBRARY_PATH="$HOME/lib"

Alternatively, the -I and -L flags add directories to the list of directories to be searched for header files and library files, respectively.

或者,-I和-L标志分别将目录添加到要搜索头文件和库文件的目录列表中。

edit: As noted by @ChrisStratton below, the library name or object file needs to be explicitly specified. AFAIK, there is no way to make gcc always link against a library (like with libc).

编辑:如下面@ChrisStratton所述,需要明确指定库名称或目标文件。 AFAIK,没有办法让gcc始终链接到一个库(比如libc)。

sources:

https://gcc.gnu.org/onlinedocs/gcc/Environment-Variables.html https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html

#1


3  

gcc uses environment variables C_INCLUDE_PATH and LIBRARY_PATH to look for header and library files. Setting them somewhere (eg., your bash_profile) should achieve what you describe:

gcc使用环境变量C_INCLUDE_PATH和LIBRARY_PATH来查找头文件和库文件。将它们设置在某处(例如,你的bash_profile)应该实现你所描述的:

export C_INCLUDE_PATH="$HOME/include"
export LIBRARY_PATH="$HOME/lib"

Alternatively, the -I and -L flags add directories to the list of directories to be searched for header files and library files, respectively.

或者,-I和-L标志分别将目录添加到要搜索头文件和库文件的目录列表中。

edit: As noted by @ChrisStratton below, the library name or object file needs to be explicitly specified. AFAIK, there is no way to make gcc always link against a library (like with libc).

编辑:如下面@ChrisStratton所述,需要明确指定库名称或目标文件。 AFAIK,没有办法让gcc始终链接到一个库(比如libc)。

sources:

https://gcc.gnu.org/onlinedocs/gcc/Environment-Variables.html https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html