利用mongodb c++ driver来编译 静态链接库,报错:
version.obj : error LNK2001: unresolved external symbol "void __cdecl boost::thr
ow_exception(class std::exception const &)" (?throw_exception@boost@@YAXABVexcep
tion@std@@@Z)
等
解决方法是修改SConstruct:
添加 env.AppendUnique(CXXFLAGS=Split("/EHsc"));
这个选项为编译动态链接库,应该去掉
以下为修改后的SConstruct,仅供参考
引用
# scons file for MongoDB c++ client library and examples
import os
# options
AddOption( "--extrapath",
dest="extrapath",
type="string",
nargs=1,
action="store",
help="comma separated list of add'l paths (--extrapath /opt/foo/,/foo) static linking" )
AddOption( "--prefix",
dest="prefix",
type="string",
nargs=1,
action="store",
default="/usr/local",
help="installation root" )
AddOption( "--release", dest="release", type="string", nargs=0, action="store", help="release build" )
env = Environment( MSVS_ARCH=None )
debug = False
def has_option( name ):
x = GetOption( name )
if x is None:
return False
return True
release = has_option( "release" )
if release:
print("release")
debug = False
else:
print("debug")
debug = True
def addExtraLibs( s ):
for x in s.split(","):
if os.path.exists( x ):
env.Append( CPPPATH=[ x + "/include" ] )
env.Append( LIBPATH=[ x + "/lib" ] )
env.Append( LIBPATH=[ x + "/lib64" ] )
#add boost
boostDir = "E:/boost"
env.Append( CPPPATH=[ boostDir ] )
env.Append( LIBPATH=[ boostDir + "/lib" ] )
#add pcre
env.Append( CPPPATH=[ "./third_party/pcre-7.4" ] )
env.Append( LIBPATH=[ "./"] )
#set unicode
env.Append( CPPDEFINES=[ "_UNICODE" ] )
env.Append( CPPDEFINES=[ "UNICODE" ] )
#env.Append( CPPFLAGS="Ehsc" )
env.AppendUnique(CXXFLAGS=Split("/EHsc"));
if GetOption( "extrapath" ) is not None:
addExtraLibs( GetOption( "extrapath" ) )
env.Append( CPPPATH=[ "mongo/" ] )
env.Append( CPPDEFINES=[ "_SCONS" , "MONGO_EXPOSE_MACROS" ] )
nix = False
linux = False
if "darwin" == os.sys.platform:
addExtraLibs( "/opt/local/" )
nix = True
elif "linux2" == os.sys.platform or "linux3" == os.sys.platform:
nix = True
linux = True
if nix:
env.Append( CPPFLAGS=" -O3" )
env.Append( LIBS=["pthread"] )
if linux:
env.Append( LINKFLAGS=" -Wl,--as-needed -Wl,-zdefs " )
#add libs: thread, filesystem, program_options
boostLibs = [ "thread" , "filesystem", "program_options" ]
#for lib in boostLibs:
#env.Append( LIBS=[ "boost_%s-vc100-mt-1_48" % lib] )
#print("boost_%s-vc100-mt-1_48" % lib)
conf = Configure(env)
if debug:
env.Append( CPPDEFINES=[ "_DEBUG" ] )
env.Append( CPPFLAGS=" /MTd " )
for lib in boostLibs:
if not conf.CheckLib("boost_%s-vc100-mt-gd-1_48" % lib):
conf.CheckLib("boost_%s-mt-1_48" % lib)
else:
env.Append( CPPDEFINES=[ "NDEBUG" ] )
env.Append( CPPFLAGS=" /MT " )
for lib in boostLibs:
if not conf.CheckLib("boost_%s-vc100-mt-1_48" % lib):
conf.CheckLib("boost_%s-mt-1_48" % lib)
dirs = [ "" , "bson/" , "bson/util/" ,
"client/" , "s/" , "shell/" ,
"db/" ,
"scripting/" ,
"util/" , "util/concurrency/" , "util/mongoutils/" , "util/net/" ]
allClientFiles = []
for x in dirs:
allClientFiles += Glob( "mongo/" + x + "*.cpp" )
allClientFiles += Glob( "mongo/util/*.c" )
libs = []
#libs += env.SharedLibrary( "mongoclient" , allClientFiles )
libs += env.Library( "mongoclient" , allClientFiles )
# install
prefix = GetOption( "prefix" )
for x in libs:
env.Install( prefix + "/lib/" , str(x) )
for x in dirs:
x = "mongo/" + x
env.Install( prefix + "/include/" + x , Glob( x + "*.h" ) )
env.Alias( "install" , prefix )
# example setup
#clientTests = []
#clientEnv = env.Clone();
#clientEnv.Prepend( LIBS=["libmongoclient.a"])
#clientEnv.Prepend( LIBPATH=["."] )
# examples
#clientTests += [ clientEnv.Program( "firstExample" , [ "client/examples/first.cpp" ] ) ]
#clientTests += [ clientEnv.Program( "secondExample" , [ "client/examples/second.cpp" ] ) ]
#clientTests += [ clientEnv.Program( "whereExample" , [ "client/examples/whereExample.cpp" ] ) ]
#clientTests += [ clientEnv.Program( "authTest" , [ "client/examples/authTest.cpp" ] ) ]
#clientTests += [ clientEnv.Program( "httpClientTest" , [ "client/examples/httpClientTest.cpp" ] ) ]
#clientTests += [ clientEnv.Program( "clientTest" , [ "client/examples/clientTest.cpp" ] ) ]
#clientEnv.Alias("clientTests", clientTests, [])