一点一滴学Linux

    返回首页    发表留言
本文作者:李德强
          第二节 编译C/C++程序
 
 

        首先来编写一个C程序:

vim c_test.c

#include <stdio.h>
int main(int argc, char **args)
{
        printf("Hello World!\n");
        return 0;
}

        保存并退出,使用gcc来对c_test.c进行编译:

gcc c_test.c -o c_test

        -o参数表示output文件,也就是执行gcc对其编译之后生成c_test可执行文件。再执行ls命令来查看当前目录下的文件列表:

ls -lh
-rwxrwxr-x. 1 lidq lidq 8.4K Jun 24 11:22 c_test
-rw-rw-r--. 1 lidq lidq   93 Jun 24 11:22 c_test.c

        可以看到gcc已经为我们生成了一个叫c_test的可执行文件。运行c_test文件:

./c_test
Hello World!

        再来编写一个C++程序:

vim cpp_test.cpp

#include <iostream>
using namespace std;
int main(int argc, char **args)
{
        cout << "Hello World!" << endl;
        return 0;
}

        保存并退出,使用g++命令对cpp_test.cpp进行编译:

g++ cpp_test.cpp -o cpp_test
ls -lh
-rwxrwxr-x. 1 lidq lidq 9.0K Jun 24 11:30 cpp_test
-rw-rw-r--. 1 lidq lidq  121 Jun 24 11:27 cpp_test.cpp

        运行cpp_test

./cpp_test
Hello World!

 

    返回首页    返回顶部
  看不清?点击刷新

 

  Copyright © 2015-2023 问渠网 辽ICP备15013245号