# tar zxvf scons-2.1.0.tar.gz
# tar zxvf jsoncpp-src-0.5.0.tar.gz
设定环境变量【关键】,绝对路径
# export MYSCONS=解压的路径
# export SCONS_LIB_DIR=$MYSCONS/engine
# cd jsoncpp-src-0.5.0
# python $MYSCONS/script/scons platform=linux-gcc
编译成功后,在在jsoncpp-src-0.5.0/libs/目录下找到相应的*.a和*.so
可以修改为:mv libjson_linux-gcc-4.4.6_libmt.so libjson.so
顶
0
踩
㈡ scons 怎么忽视某个.c文件
介绍,scons用的是python语法。需要安装python和scons后才能运行,能够跨平台。比较automake自动生成makefile文件,scons可以认为直接是make的功能,因为只需要执行scons命令就等于执行了make的功能。
现在有一个hello.c的文件。
新建一个SConstruct文件,是一个python脚本文件。
Program('hello.c') 编译hello.c并生成.o文件和可执行文件
Object('hello.c') 编译hello.c但只生成生成.o文件
这两个方法都是python的method。
如果想执行clean操作,我们不需要再象makefile那样指名make clean语句,而是直接执行scons -c 或者scons -clean就可以。程序会根据SConstruct文件内容自动清除。
SConstruct的读取和执行顺序是彼此独立的,直接看以下例子。
SConstruct文件内容:
print "Calling Program('hello.c')"
Program('hello.c')
print "Calling Program('goodbye.c')"
Program('goodbye.c')
print "Finished calling Program()"
㈢ 怎样使用Scons生成 交叉编译的动态库
第一步,我先从简单的调用出发,定义了一个简单的函数,该函数仅仅实现一个整数加法求和:
LIBEXPORT_API int mySum(int a,int b){ return a+b;}
C# 导入定义:
public class RefComm
{
[DllImport("LibEncrypt.dll",
EntryPoint=" mySum ",
CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
public static extern int mySum (int a,int b);
}
在C#中调用测试:
int iSum = RefComm.mySum(,);
运行查看结果iSum为5,调用正确。第一步试验完成,说明在C#中能够调用自定义的动态链接库函数。
第二步,我定义了字符串操作的函数(简单起见,还是采用前面的函数名),返回结果为字符串:
LIBEXPORT_API char *mySum(char *a,char *b){sprintf(b,"%s",a); return a;}
C# 导入定义:
public class RefComm
{
[DllImport("LibEncrypt.dll",
EntryPoint=" mySum ",
CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
public static extern string mySum (string a, string b);
}
在C#中调用测试:
string strDest="";
string strTmp= RefComm.mySum("45", strDest);
运行查看结果 strTmp 为"45",但是strDest为空。我修改动态链接库实现,返回结果为串b:
LIBEXPORT_API char *mySum(char *a,char *b){sprintf(b,"%s",a) return b;}
修改 C# 导入定义,将串b修改为ref方式:
public class RefComm
{
[DllImport("LibEncrypt.dll",
EntryPoint=" mySum ",
CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
public static extern string mySum (string a, ref string b);
}
在C#中再调用测试:
string strDest="";
string strTmp= RefComm.mySum("45", ref strDest);
运行查看结果 strTmp 和 strDest 均不对,含不可见字符。再修改 C# 导入定义,将CharSet从Auto修改为Ansi:
public class RefComm
{
[DllImport("LibEncrypt.dll",
EntryPoint=" mySum ",
CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]
public static extern string mySum (string a, string b);
}
在C#中再调用测试:
string strDest="";
string strTmp= RefComm. mySum("45", ref strDest);
运行查看结果 strTmp 为"45",但是串 strDest 没有赋值。第二步实现函数返回串,但是在函数出口参数中没能进行输出。再次修改 C# 导入定义,将串b修改为引用(ref):
public class RefComm
{
[DllImport("LibEncrypt.dll",
EntryPoint=" mySum ",
CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]
public static extern string mySum (string a, ref string b);
}
运行时调用失败,不能继续执行。
第三步,修改动态链接库实现,将b修改为双重指针:
LIBEXPORT_API char *mySum(char *a,char **b){sprintf((*b),"%s",a); return *b;}
C#导入定义:
public class RefComm
{
[DllImport("LibEncrypt.dll",
EntryPoint=" mySum ",
CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]
public static extern string mySum (string a, ref string b);
}
在C#中调用测试:
string strDest="";
string strTmp= RefComm. mySum("45", ref strDest);
运行查看结果 strTmp 和 strDest 均为"45",调用正确。第三步实现了函数出口参数正确输出结果。
第四步,修改动态链接库实现,实现整数参数的输出:
LIBEXPORT_API int mySum(int a,int b,int *c){ *c=a+b; return *c;}
C#导入的定义:
public class RefComm
{
[DllImport("LibEncrypt.dll",
EntryPoint=" mySum ",
CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]
public static extern int mySum (int a, int b,ref int c);
}
在C#中调用测试:
int c=0;
int iSum= RefComm. mySum(,, ref c);
运行查看结果iSum 和c均为5,调用正确。
经过以上几个步骤的试验,基本掌握了如何定义动态库函数以及如何在 C# 定义导入,有此基础,很快我实现了变长加密函数在 C# 中的调用,至此目标实现。
三、结论
在 C# 中调用 C++ 编写的动态链接库函数,如果需要出口参数输出,则需要使用指针,对于字符串,则需要使用双重指针,对于 C# 的导入定义,则需要使用引用(ref)定义。
对于函数返回值,C# 导入定义和 C++ 动态库函数声明定义需要保持一致,否则会出现函数调用失败。定义导入时,一定注意 CharSet 和 CallingConvention 参数,否则导致调用失败或结果异常。运行时,动态链接库放在 C# 程序的目录下即可,我这里是一个 C# 的动态链接库,两个动态链接库就在同一个目录下运行。
㈣ 新人求教 驱动源码编译安装
1、安装scons
(1) 下载python2.7, 使用x86_32位,因为scons只有32位安装包可用;
(2) 下载scons2.3.0;
(3) 安装python 和 scons, 将C:\Python27\Scripts写入PATH;
(4) 下载安装pywin32 ,It is recommended you install pywin32 if you want to do parallel builds (scons -j)
2、安装boost库(1.49版本).
解压后双击bootstrap.bat,生成bjam.exe后,cd到目录c:\boost下,(将boost_1_49更名为boost了)编译boost。
编译命令:C:\boost>bjam variant=release --with-filesystem --with-thread --with-date_time --with-program_options threading=multi toolset=msvc-10.0 link=static runtime-link=static address-model=32
这是使用VS2010环境编译的release版本,编译完成后,生成C:\boost\stage\lib文件夹,下面有6个lib库:
如果要编译成debug版本,使用命令:bjam variant=debug --with-filesystem --with-thread --with-date_time --with-program_options threading=multi toolset=msvc-10.0 link=static runtime-link=static address-model=32
编译完成后,生成C:\boost\stage\lib文件夹,下面有10个lib库和dll:
此处为MongoDB文档中对于编译boost库的要求原文:
When using bjam, MongoDB expects
variant=debug for debug builds, and variant=release for release builds
threading=multi
link=static runtime-link=static for release builds
address-model=64 for 64 bit(64位的话,把32换为64)。link=static runtime-link=static,boost需要编译成静态库,因为mongodb只会去链接boost的静态库
address-model=64在win7 64环境下此项必须,不加在编译mongodb的c++ client时会出现链接错误。
3、下载mongo2.4.6源码 http://www.mongodb.org/downloads官网下载
编译Mongoclient.lib
cmd命令提示符下,cd到解压后的文件目录,例如我放在了E盘,E:\mongodb-src-r2.4.6,输入命令:
scons –-dd --32 mongoclient.lib // build C++ client driver library
Add --64 or --32 to get the 64- and 32-bit versions, respectively. Replace --release with --dd to build a debug build.
编译后在mongodb\build\win32\32\dd\client_build\生成mongoclient.lib.
4、测试程序
就用Mongodb自带的例子吧,使用VS2010打开E:\mongodb-src-r2.4.6\src\mongo\client\examples中的simple_client_demo.vcxproj,编译,会提示生成simple_client_demo.sln,保存。
使用debug模式,配置工程环境:打开工程->属性,配置Configuration Properties下的VC++ Directories,头文件路径添加C:\boost,Lib库路径添加boost的lib,以及mongodb client的lib:
C:\boost\stage\lib
E:\mongodb-src-r2.4.6\build\win32\32\dd\client_build
进入C/C++下面的Code Generation,将Runtime Library设置为Multi-threaded Debug (/MTd)
进入Linker下面的Input,设置Additional Dependencies,添加ws2_32.lib,psapi.lib,Dbghelp.lib,mongoclient.lib
将E:\mongodb-src-r2.4.6\build\win32\32\dd\mongo\base下生成的error_codes.h和error_codes.cpp文件,拷贝到E:\mongodb-src-r2.4.6\src\mongo\base目录下。
ok,编译、运行.
5、问题解决
error LNK2038: mismatch detected for '_MSC_VER': value '1700' doesn't match value '1600' in error_codes.obj
1>mongoclient_d.lib(dbclient.obj) : error LNK2038: mismatch detected for '_MSC_VER': value '1700' doesn't match value '1600' in error_codes.obj
1>mongoclient_d.lib(assert_util.obj) : error LNK2038: mismatch detected for '_MSC_VER': value '1700' doesn't match value '1600' in error_codes.obj
1>mongoclient_d.lib(jsobj.obj) : error LNK2038: mismatch detected for '_MSC_VER': value '1700' doesn't match value '1600' in error_codes.obj
1>mongoclient_d.lib(status.obj) : error LNK2038: mismatch detected for '_MSC_VER': value '1700' doesn't match value '1600' in error_codes.obj
1>mongoclient_d.lib(mutexdebugger.obj) : error LNK2038: mismatch detected for '_MSC_VER': value '1700' doesn't match value '1600' in error_codes.obj
VS的版本不匹配,lib是在更高级的版本中编译生成的,而使用的时候,是在低级版本中使用的,所以出现了不匹配的错误。例如,我在VS2010 SP1和VS2012的环境下编译的,而使用是在VS2010上使用,所以在编译时,出现了以上问题。
1>mongoclient.lib(stacktrace.obj) : error LNK2001: unresolved external symbol __imp_SymCleanup
1>mongoclient.lib(stacktrace.obj) : error LNK2001: unresolved external symbol __imp_SymGetMoleInfo64
1>mongoclient.lib(stacktrace.obj) : error LNK2001: unresolved external symbol __imp_SymInitialize
1>mongoclient.lib(stacktrace.obj) : error LNK2001: unresolved external symbol __imp_StackWalk64
1>mongoclient.lib(stacktrace.obj) : error LNK2001: unresolved external symbol __imp_SymFromAddr
在工程依赖库中添加Dbghelp.lib
其它问题,看看你手头的编译器、编译出来的boost库版本、mongoclient.lib的版本,是否对应好了。
㈤ 请教Scons python 编译问题
一、概述
scons是一个Python写的自动化构建工具,和GNU make相比优点明显:
1、移植性:python能运行的地方,就能运行scons
2、扩展性:理论上scons只是提供了python的类,scons使用者可以在这个类的基础上做所有python能做的事情。比如想把一个已经使用了Makefile大型工程切换到scons,就可以保留原来的Makefile,并用python解析Makefile中的编译选项、源/目标文件等,作为参数传递给scons,完成编译。
3、智能:Scons继承了autoconf/automake的功能,自动解析系统的include路径、typedef等;“以全局的观点来看所有的依赖关系”
二、scons文件
scons中可能出现的文件:
SConstruct,Sconstruct,sconstruct,SConscript
scons将在当前目录以下次序 SConstruct,Sconstruct,sconstruct 来搜索配置文件,从读取的第一个文件中读取相关配置。
在配置文件SConstruct中可以使用函数SConscript()函数来定附属的配置文件。按惯例,这些附属配置文件被命名为”SConscript”,当然也可以使用任意其它名字。
三、scons的命令行参数
scons: 执行SConstruct中脚本
scons -c clean
scons -Q 只显示编译信息,去除多余的打印信息
scons -Q --implicit-cache hello 保存依赖关系
--implicit-deps-changed 强制更新依赖关系
--implicit-deps-unchanged 强制使用原先的依赖关系,即使已经改变
四、SConstruct提供的方法
1、Program:生成可执行文件
Program('hello.c') 编译hello.c可执行文件,根据系统自动生成(hello.exe on Windows; hello on POSIX)
Program('hello','hello.c') 指定Output文件名(hello.exe on Windows; hello on POSIX)
Program(['hello.c', 'file1.c', 'file2.c']) 编译多个文件,Output文件名以第一个文件命名
Program(source = "hello.c",target = "hello")
Program(target = "hello" , source = "hello.c")
Program('hello', Split('hello.c file1.c file2.c')) 编译多个文件
Program(Glob("*.c"))
src = ["hello.c","foo.c"];Program(src)
2、Object:生成目标文件
Object('hello.c') 编译hello.c目标文件,根据系统自动生成(hello.obj on Windows; hello.o on POSIX)
3、Library:生成静态/动态库文件
Library('foo', ['f1.c', 'f2.c', 'f3.c']) 编译library
SharedLibrary('foo', ['f1.c', 'f2.c', 'f3.c']) 编译 shared library
StaticLibrary('bar', ['f4.c', 'f5.c', 'f6.c']) 编译 static library
库的使用:
Program('prog.c', LIBS=['foo', 'bar'], LIBPATH='.') 连接库,不需加后缀或是前缀
4、SourceSignatures:判断源文件是否修改
SourceSignatures('MD5') 根据内容是否改变,默认方式
SourceSignatures('timestamp') 根据修改时间
5、TargetSignatures:判断目标文件是否改变
TargetSignatures('build') 根据编译结果
TargetSignatures('content') 根据文件内容,如果只是加了句注释,将不会被重新编译
6、Ignore:忽略依赖关系
Ignore(hello, 'hello.h') 忽略某个依赖关系
7、Depends:明确依赖关系
Depends(hello, 'other_file') 明确依赖关系
8、SConscript:scons的配置文件。
源文件的目录结构如下:
src:
| SConstruct
|test.cpp
| mA(目录):
| SConscript
| func.cpp
其中test.cpp为主文件,中调用func.cpp中定义的函数
SConstruct内容如下:
[cpp]view plain
subobj=SConscript(['mA/SConscript'])
obj=subobj+Object(Glob("*.cpp"))
Program("test",list(obj))
[cpp]view plain
obj=Object(Glob("*.cpp"))
Return("obj")
10.env:环境变量
环境变量用于设置在编译过程中的各种参数,可以用下面的SConstruct打印环境变量的所有信息(实际上env就是一个python字典)
可以使用如下的SConstruct查看环境变量的内容:
[cpp]view plain
env=Environment()
dict=env.Dictionary()
keys=dict.keys()
keys.sort()
forkeyinkeys:
print"constructionvariable='%s',value='%s'"%(key,dict[key])
环境变量的复制:
env = Environment(CC = 'gcc')
opt = env.Clone(CCFLAGS = '-O2')
dbg = env.Clone(CCFLAGS = '-g')
环境变量的替换:
env = Environment(CCFLAGS = '-DDEFINE1')
env.Replace(CCFLAGS = '-DDEFINE2')
env.Program('foo.c')
环境变量的输入输出:用于统一多目录源文件的编译选项,如:
src:
| SConstruct
| libstlport.a
| test.cpp
| include(目录):
| foo.h
| mA(目录):
| SConscript
|func.cpp
test.cpp和mA/func.cpp都引用了include/foo.h,test.cpp调用了mA/func.cpp的功能函数,其中include/foo.h中定义了一个包含string类型的类。
SConstruct如下:
[cpp]view plain
env=Environment()
flags=env.ParseFlags(['-pthread-I/usr/include/stlport','-L.'])
env.MergeFlags(class_flags)
subobj=SConscript(['mA/SConscript'])
obj=subobj+env.Object(Glob("*.cpp"))
env.Program("test",list(obj),LIBS=['libstlport.a'])
mA/SConscrip如下:
[cpp]view plain
obj=Object(Glob("*.cpp"))
Return("obj")
不出意外的话上边的工程编译可以通过,但是运行的时候会Aborted。因为test.cpp,mA/func.cpp都使用了包含string类型的那个类,但是由于编译环境的不同,test.cpp认为string变量的大小是24字节, mA/func.cpp认为string变量的大小是4个字节(libstlport.a捣的鬼)。
解决问题的办法就是环境变量输出,修改SConstruct和mA/SConscript如下:
SConstruct:
[cpp]view plain
env=Environment()
flags=env.ParseFlags(['-pthread-I/usr/include/stlport','-L.'])
env.MergeFlags(class_flags)
Export('env')
subobj=SConscript(['mA/SConscript'],exports='env')
obj=subobj+env.Object(Glob("*.cpp"))
env.Program("test",list(obj),LIBS=['libstlport.a'])
㈥ 请教Scons python 编译问题
哈哈,解决问题的方法不可思议,我看了半天main.py后,就把那2个代码SConstruct 和SConscript拷到python的根目录下,居然编译一大半,不过还有问题,请帮助我;提示如下
E:\svn_rt-thread\rt-thread\bsp\mini2440>scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
scons: building associated VariantDir targets: build\bsp
armcc -o build\bsp\application.o -c --device DARMSS9 --apcs=interwork --diag_sup
press=870 -I"C:\Keil\ARM\BIN30/ARM/RV31/INC" -g -O0 -IE:\svn_rt-thread\rt-thread
\include -IE:\svn_rt-thread\rt-thread\libcpu\arm\s3c24x0 -IE:\svn_rt-thread\rt-t
hread\libcpu\arm\common -I. -IE:\svn_rt-thread\rt-thread -IE:\svn_rt-thread\rt-t
hread\components\finsh -IE:\svn_rt-thread\rt-thread\components\dfs -IE:\svn_rt-t
hread\rt-thread\components\dfs\include -IE:\svn_rt-thread\rt-thread\components\r
tgui\include -IE:\svn_rt-thread\rt-thread\components\rgtui\common -IE:\svn_rt-th
read\rt-thread\components\rtgui\server -IE:\svn_rt-thread\rt-thread\components\r
tgui\widgets application.c
'armcc' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
scons: *** [build\bsp\application.o] Error 1
scons: building terminated because of errors.
E:\svn_rt-thread\rt-thread\bsp\mini2440>
以下图片