c++ - GCC 不链接静态库依赖(makefile)

标签 c++ gcc makefile linker

我使用一个静态库,让我们假设 cityhash,我已经构建并安装到/usr/local/lib。我有一个使用 cityhash 的文件 foo.cxx,例如:

// foo.cxx
u64 get_hash(const std::string &s) {
  return CityHash64(s.data(), s.size());
}

我从中构建了一个静态库:

gcc -c foo.cxx  => foo.o
ar rcs libfoo.a foo.a  => libfoo.a

我有另一个文件 bar.cxx,它使用 foo.cxx 并间接使用 CityHash 函数。我编译它,并链接到 libcityhash.a 和 libfoo.a,如下所示:

gcc -c bar.cxx  => bar.o
gcc -L. -o bar bar.o -lcityhash -lfoo

但这不起作用,链接器提示 CityHash64 是 undefined reference 。怎么了?当我不创建静态库时 libfoo.a 一切正常。

最佳答案

看到这个。您需要编写链接器参数 -lfoo -lcityhash。需要符号的库应该在提供它的库之前。

Why does the order in which libraries are linked sometimes cause errors in GCC?

关于c++ - GCC 不链接静态库依赖(makefile),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34823423/

相关文章:

c++ - 对汇编函数的 undefined reference

c - 执行时在 c 中自动进行代码测试

makefile - 找不到 Ranlib

c++ - mpi All-to-All Scatter/Gather的高效实现

c++ - 为什么这个虚拟析构函数在 C++ 中不起作用?

union 成员的编译错误 "has no member named"

C++ 为 VS2012 构建 Winsparkle

c++ - 静态库多重定义链接错误

c++ - 免费注册 com 和 Dispatch LPPICTUREDISP

c - 从 ANSI C 代码获取控制流图