139

I am trying to include the path to extra libraries in my makefile, but I can't figure out how to get the compiler to use that path. so far I have:

g++ -g -Wall testing.cpp fileparameters.cpp main.cpp -o test

and I want to include the path to

/data[...]/lib

because testing.cpp includes files from that library. Also, I'm on a linux machine.

EDIT: Not a path to a library. Just to files that were included. My bad.

1

3 Answers 3

238

To specify a directory to search for (binary) libraries, you just use -L:

-L/data[...]/lib

To specify the actual library name, you use -l:

-lfoo  # (links libfoo.a or libfoo.so)

To specify a directory to search for include files (different from libraries!) you use -I:

-I/data[...]/lib

So I think what you want is something like

g++ -g -Wall -I/data[...]/lib testing.cpp fileparameters.cpp main.cpp -o test

These compiler flags (amongst others) can also be found at the GNU GCC Command Options manual:

12
  • 5
    Basically someplace before it's needed; the -I should come before the .cpp filenames, for example. May 26, 2011 at 15:36
  • 1
    Is it possible to make the compiler figuure that out, like add the library to a system variable?
    – Fred
    Mar 15, 2013 at 19:10
  • 7
    @Fred, yes, g++ understands a whole bunch of environment variables which can be used to control the include path, library path, etc. Mar 16, 2013 at 3:09
  • 1
    what's the difference between -L and -I (capital i, not lower L)?
    – Youda008
    Jul 25, 2015 at 19:52
  • 1
    @Youda008 Capital ell: search path for libraries. Capital eye: search path for include files. Small ell: name of library to link. Jul 25, 2015 at 20:35
6

In your MakeFile or CMakeLists.txt you can set CMAKE_CXX_FLAGS as below:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/path/to/your/folder")
2

Alternatively you could setup environment variables. Suppose you are using bash, then in ~/.bashrc, write

C_INCLUDE_PATH="/data/.../lib/:$C_INCLUDE_PATH" ## for C compiler
CPLUS_INCLUDE_PATH="/data/.../lib/:$CPLUS_INCLUDE_PATH" ## for Cpp compiler
export C_INCLUDE_PATH
export CPLUS_INCLUDE_PATH

and source it with source ~/.bashrc. You should be good to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.