一、基本命令
c编译、运行:gcc -Wall -g -o ~/Desktop/test ~/Desktop/test.c
./Desktop/test
c++编译、运行:
g++ ~/Desktop/c++/test.cpp -o ~/Desktop/c++/test.exe
./Desktop/c++/test.exe
注意具体目录
二、添加警告提示
c++:g++ -Wall ~/Desktop/c++/test.cpp -o ~/Desktop/c++/test.exe
三、强制支持c++11标准
c++: bogon:bin chen$ g++ -Wall -std=c++11 ~/Desktop/c++/test.cpp -o ~/Desktop/c++/test.exe
四、分离式编译
比如同一个文件夹喜有这么样3个文件:
test.cpp
#include "func.h"func.h
#include <iostream>
int main()
{
std::cout << "5! is " << fact(5) << std::endl;
std::cout << func() << std::endl;
std::cout << abs(-9.78) << std::endl;
}
int fact(int val);
int func();
template <typename T> T abs(T i)
{
return i >= 0 ? i : -i;
}
fact.cc
#include "func.h"以上节选自c++ Primer
#include <iostream>
int fact(int val)
{
if (val == 0 || val == 1)
return 1;
else
return val * fact(val - 1);
}
int func()
{
int n, ret = 1;
std::cout << "input a number: ";
std::cin >> n;
while (n > 1) ret *= n--;
return ret;
}
c++:
g++ ~/Desktop/c++/fact.cc -Wall -std=c++11 ~/Desktop/c++/test.cpp -o ~/Desktop/c++/test