143

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 header files from that library that were included. My bad.

Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
mrswmmr
  • 2,081
  • 5
  • 21
  • 23
  • 1
    Possible duplicate of [How to make g++ search for header files in a specific directory?](https://stackoverflow.com/questions/12654013/how-to-make-g-search-for-header-files-in-a-specific-directory) – phuclv Mar 08 '18 at 07:09

3 Answers3

248

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:

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
7

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")
Kartik Javali
  • 327
  • 1
  • 4
  • 11
3

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.

zyy
  • 1,271
  • 15
  • 25