G G*_*ill 1 c++ static-linking g++4.8
我正在尝试使用谷歌的 cityhash 哈希函数。我无法将它链接到我的 C++ 代码。我已经安装了 cityHash,它在我的 /usr/local/lib 中生成了 libcityhash.la 等文件。
\n\n我正在设置 LD_LIB_LIBRARY=/usr/local/lib,但它似乎没有链接到这些文件。
\n\n代码:
\n\n#include <iostream>\n#include <fstream>\n#include <cstdlib>\nint main()\n{\n std::ifstream file("dev/urandom");\n char buff[4096];\n file.read(buff, 4096);\n const uint128 hashed = CityHash128(buff,4096);\n file.close();\n
Run Code Online (Sandbox Code Playgroud)\n\n}
\n\n编译:
\n g++ -o city cityHash.cpp
错误:
\n /tmp/cctSoHTX.o: 在函数 main:\ncityHash.cpp:(.text+0x73): 未定义引用 `CityHash128(char const*, unsigned long)\'\ncollect2: 错误: ld 返回 1退出状态
我包含“city.h”并尝试按如下方式编译它:
\n\ng++ -I /usr/local/include/ -L/usr/local/lib -llibcityhash.a cityHash.cpp -o city
但我仍然得到:对 `CityHash128(char const*, unsigned long)\' \xe2\x80\x93 的未定义引用
\n好吧,这就是那句古老的“顺序决定一切”。代替:
g++ -I /usr/local/include/ -L/usr/local/lib /usr/local/lib/libcityhash.a cityHash.cpp -o city
Run Code Online (Sandbox Code Playgroud)
你应该做:
g++ -I /usr/local/include/ -L/usr/local/lib cityHash.cpp -o city -lcityhash
Run Code Online (Sandbox Code Playgroud)
(库和目标文件按照命令行中出现的顺序进行处理,并且由于到目前为止,当您列出库时,没有任何代码使用库中的任何内容,因此该库中没有任何内容 - 然后当您到达实际的如果代码确实使用了它,则您不会在其后面给链接器提供库,因此它找不到该符号 - 请注意,这取决于链接器的行为,因此相同的规则可能不适用于例如MS Visual Studio 编译器/链接器设置)
归档时间: |
|
查看次数: |
1453 次 |
最近记录: |