2.2.9. Example: Building a C++ program with the GCC (compiling and linking in one step)
To build a minimal C++ program, use the following steps.
In this example, compiling and linking the code is done in one step.
Procedure
Create a directory
hello-cpp:$ mkdir hello-cppChange to the created directory:
$ cd hello-cppCreate file
hello.cppwith the following contents:#include <iostream> int main() { std::cout << "Hello, World!\n"; return 0; }Compile and link the code with
g++:$ g++ hello.cpp -o helloworldThis compiles the code, creates the object file
hello.o, and links the executable filehelloworldfrom the object file.Run the resulting executable file:
$ ./helloworldHello, World!Optional: Change back to the parent directory:
$ cd ..Optional: Remove the
hello-cppdirectory:$ rm -r hello-cpp