如何将自己的头文件目录添加到 Mac 终端 gcc 中?
我正在尝试编译一个 C 程序 (myProgram.c),其中包含指定目录中的自定义 .h 文件。如何将目录添加到 gcc,以便我可以随时使用 gcc myProgram
之类的命令构建 myProgram.c(没有标志以及什么没有)
I'm trying to compile a C program (myProgram.c) that includes a custom .h file that is in a specified directory. How can I add the directory to gcc so that I can build myProgram.c anytime using just a command like gcc myProgram
(with no flags and what not)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过更改
C_INCLUDE_PATH
环境变量来做到这一点,例如,您可以将其添加到
.bashrc
或.bash_profile
或任何始终具有环境变量设置正确。 以下是有关如何对库和 C++ 执行相同操作的参考。You can do this by altering the
C_INCLUDE_PATH
environment variable, e.g.You can add that to your
.bashrc
or.bash_profile
or whatever to always have the environment variable set properly. Here's a reference on how you can do the same for libraries and C++.必须使用一整套标志才能在 El Capitan 上运行:
had to use a whole set of flags to get this working on El Capitan:
Makefile 在这种情况下会很有帮助,它们简化了多个文件项目的编译。
假设您正在使用这些相同的文件,并且它们位于同一目录
示例 makefile 可能看起来像
或类似的东西,一般格式是
因此,通过运行
make all
您将指示编译器将源代码编译为目标文件的命令行,然后将这些目标文件链接到可执行文件中。Make 应该可以在任何现代系统上轻松使用。有关基本 makefile 和用法的更多信息,请参阅这个简单的教程:http://mrbook.org/tutorials/make/
Makefiles would be helpful in this situation, they ease the compilation of multiple file projects.
Assuming you are using these same files and they are in the same directory
A sample makefile could look like
Or something similar, the general format is
So by running
make all
from the commandline you would be instructing the compiler to compile your source code into object files, and then link those object files together into an executable.Make should be easily available on any modern system. For more information on basic makefiles and usage refer to this simple tutorial: http://mrbook.org/tutorials/make/