7

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.

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
William7777
  • 71
  • 1
  • 1
  • 5
  • 2
    Might be time to learn how to create a simple makefile. – goji Sep 15 '13 at 04:09
  • Yup, makefiles are what you are definitely looking for here. http://stackoverflow.com/questions/1484817/how-do-i-make-a-simple-makefile-gcc-unix – Steven Huang Sep 15 '13 at 04:27

3 Answers3

3

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'.

goji
  • 6,911
  • 3
  • 42
  • 59
2

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
mikeTronix
  • 584
  • 9
  • 16
2

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.

Escualo
  • 40,844
  • 23
  • 87
  • 135