构建第一个程序
使用scons构建hello程序
hello.c 内容
#include <stdio.h>
int main(void)
{
printf("hello\r\n");
return 0;
}
SConstruct
Program('hello.c')
在命令窗口中执行 scons
可以自动构建程序,输出如下:
admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day1
$ scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gcc -o hello.o -c hello.c
gcc -o hello.exe hello.o
scons: done building targets
在win10上可以看到在当前目录下,会一个 hello.exe
的文件生成。执行 hello.exe
可以在命令窗口看到打印输出。 并且可以看到中间文件 hello.o
的输出。
在 linux
上,可能会输出其他的文件,都是可以使用的。下面如没有特殊提起,则环境默认是 win10 。
admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day1
$ ls
SConstruct hello.c hello.exe hello.
admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day1
$ hello.exe
hello
编译目标文件(.o)
修改 SConstruct
内容
Object('hello.c')
在命令窗口执行 scons
,可以看出只生成了目标文件 hello.o
,没有 hello.exe
说明指令起作用了。
admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day1
$ scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gcc -o hello.o -c hello.c
scons: done building targets.
admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day1
$ ls
SConstruct hello.c hello.o
清除构建后的文件
执行 scons -c
可以将构建过程的中间文件清除。
admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day1
$ scons -c
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Cleaning targets ...
Removed hello.o
scons: done cleaning targets.
admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day1
$ ls
SConstruct hello.c