2017-07-14 196 views
2

我在我的C++程序中使用OpenSSL,我需要鏈接cryptossl。如果是例如GCC,我只想通過:如何使用WAF鏈接靜態庫?

-lcrypto -lssl 

我加入網絡,模擬器3 這種依賴關係,但我不知道如何在WAF做到這一點。我應該如何將它們添加爲依賴項?

回答

3

首先你需要檢查configure如果庫是可用的,那麼你可以build它。

def configure(cnf): 
    # other parameters omitted for brevity 
    cnf.check(lib=["crypto", "ssl"]) 

def build(bld): 
    # other parameters omitted for brevity 
    bld(use=["crypto", "ssl"]) 

您也可以使用uselib_store參數,如果你不想重複庫:

cnf.check(lib=["crypto", "ssl"], uselib_store=["libs"]) 
bld(use=["libs"]) 
0

我嘗試添加CRYPTOPP,而不是Crypto,因爲我可以CRYPTOPP做同樣的工作。

這是如何爲我工作的。 以下代碼需要在ns-3.xx目錄中的wscript中添加/編輯。

def configure(conf): 
    #other parameters removed 
    env = conf.env 
    conf.env['cryptopp'] = conf.check(mandatory=True, lib='cryptopp', uselib_store='CRYPTOPP') 
    conf.env['sph'] = conf.check(mandatory=True, lib='sph', uselib_store='SPH') 
    conf.env.append_value('CXXDEFINES', 'ENABLE_CRYPTOPP') 
    conf.env.append_value('CCDEFINES', 'ENABLE_CRYPTOPP') 

    conf.env['lssl'] = conf.check(mandatory=True, lib='ssl', uselib_store='OPENSSL') 
    conf.env.append_value('CXXDEFINES', 'ENABLE_SSL') 
    conf.env.append_value('CCDEFINES', 'ENABLE_SSL') 

.....

def create_ns3_program(bld, name, dependencies=('core',)): 
    program = bld(features='cxx cxxprogram') 
    #other parameters removed 
    program.use = program.ns3_module_dependencies 
    if program.env['ENABLE_STATIC_NS3']: 
     if sys.platform == 'darwin': 
      program.env.STLIB_MARKER = '-Wl,-all_load,-lcryptopp,-lsph' 
     else: 
      program.env.STLIB_MARKER = '-Wl,-Bstatic,--whole-archive,-lcryptopp,-lsph' 
      program.env.SHLIB_MARKER = '-Wl,-Bdynamic,--no-whole-archive,-lcryptopp,-lsph' 
    else: 
     if program.env.DEST_BINFMT == 'elf': 
      # All ELF platforms are impacted but only the gcc compiler has a flag to fix it. 
      if 'gcc' in (program.env.CXX_NAME, program.env.CC_NAME): 
       program.env.append_value ('SHLIB_MARKER', '-Wl,--no-as-needed,-lcryptopp,-lsph') 

    return program 

.......

def add_scratch_programs(bld): 
    all_modules = [mod[len("ns3-"):] for mod in bld.env['NS3_ENABLED_MODULES']] 
    try: 
     for filename in os.listdir("scratch"): 
      if filename.startswith('.') or filename == 'CVS': 
       continue 
      if os.path.isdir(os.path.join("scratch", filename)): 
       obj = bld.create_ns3_program(filename, all_modules) 
       obj.path = obj.path.find_dir('scratch').find_dir(filename) 
       obj.source = obj.path.ant_glob('*.cc') 
       obj.target = filename 
       obj.name = obj.target 
       obj.install_path = None 
       #Add the below paramters 
       obj.uselib = 'CRYPTOPP' 
       obj.uselib = 'SPH' 
       obj.uselib = 'OPENSSL' 
      elif filename.endswith(".cc"): 
       name = filename[:-len(".cc")] 
       obj = bld.create_ns3_program(name, all_modules) 
       obj.path = obj.path.find_dir('scratch') 
       obj.source = filename 
       obj.target = name 
       obj.name = obj.target 
       obj.install_path = None 
       #Add the below paramters 
       obj.uselib = 'CRYPTOPP' 
       obj.uselib = 'SPH' 
       obj.uselib = 'OPENSSL' 
    except OSError: 
     return 

這基本上將所有paramters在運行時你是從頭開始運行模擬器。

PS:該腳本特定於ns-3。