Details on the basic use of linux c++ compiler g++

  • 2020-05-12 02:53:31
  • OfStack

linux c++ compiler g++ basic use

g++ is the compiler of c++ under linux, which takes four steps to compile

1. Preprocess to generate.i files
2. The pre-processed file is not converted into assembly language, and the file is generated.s
3. Have compiled into object code (machine code) generated.o files
4. Connect the target code to generate an executable program

g++ compile c++ frequently used parameters:

-c

Compile only, not connect. For example: g++ -c helloworld.cpp

Only helloworld is generated.o is not connected

-o

Specify the output file name. For example: g++ -c helloworld. cpp-o abc. o
By default, helloworld.o is generated, and after -o abc.o is generated, abc.o is generated

-I

Appends the path to a packet header file. For example: g++ helloworld.cpp-I "/usr/helloworld/include"

-l

Small L, with 1 library attached, for example to use libabc.so on g++ helloworld.cpp-labc

-L

Add a path to a library, such as g++ hello.cpp-L "/usr/hello/lib" -labc

-shared

Generate dynamic library files, such as: g++ -shared hellp. cpp-o libhello. so

There are several problems when calling the dynamic library. Sometimes, the directory where the header file of the library is located is entered through include. The file of the library is booted through the parameter "-L" and the library name "-l" is specified. The fact that a Shared library is linked to a compiler does not mean that it can be found at execution time. So "-L" is not useful for execution, you need to specify the path of the Shared library. There are three methods:

Modify LD_LIBRARY_PATH to specify the path to the Shared library. LD_LIBRARY_PATH: this environment variable indicates the path where the dynamic connector can load the dynamic library. Use the following command at the terminal:


[root@localhost sharelib]# export LD_LIBRARY_PATH = .
[root@localhost sharelib]# export LD_LIBRARY_PATH = your lib dir export

b. Through/etc/ld. so. conf file to specify dynamic library catalog. Then run the ldconfig command to update the search for the Shared library's path. This usually solves the problem of libraries not being able to link for good.

c. Or copy the library file to /lib, then ldconfig is ok. This method is tricky and breaks the purity of the original library files, so it should not be the preferred method.

Modify/etc/ld. so. conf file, and then call/sbin ldconfig requires root permissions, if there is no root permissions, so can only adopt the method of output LD_LIBRARY_PATH.

The type of file under linux does not depend on its suffix, but 1 is generally:

.o, the target file, is equivalent to the.obj file in windows
.so is a Shared library, which is shared object for dynamic connection, similar to dll
a is a static library, which is many.o is combined in 1, which is used for static connection
.la is a set of Shared libraries automatically generated by libtool, which mainly records some configuration information.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: