gcc中-c和-o如何在一起使用

来源:互联网 发布:db数据库 编辑:程序博客网 时间:2024/06/06 09:44

针对c++中的工程问题在vim中用gcc编写的问题

例如有两个.cpp文件,需要编译,链接这两个文件。那么如何做呢?

//file1.cpp

#include <iostream>using namespace std;int add(int x,int y);int main(){int x,y,sum;cout<<"Enter two numbers:"<<endl;cin>>x;cin>>y;sum=add(x,y);cout<<"The sum is:"<<endl;cout<<sum<<endl;return 0;}

//file2.cpp

int add(int a,int b){int c;c=a+b;return c;}

在终端中输入以下代码

<pre name="code" class="plain">g++ -c file1.cppg++ -c file2.cpp<pre name="code" class="plain">g++ -o file file1.o file2.o

这里的file是链接生成的可执行文件。

输入:

./file
即可运行文件了。

0 0