Home  >  Article  >  Operation and Maintenance  >  How to compile cpp files in linux?

How to compile cpp files in linux?

藏色散人
藏色散人Original
2019-05-21 15:56:558152browse

How to compile cpp files in linux?

If we have a written demo.cpp function, how do we compile the program and run the result under the Linux system?

We can execute the command:

g++ demo.cpp -o demo

to generate an executable program demo, and then execute the command:

./demo

to execute the program.

Similarly, if it is a .c file, you can execute the command:

gcc demo.c -o demo

In addition, during compilation, if we need to add the path of the header file and library file, you can refer to The following parameters:

-l: used to specify the library to be linked to by the program. The -l parameter is followed by the library name. The relationship between the library name and the real library file name can be explained as: if the library name is caffe, its library file name is libcaffe.so.

-L: The compiler searches for library files according to the path specified by -L. Generally, you can use -l after -L to specify multiple library files at one time. For example, .a (static library) and .so (dynamic library) under Linux.

-I: The compiler searches for header files according to the path specified by -I. (xxx.h)

For example, assuming I have written a demo.cpp file and need to use caffe-related files, I can execute the command:

g++ demo.cpp -o demo -I ~/caffe/include/ -D CPU_ONLY \
-I ~/caffe/.build_debug/src/ -L ~/caffe/build/lib -lcaffe

The above is the detailed content of How to compile cpp files in linux?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more