介绍LNK2019的scons构建工具制作的Config Libs

Config Libs produced by scons construction tool introducing LNK2019

我们正在使用 SCons 构建我们的项目,包括生成静态库和 Visual Studio 项目文件(.sln、.vcxproj)等内容。在 SConstruct 文件中(写在 Python 中),我们指定:

CCFLAGS=['/Od','/Zi','/EHsc','/W3']
env=Environment(ENV=ENV,CPPPATH=include_path,CCFLAGS=CCFLAGS,MSVS_ARCH=arc,TARGET_ARCH=arc)   
lib=env.StaticLibrary(target=lib_file,source=lib_src_files)
proj=env.MSVSProject(target=name+env['MSVSPROJECTSUFFIX'],srcs=lib_src_files,incs=lib_header_files,buildtarget=lib,variant=build_type,auto_build_solution=0)

生成我们的VS工程文件和静态库。一切顺利,我们终于得到了我们的库。 但是当我们在其他自定义项目中将这些库用作第三方库时,它会产生 lnk2009 error:

error   1   error LNK2019: unresolved external symbol "public: virtual
__thiscall Physika::Vector<double,3>::~Vector<double,3>(void)" (??1?$Vector@N@Physika@@UAE@XZ),referenced in function
_tmain()    C:\Users\suitmyself\documents\visual studio 2010\Projects\Physika_config\Physika_config\vector3d_test.obj

error   2   error LNK2019: unresolved external symbol "public: __thiscall Physika::Vector<double,3>::Vector<double,3>(double,double,double)" (??0?$Vector@N@Physika@@QAE@NNN@Z),referenced in function 
_tmain()    C:\Users\suitmyself\documents\visual studio 2010\Projects\Physika_config\Physika_config\vector3d_test.obj

注意这里的VS工程是手动生成的。我们一定要包含路径(头文件)和库路径,库文件配置没有错误,库文件找对了。似乎 .obj 根本无法匹配库文件中的正确符号,导致此 lnk 错误。

然而奇怪的是,如果我们使用SCons构建我们的VS项目并像上面那样指定CCFLAGS,一切正常,没有检测到lnk错误并且正常生成EXE。

那么为什么会出现这个错误呢?

问题确实出在custom vs proj settings的设置上,有三种情况下config会通过linker:

1: In Release Mode, \MT specified
2: In Release Mode, \MTd specified, add "ITERATOR_DEBUG_LEVEL=0" in preprocessor definiton.
3: In Debug Mode, \MTd specified, add "ITERATOR_DEBUG_LEVEL=0" in preprocessor definiton.

注意在调试模式下,指定一个编译选项\MT不会通过链接器,具体原因还不清楚,但是如果你这样做是行不通的。(一些LNK错误2001和2005发生)。

似乎 ITERATOR_DEBUG_LEVEL 被 Scons 默认为 0,相比之下,发布模式下的默认值是 0,调试模式下的默认值是 2。另外,我怀疑 \MTd 是由 Scons 默认设置的。任何使用 Scons 的用户都应该非常小心这一点。