如何将我想在 C++ 中使用的 C 库的库路径添加到环境中

How do I add library paths of C libraries that I want to use in C++ to the environment

我想在 C++ 文件中使用 C 中的 VLFeat 库。他们的 g++ 教程提供了一个基本的 "Hello World" 示例,编译如下:

g++ main.cpp -o vlfeat-test -I /disk/no_backup/lesi/vlfeat-0.9.20/ -L /disk/no_backup/lesi/vlfeat-0.9.20/bin/glnxa64/ -lvl

这很好用。我现在想要的是将库添加到我的 .bashrc,所以我不需要额外的标志:

export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/disk/no_backup/lesi/vlfeat-0.9.20
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/disk/no_backup/lesi/vlfeat-0.9.20/bin/glnxa64

并像这样使用它:

g++ main.cpp -o vlfeat-test

不幸的是,我收到以下错误:

/tmp/cc6tzB55.o: In function `main':
main.cpp:(.text+0x10): undefined reference to `vl_get_printf_func'
collect2: error: ld returned 1 exit status

我做错了什么?

这是教程中的 "Hello World" 代码:

extern "C" {
  #include <vl/generic.h>
}

int main (int argc, const char * argv[]) {
VL_PRINT ("Hello world!\n") ;
  return 0;
}

VLFeat 库 Link:http://www.vlfeat.org/index.html

您指定了在哪里可以找到 header,但您没有告诉编译器 link 针对库。

没有从 "this C file included this header" 到 "I better link this program with this library" 的神奇映射,它不是那样工作的。

我可以用一个 unwind-mess.h header 创建一个库,声明在三个不同的库文件中实现的函数,并调用库 libcream.alibmeringue.alibberry.a

您仍然需要 -lvl 选项来告诉编译器有额外的库代码需要 linked 反对。