在 linux 中更新包含路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11520084/
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
Update include path in linux
提问by username_4567
I have few header files in /my/path/to/file folder. I know how to include these files in new C program but everytime I need to type full path to header file before including it. Can I set some path variable in linux such that it automatically looks for header files ?
我在 /my/path/to/file 文件夹中有几个头文件。我知道如何在新的 C 程序中包含这些文件,但每次我都需要在包含头文件之前键入头文件的完整路径。我可以在 linux 中设置一些路径变量,以便它自动查找头文件吗?
采纳答案by betabandido
You could create a makefile. A minimal example would be:
你可以创建一个makefile。一个最小的例子是:
INC_PATH=/my/path/to/file
CFLAGS=-I$(INC_PATH)
all:
gcc $(CFLAGS) -o prog src1.c src2.c
From here you could improve this makefile in many ways. The most important, probably, would be to state compilation dependencies (so only modified files are recompiled).
从这里您可以通过多种方式改进这个 makefile。最重要的可能是声明编译依赖项(因此只重新编译修改过的文件)。
As a reference, here you have a link to the GNU make documentation.
作为参考,这里有一个指向GNU make 文档的链接。
If you do not want to use makefiles, you can always set an environment variable to make it easier to type the compilation command:
如果不想使用 makefile,可以随时设置环境变量,以便更轻松地键入编译命令:
export MY_INC_PATH=/my/path/to/file
Then you could compile your program like:
然后你可以编译你的程序,如:
gcc -I${MY_INC_PATH} -o prog src1.c src2.c ...
You may want to define MY_INC_PATH
variable in the file .bashrc
, or probably better, create a file in a handy place containing the variable definition. Then, you could use source
to set that variable in the current shell:
您可能希望MY_INC_PATH
在文件中定义变量.bashrc
,或者可能更好的是,在包含变量定义的方便位置创建一个文件。然后,您可以使用source
在当前 shell 中设置该变量:
source env.sh
I think, however, that using a makefile is a much preferable approach.
但是,我认为使用 makefile 是一种更可取的方法。
回答by Vladimir Mitrovic
I'm not in Linux right now and I can't be bothered to reboot to check if everything's right, but have you tried making symbolic links? For example, if you are on Ubuntu:
我现在不在 Linux 中,我也懒得重新启动以检查一切是否正确,但是您是否尝试过创建符号链接?例如,如果您使用的是 Ubuntu:
$ cd /usr/include
$ sudo ln -s /my/path/to/file mystuff
So then when you want to include stuf, you can use:
那么当你想包含 stuf 时,你可以使用:
#include <mystuff/SpamFlavours.h>
回答by xhudik
there is a similar question and likely better solved (if you are interested in a permanent solution): https://stackoverflow.com/a/558819/1408096
有一个类似的问题,可能会更好地解决(如果您对永久解决方案感兴趣):https: //stackoverflow.com/a/558819/1408096
Try setting C_INCLUDE_PATH(for C header files) or CPLUS_INCLUDE_PATH(for C++ header files).
尝试设置C_INCLUDE_PATH(对于 C 头文件)或CPLUS_INCLUDE_PATH(对于 C++ 头文件)。
Kudos:jcrossley3
荣誉:jcrossley3