由于调用./waf
后的链接错误,我目前在尝试使用 ns-3 中的 gcrypt 库中的代码时遇到问题。我已经正确安装了 gcrypt,因为以下程序在使用g++ test.cpp -o test -lgcrypt
编译时工作正常。
#include <stdio.h>
#include <gcrypt.h>
int main(void)
{
char *s = "some text";
unsigned char *x;
unsigned i;
unsigned int l = gcry_md_get_algo_dlen(GCRY_MD_SHA256); /* get digest length (used later to print the result) */
gcry_md_hd_t h;
gcry_md_open(&h, GCRY_MD_SHA256, GCRY_MD_FLAG_SECURE); /* initialise the hash context */
gcry_md_write(h, s, strlen(s)); /* hash some text */
x = gcry_md_read(h, GCRY_MD_SHA256); /* get the result */
for (i = 0; i < l; i++)
{
printf("%02x", x[i]); /* print the result */
}
printf("n");
return 0;
}
但是,在 ns-3 中复制此代码会产生多个与链接时以下错误类似的错误:
/home/xxx/Desktop/ns-allinone-3.28.1/ns-3.28.1/build/../scratch/ns3consensus/AppCons.cc:251: undefined reference to `gcry_md_get_algo_dlen'
此外,ns-3 本身似乎认识到安装了 gcrypt,因为./waf configure
的输出表明 gcrypt 库是随Gcrypt library : enabled
一起安装的。
我已经按照 https://www.nsnam.org/wiki/HOWTO_use_ns-3_with_other_libraries 的建议添加到顶级wscriptconf.env.append_value("LINKFLAGS", ["-lgcrypt"])
,但是问题仍然存在。我是否需要添加 wscript 的其他内容,或者我缺少其他链接基础知识?
这个问题的答案是如何将库包含在waf
中。
- 包括由
cfg.env.append_value('INCLUDES', ['/usr/local/include'])
添加, - 库搜索路径由
conf.env.append_value('LIBPATH', ["/usr/local/lib"])
和 - 检查/编译/链接时,您使用关键字use=name_of_the_library,因此在这里它会
use='gcrypt'
.