在 Ubuntu 命令行中,可以使用 GCC 编译器编译 C 代码并运行 'hello world' 程序。

编译步骤:

  1. 编写 C 代码:

    创建一个名为 test.c 的文件,并写入以下代码:

    #include <stdio.h>
    
    int main() {
        printf("Hello, world!\n");
        return 0;
    }
    
  2. 编译代码:

    使用 GCC 编译器编译代码,例如:

    aarch64-linux-gnu-gcc-11 test.c -o test -static
    

    这将生成一个名为 test 的可执行文件,并使用静态链接方式编译。

  3. 运行程序:

    在命令行中执行编译后的可执行文件:

    ./test
    

    您将看到输出:

    Hello, world!
    

其他选项:

  • 若未指定输出文件名,则默认生成名为 a.out 的可执行文件。

  • 可使用 -o 选项指定输出文件名,例如:

    aarch64-linux-gnu-gcc-11 test.c -o my_program -static
    
  • 使用 -static 选项进行静态链接,将所有库函数都链接到可执行文件,生成一个独立的可执行文件,不需要依赖外部库。

Ubuntu 命令行编译 C 代码并运行 'hello world' 指南

原文地址: https://www.cveoy.top/t/topic/oQdy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录