compiling c++ with gcc/g++ compiler

I'm new to c++ and i want to compile my testprogram .
i have now 3 files "main.cpp" "parse.cpp" "parse.h"

how can i compile it with one command?


Compile them both at the same time and place result in a.out

$ g++ file.cpp other.cpp

Compile them both at the same time and place results in prog2

$ g++ file.cpp other.cpp -o prog2

Compile each separately and then link them into a.out

$ g++ -c file.cpp
$ g++ -c other.cpp
$ g++ file.o other.o

Compile each separately and then link them into prog2

$ g++ -c file.cpp
$ g++ -c other.cpp
$ g++ file.o other.o -o prog2

Typically you will use the make utility with a makefile. Here is a tutorial.

If you only want a simple compilation though you can just use gcc directly...

You don't specify the .h files as they are included by the .cpp files.

gcc a.cpp b.cpp -o program.out
./program.out


Using command line compilation is trivial for a simple basic program but it gets more difficult nor impossible when you'll try to compile a "real program". I mean not a big one a simple collection of various source files, libraries with some compiler options for example.

For this purposes there are various build systems and the one I recommend you is CMake. It's an open source cross-platform build system very powerful and ease to use. I use it every day and I think it's more easy to use than other ones like the AutoTools

More information on: www.cmake.org

链接地址: http://www.djcxy.com/p/85754.html

上一篇: C ++无法解析的外部符号

下一篇: 用gcc / g ++编译器编译c ++