Scons编译脚本

scons类似于makefile,但是比makefile强大的多。
scons语法简单,完全使用python语言就可以进行编译脚本编写和编译一些大的工程。

 

muhe@muhe-HP:~/test_scons$ ls
hello.c SConstruct

#include <stdio.h>
int main()
{
    printf("111\n");
    return 0;
}

SConstruct

Program('me', Glob('*.c'))

muhe@muhe-HP:~/test_scons$ scons -Q
gcc -o hello.o -c hello.c
gcc -o me hello.o
muhe@muhe-HP:~/test_scons$ ls
hello.c hello.o me SConstruct
muhe@muhe-HP:~/test_scons$ ./me
111
muhe@muhe-HP:~/test_scons$ scons -c
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Cleaning targets ...
Removed hello.o
Removed me
scons: done cleaning targets.

 

Scons命令
scons:进行代码文件编译
scons -Q:进行代码文件编译,不显示Scons内部操作打印的信息,只显示编译信息
scons -c:清除编译中间文件和可执行文件

Scons编译脚本
Scons对应的编译脚本名称为SConstruct,就如同make对应的编译脚本为makefile

SConstruct脚本编写

编译函数
Program():执行编译操作,生成可执行文件
Library():执行编译操作,生成静态库
StaticLibrary():执行编译操作,生成静态库
SharedLibrary():执行编译操作,生成动态库
Environment():编译环境

编译参数
target,生成的执行文件名字
source,编译文件
LIBS,依赖库
LIBPATH,依赖库路径,有环境变量的可不添加,针对用户库或第三方库
CPPPATH,头文件路径
CCFLAGS,编译参数

其他函数
Split():将字符串分隔为列表
Glob('*.cpp'):加入所有文件

编译参数可以以任意顺序加入编译函数,编译函数依据编译参数对代码文件进行编译。

编译的时候想添加上-g -O2 -Wall参数
使用环境变量
env=Environment(CCFLAGS = ['-g','-O3','-Wall'])  
生成程序的时候使用env.Program(...)即可
例如:
env=Environment(CCFLAGS = ['-g','-O3','-Wall'])  
env.Program('程序名', Glob('*.cpp'))

 

指定程序编译过程中需要查找的头文件路径
使用关键字CPPPATH
例如:
Program('hello','hello.c', CPPPATH = ['include', '/home/project/inc'])
生成hello程序的时候hello.c可能引用了其他的头文件,在编译hello.c的时候会查找./include和/home/project/inc下是否有需要的头文件

 

 一个简单例子

将文件放在当前目录,头文件放在上级目录。

str = Split('main.cpp display.cpp')
Program('main',str,CPPPATH = '../',LIBS = 'pthread')

一个复杂项目的例子

TARGETNAME = 'server'  
LIB = Split('jsoncpp logger ACE basetool tinyxml pthread dl rt')
FILELIST = Glob('*.cpp')
HEADFILEPATH = Split('../../common/include ../../3rd/ACE_wrappers ../../3rd/include/')
LIB_DIR = Split('../../3rd/lib ../../common/lib')
Program(target = TARGETNAME,source = FILELIST,LIBS = LIB,CPPPATH = HEADFILEPATH,LIBPATH = LIB_DIR)

 

posted @ 2019-08-15 11:28  牧 天  阅读(1480)  评论(0)    收藏  举报