Add included paths and a shared library for g ++ on an ongoing basis

I am trying to compile a .cpp file with g ++ in a 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 ++ all the time so I don't have to enter them every time. And I also want it to be applied to other applications.

I am using ubuntu.

Can anyone help me out? Thank you so much in advance.

+4
source share
3 answers

The best, most flexible way to do this is through the build system, using Make or CMake or something similar. But there is a serious learning curve. Now it may be easier to just create a script file to run the same commands that you successfully used from the command line.

I assume you are using a bash shell. You can simply edit the file - name it "compile.bash". In the first line of the file, enter "#! / Bin / bash". This tells the system to interpret this file as a bash script file. Then, on one or more of the following lines, enter the commands that you just indicated in your question, just like you used them before. Save the file. Then run this command from the command line: "chmod + x compile.bash" (without quotes). Make sure the new file is in the directory from which you are compiling, and you can simply type: "compile.bash" instead of the long command line that you used 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 
+2
source

There are three different questions that you need to explore further:

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

LIBRARY_PATH

LIBRARY_PATH is a colon-separated list of directories similar to PATH . When it is configured as its own compiler, GCC tries to specify the specified directories when searching for special linker files if they cannot find them using GCC_EXEC_PREFIX . GCC binding also uses these directories when searching for regular libraries for the -l option (but directories specified with -L come first).

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

And the most important thing:

  • What is a software creation tool or Makefile . To do this, you can go to the Scons page, CMake, or the GNU Make page. In short, each option provides you with tools to describe how to create your software, and then actually builds it with a simple command ( scons , cmake or make , depending on the system you choose).

So, overall, I have no answer to your question. I can only advise you to study them.

+2
source

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

 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 

It might be wise to find a gnu make tutorial for yourself so you can better understand this example.

Now, instead of manually launching the command, you can simply do make.

+2
source

Source: https://habr.com/ru/post/1502228/


All Articles