Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I include a path to libraries in g++

Tags:

path

g++

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.

like image 958
mrswmmr Avatar asked Sep 26 '22 16:09

mrswmmr


People also ask

How do I link a library in Makefile?

If you have a Makefile project, you're responsible of the maintenance of the Makefile. To link the library in, you need to add it to the files to link at the linker command, which may be a g++, gcc or ld command line.

What is G ++ command?

g++ command is a GNU c++ compiler invocation command, which is used for preprocessing, compilation, assembly and linking of source code to generate an executable file.

Where are gcc library files located?

The C standard library itself is stored in '/usr/lib/libc.

Where does g ++ look for header files?

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.


2 Answers

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:

  • 3.16 Options for Directory Search
like image 226
Ernest Friedman-Hill Avatar answered Oct 21 '22 09:10

Ernest Friedman-Hill


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")
like image 6
Kartik Javali Avatar answered Oct 21 '22 09:10

Kartik Javali