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

  1. Create a directory hello-cpp:

    $ mkdir hello-cpp
  2. Change to the created directory:

    $ cd hello-cpp
  3. Create file hello.cpp with the following contents:

    #include <iostream>
    
    int main() {
      std::cout << "Hello, World!\n";
      return 0;
    }
  4. Compile and link the code with g++:

    $ g++ hello.cpp -o helloworld

    This compiles the code, creates the object file hello.o, and links the executable file helloworld from the object file.

  5. Run the resulting executable file:

    $ ./helloworld
    Hello, World!
  6. Optional: Change back to the parent directory:

    $ cd ..
  7. Optional: Remove the hello-cpp directory:

    $ rm -r hello-cpp
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

关于红帽文档

Legal Notice

Theme

© 2026 Red Hat
返回顶部