UNIX & GNU/Linux - gcc - Option -I for adding includes to the compilation

During the compilation step, we can add includes that will be added to the .o generated.

For that, we use the -I option (this is an uppercase "i", standing for "include") with the path to the includes we want to add.

Let's suppose we want to add personal includes previously added in a directory.
So it can be something like that:

gcc -c -o main.o main.c -I"/home/soft/library/personal/include"

With a Makefile, we can use this example:

## BadproG.com
## Makefile

## Variables
NAME            = myProg
SRC                = main.c
OBJ                = $(SRC:.c=.o)
INCLUDES    = -I"/home/soft/library/personal/include"
CPPFLAGS    = $(INCLUDES) -Wall -Werror -Wextra -pedantic -ansi
CC        = gcc

## Rules
$(NAME) : $(OBJ)
    $(CC) $(OBJ) -o $(NAME)
all     : $(NAME)
clean   :
    rm -f $(OBJ)
fclean  : clean
    rm -f $(NAME)
re    : fclean all
r    : re
    rm -f *~
    rm -f *.o

Notice that the CPPFLAGS variable is added automatically by the compiler during the compilation.
So be careful, even if you don't specify it explicitely, it will be used.
It means that this line:

 $(CC) $(OBJ) -o $(NAME)

is equal to:

 $(CC) $(OBJ) -o $(NAME) $(CPPFLAGS)

The -I option is also available for g++.
An easy way to adding includes. angel

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.