程序自动编译:
在vc中,点击“生成解决方案”就能生成解决方案;
在linux中使用Makefile,是一个脚本文件,和vc中生成解决方案差不多。
写如下代码:
other.h
void printOther();
other.cpp
#include <stdio.h>
#include "other.h"
void printOther() {
printf("printOther called\n");
}
main.cpp
#include "other.h"
#include <stdio.h>
int main() {
printf("main called\n");
printOther();
return 0;
}
运行截图如下:
方法:
1.创建一个文件叫Makefile
2.输入命令,根据Makefile中的指示,自动执行所有的步骤
如:make -f Makefile
make file文件如下:
helloworld:
g++ main.cpp other.cpp -o helloworld
如下图展示:
创建一个makefile文件:(使用touch Makefile或右键点击新建文件)
make命令会自动解析Makefile里面的内容
或 make -f Makefile
Makefile写法:
target:dependencies
<TAB>system command1
<TAB>system command2
<TAB>system command...
target:目标,
dependencies:依赖
<TAB>每行命令前必须插入一个TAB
system command:系统命令
当存在很多规则时,默认从第一条规则开始执行(只执行一条规则)
输入make命令时,同时显式指定要执行的那一条rule:
make clean
make -f Makefile clean
如下图:
因为是vs创建的,用Makefile把vc有关的东西删掉:
如下图:
Makefile如下:
helloworld:
g++ main.cpp other.cpp -o helloworld
clean:
rm -rf *.vcxproj *.sln *.filters