74

I have the following code in a sample file:

#include "SkCanvas.h"
#include "SkDevice.h"
#include "SkGLCanvas.h"
#include "SkGraphics.h"
#include "SkImageEncoder.h"
#include "SkPaint.h"
#include "SkPicture.h"
#include "SkStream.h"
#include "SkWindow.h"

However, this code is located in various folders within /home/me/development/skia (which includes core/ animator/ images/ ports/ svg/ and a lot more.)

How can I make GCC recognize this path?

CharlesB
  • 86,532
  • 28
  • 194
  • 218
Mark
  • 898
  • 1
  • 6
  • 9

3 Answers3

104

Try gcc -c -I/home/me/development/skia sample.c.

Alberto Chiusole
  • 2,204
  • 2
  • 16
  • 26
Tim Gilbert
  • 5,721
  • 5
  • 33
  • 28
  • 2
    Glad to see this answer here. Another point worth mentioning would be that when you have many ".c" source files, it's necessary to specify each and every one of them in the commandline itself. You can't just do something like a -I to specify that all source files are in a certain directory. – Nav Sep 06 '11 at 06:20
  • 4
    If the header is in the same directory as the source, do you need a special include? I can't get my code to compile either way, and I'm not sure what the problem is – CodyBugstein Dec 08 '13 at 16:46
  • 1
    According to [this answer](https://stackoverflow.com/a/510151/1403102) to a similar question, `gcc` would not search the subdirectories for the different header files automatically. Instead, `pkg-config` could produce the proper `-I` option? – Robert Schwarz Sep 25 '17 at 14:38
  • 1
    What's the difference between `-I` and `-L`? – falsePockets Oct 15 '18 at 00:31
  • @falsePockets The `-L` flag tells GCC to include a library. For example, `gcc -c -c a.c` will include `libmath`. The `-I` flags tells GCC to include files from a different directory. – Ed The ''Pro'' Jan 21 '19 at 10:39
  • 1
    @EdwinPratt Perhaps you meant to say that `-L` tells GCC where to *look* for binary libraries to include (which are specified with `-l`). And `-I` tells GCC where to look for header files to include. – Aviv Cohn Mar 31 '20 at 11:58
36

The -I directive does the job:

gcc -Icore -Ianimator -Iimages -Ianother_dir -Iyet_another_dir my_file.c 
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Reginaldo
  • 897
  • 5
  • 12
  • To anyone needing this: it looks like you can include directories with spaces in them with quotes: `-I"some path/with spaces"`. – Gabriel Staples Apr 26 '22 at 00:32
9

Using environment variable is sometimes more convenient when you do not control the build scripts / process.

For C includes use C_INCLUDE_PATH.

For C++ includes use CPLUS_INCLUDE_PATH.

See this link for other gcc environment variables.

Example usage in MacOS / Linux

# `pip install` will automatically run `gcc` using parameters
# specified in the `asyncpg` package (that I do not control)

C_INCLUDE_PATH=/home/scott/.pyenv/versions/3.7.9/include/python3.7m pip install asyncpg

Example usage in Windows

set C_INCLUDE_PATH="C:\Users\Scott\.pyenv\versions\3.7.9\include\python3.7m"

pip install asyncpg

# clear the environment variable so it doesn't affect other builds
set C_INCLUDE_PATH=
Scott P.
  • 1,054
  • 11
  • 12