Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add my own header file directory to Mac Terminal gcc?

Tags:

c

bash

shell

macos

gcc

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)

like image 958
m0rtimer Avatar asked Apr 18 '11 00:04

m0rtimer


People also ask

Where does GCC look for header files on Mac?

GCC looks for headers requested with #include " file " first in the directory containing the current file, then in the directories as specified by -iquote options, then in the same places it would have looked for a header requested with angle brackets. For example, if /usr/include/sys/stat. h contains #include "types.

Which option in GCC is used to specify the location of the .h files?

gcc -I adds include directory of header files.

Does GCC need header files?

GCC needs to install corrected versions of some system header files. This is because most target systems have some header files that won't work with GCC unless they are changed. Some have bugs, some are incompatible with ISO C, and some depend on special features of other compilers.

How do I create a custom header file?

To make a header file, we have to create one file with a name, and extension should be (*. h). In that function there will be no main() function. In that file, we can put some variables, some functions etc.


2 Answers

You can do this by altering the C_INCLUDE_PATH environment variable, e.g.

C_INCLUDE_PATH=~/include
export C_INCLUDE_PATH

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++.

like image 69
Rafe Kettler Avatar answered Oct 05 '22 09:10

Rafe Kettler


had to use a whole set of flags to get this working on El Capitan:

export DYLD_LIBRARY_PATH=/usr/local/include
export CPPFLAGS="-I/usr/local/include/snappy-c.h"
export CFLAGS="-I/usr/local/include/snappy-c.h"
export CXXFLAGS="-I/usr/local/include/snappy-c.h"
export LDFLAGS="-L/usr/local/lib"
like image 36
MrE Avatar answered Oct 05 '22 09:10

MrE