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

How to compile cpp files in linux?

藏色散人
Release: 2019-06-04 15:28:43
Original
8227 people have browsed it

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
Copy after login

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

./demo
Copy after login

to execute the program.

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

gcc demo.c -o demo
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template