Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add include paths and shared library for g++ permanently

I try to compile .cpp file with g++ in terminal:

g++ -o main main.cpp \
  -I/usr/include/glib-2.0 \
  -I/usr/include/json-glib-1.0 \
  -I/usr/lib/x86_64-linux-gnu/glib-2.0/include/ \
  -L/usr/lib/x86_64-linux-gnu -ljson-glib-1.0 -lglib-2.0

And it works.

But I want to add these .so libraries and include files for g++ permanently so that I don't need to type these every time. And I also want to make it apply for other applications.

I am using ubuntu.

Could anyone help me out? Thank you a lot in advance.

like image 556
William7777 Avatar asked Sep 15 '13 04:09

William7777


People also ask

What is include path in C++?

includePath An include path is a folder that contains header files (such as #include "myHeaderFile. h" ) that are included in a source file. Specify a list of paths for the IntelliSense engine to use while searching for included header files.

Where does g ++ look for libraries?

g++ does find the library in /usr/local/lib : once you do things properly and actually tell it the name of the library to look for. Also note that discussion of -L is a red herring for the same reason.

How are shared libraries loaded?

Shared libraries are the most common way to manage dependencies on Linux systems. These shared resources are loaded into memory before the application starts, and when several processes require the same library, it will be loaded only once on the system. This feature saves on memory usage by the application.


3 Answers

Here is a very basic example of a Makefile using pkg-config, which you should really use with glib anyway, takes a lot of the pain away:

CXXFLAGS += $(shell pkg-config --cflags glib-2.0) $(shell pkg-config --cflags json-glib-1.0)
LIBS     += $(shell pkg-config --libs glib-2.0) $(shell pkg-config --libs json-glib-1.0)

all: main

main: main.o
    $(CXX) $(CXXFLAGS) main.o -o main $(LIBS)

clean:
    rm -f main main.o

Might be wise to find yourself a gnu make tutorial, so that you can better understand this example.

Now instead of running your manually typed shell command, you can just do 'make'.

like image 178
goji Avatar answered Sep 21 '22 00:09

goji


The best, most flexible way to do this is via a build system, using Make or CMake or something similar. But there is a serious learning curve. It may be simpler for right now to just create a script file to run the same commands that you have successfully used from the command line.

I assume you are using the bash shell. You can just edit a file -- call it "compile.bash". At the first line of the file, type "#!/bin/bash". That tells the system to interpret this file as a bash script file. Then on one or more subsequent lines, type the commands you just provided in you question, exactly as you use them previously. Save the file. Then run this command from the command line: "chmod +x compile.bash" (without the quotes). Make sure that the new file is located in the directory that you compile from, and you can just type: "compile.bash" instead of the long command line you were using before.

Example file "compile.bash"

#!/bin/bash
g++ -o main main.cpp -I/usr/include/glib-2.0 -I/usr/include/json-glib-1.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include/ -L/usr/lib/x86_64-linux-gnu -ljson-glib-1.0 -lglib-2.0
like image 38
mikeTronix Avatar answered Sep 22 '22 00:09

mikeTronix


There are three different things you need to investigate further:

  • Environment variables affecting your compiler. Since you are using GCC, I can point out this page. In particular, you should read about:

LIBRARY_PATH

The value of LIBRARY_PATH is a colon-separated list of directories, much like PATH. When configured as a native compiler, GCC tries the directories thus specified when searching for special linker files, if it can't find them using GCC_EXEC_PREFIX. Linking using GCC also uses these directories when searching for ordinary libraries for the -l option (but directories specified with -L come first).

  • The manner in which your OS searches for shared, dynamic libraries. Since you are using Linux, I would recommend this page (discussing ldconfig).

And, most importantly:

  • What is a software construction tool or Makefile. For that you can refer to the Scons page, the CMake page, or the GNU Make page. Briefly, each option provides you with the means for you to describe how to build your software, and then actually building it using a simple command (scons, cmake, or make, depending on what system you chose).

So, all in all I don't have an answer to your question. I can only advice you to look into these.

like image 38
Escualo Avatar answered Sep 21 '22 00:09

Escualo