常用编译命令
g++ –c Hello.cc 编译文件,生成目标文件 Hello.o
g++ Hello.o –o abc 连接 并重命名为可执行文件 abc
g++ Hello.cc 编译连接一起,生成a.out
g++ Hello.cc –o hello 生成a.out并命名为hello
编译多个文件
定义头文件header.h,代码如下
int fact(int n);
int static_val();
int mabs(int);
函数模块,func.cpp,代码如下
#include "header.h"
int fact(int n)
{
int ret = 1;
while(n > 1)
ret *= n--;
return ret;
}
int static_val()
{
static int count = 1;
return ++count;
}
int mabs(int n)
{
return (n > 0) ? n : -n;
}
定义main.cpp
#include <iostream>
#include "header.h"
using namespace std;
int main()
{
int j = fact(5);
cout << "5! is " << j << endl;
for(int i=1; i<=5; ++i)
{
cout << static_val() << " ";
}
cout << endl;
cout << "mabs(-8) is " << mabs(-8) << endl;
return 0;
}
在同一个文件下编译,头文件起申明作用,只需要编译两个cpp文件
g++ -c func.cpp
g++ -c main.cpp
g++ main.o func.o -o test