我使用一个静态库,让我们假设 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/