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.
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.
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.
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.
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'.
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
There are three different things you need to investigate further:
LIBRARY_PATH
The value of
LIBRARY_PATH
is a colon-separated list of directories, much likePATH
. When configured as a native compiler, GCC tries the directories thus specified when searching for special linker files, if it can't find them usingGCC_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).
ldconfig
).And, most importantly:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With