c++ - 编译错误

标签 c++ sparsehash

我试图编译这个项目:https://github.com/ccshiro/corecraft 我使用的是 Ubuntu 16.04,我已经安装了:gcc 4.9、5.0、6.0; g++ 4.9, 5.0;铛; cmake3;和 libsparsehash-dev 。

我遇到了这个错误:

[ 96%] Linking CXX executable mangosd
../game/libgame.a(Map.cpp.o): In function `sh_hashtable_settings<ObjectGuid, std::tr1::hash<ObjectGuid>, unsigned long, 4>::hash(ObjectGuid const&) const':
/usr/include/google/sparsehash/hashtable-common.h:65: undefined reference to `std::tr1::hash<ObjectGuid>::operator()(ObjectGuid) const'
collect2: error: ld returned 1 exit status
src/mangosd/CMakeFiles/mangosd.dir/build.make:244: recipe for target 'src/mangosd/mangosd' failed
make[2]: *** [src/mangosd/mangosd] Error 1
CMakeFiles/Makefile2:930: recipe for target 'src/mangosd/CMakeFiles/mangosd.dir/all' failed
make[1]: *** [src/mangosd/CMakeFiles/mangosd.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2

在这里Map.cpp , 这里 /usr/include/google/sparsehash/hashtable-common.h

我尝试用谷歌搜索“collect2: error: ld returned 1 exit status”错误,发现代码中可能是西里尔文或非拉丁文符号,但我没有在上面的这 2 个文件中发现任何错误。 在问题跟踪器上,我也发现了另一个人的同样错误 https://github.com/ccshiro/corecraft/issues/5

我不是 C++ 程序员,所以我不明白这里出了什么问题,谁能帮我解决这个问题?

最佳答案

您看到的是链接器错误。一切都编译得很好,然后当链接器开始将代码拼接在一起时,它缺少一个定义 std::tr1::hash<ObjectGuid>::operator() 功能的对象。哈希运算符。这是一个模板特化,允许将此对象用作映射(或哈希集)中的唯一键。

指定散列函数的模板here .乍一看,我不明白为什么它不应该链接,但后来我意识到链接器正在寻找 std::tr1::hash<ObjectGuid>而不是 std::hash<ObjectGuid> .基本上,看起来您的 STL 库正在使用 TR1这是 C++11 的较旧的准标准版本。

您的第一个尝试应该是弄清楚如何指定您的编译器使用更新版本的 STL 库。您应该能够添加 -std=c++11到 CMAKE C++ 标志(而不是 -std=c++0X)。这是否意味着编辑 CMakeLists.txt 文件以包含标志或确保您的编译器安装了更现代版本的 STL。

这应该可以解决问题。我可以想到另一种解决方案,但我怀疑链接到旧版本的 STL 会导致更多错误。

关于c++ - 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50731343/

相关文章:

c++ - 错误: cannot bind non-const lvalue reference of type ‘Position&’ to an rvalue of type ‘Position’

C++:在删除(*it)后使用dense_hash_set的迭代器

c++ - 使用 gtest 和 google sparsehash 时元组的重新定义

c++ - Google Sparsehash 在不可平凡复制的类型上使用 realloc()

c++ - 进程返回-1073741819(0xC0000005)执行时间: 2.049 s

c++ - 从 C/C++ 使用 UIKit

c++ - _bstr_t 不是已知标识符

c++ - 我只想在 HTTP/1.1 的请求结束时关闭连接