我正在尝试通过KIT编译Contraction Hierarchies Implementation

该软件已于2009年发布,此后显然未进行维护。由于在此期间发生了一些变化(使用新的C++标准和编译器版本),因此代码不再可以立即进行编译。

README在编译说明方面不是很冗长,说您应该只调用make。但是,使用make给我以下错误:

g++ -Wall -W -Wno-unused-parameter -O6 -c -o main.o main.cpp
在/usr/include/c++/4.8/ext/hash_map:60:0包含的文件中,
从命令/../io/../datastr/graph/../../io/serialize.h:26,
从命令/../io/../datastr/graph/edge.h:26,
从命令/../io/../datastr/graph/graph.h:59,
从命令/../io/createGraph.h:28开始,
从command/NodeOrder.h:29,
来自main.cpp:35:
/usr/include/c++/4.8/backward/backward_warning.h:32:2:警告:#warning该文件至少包含一个不推荐使用或过时的 header ,以后将其删除而不作另行通知。请改用具有同等功能的不建议使用的界面。有关替换 header 和接口(interface)的列表,请查阅文件reverse_warning.h。要禁用此警告,请使用-Wno不推荐使用。 [-Wcpp]
#警告\
^
命令/../processing/../EliminationWeight.h:42:35:错误:对构造函数的调用不能出现在常量表达式中
静态常量类型MAX_VALUE = __DBL_MAX__;
^
命令/../processing/../EliminationWeight.h:43:36:错误:对构造函数的调用不能出现在常量表达式中
静态常量类型MIN_VALUE = -__ DBL_MAX__;
^
make:*** [main.o]错误1

我必须怎么做才能进行编译?

最佳答案

为了使软件编译没有错误,需要执行几个步骤。我已经使用gcc 4.8.1和Boost 1.53.0测试了这些指令。

对构造函数的调用不能出现在常量表达式中

(我已经详细回答了there这个子问题,但是这里将重复重要的步骤。)

我们遇到的第一个编译错误是由于该软件使用了GCC编译器的非标准扩展。看起来,在软件开发时,不必通过编译器开关启用这些功能,但是现代GCC要求这样做。但是,为了将来的兼容性,我们将摆脱它们并以标准兼容的方式实现这些功能。

编译器告诉我们问题出在EliminationWeight.h的第42和43行。这些是该文件中的行:

static const Type MAX_VALUE = __DBL_MAX__;
static const Type MIN_VALUE = -__DBL_MAX__;

GCC在这里提示,因为该类中的double的初始化未包含在该标准中。相反,我们应该分开进行。因此,删除初始化并将行更改为以下内容:
static const Type MAX_VALUE;
static const Type MIN_VALUE;

还要删除#include <limits>,因为我们在此文件中不再需要它。

由于我们仍想初始化这些值,因此我们看一下main.cpp并找到第84行及其后的内容:
// doesn't look nice, but required by the compiler (gcc 4)
const EdgeWeight Weight::MAX_VALUE;
const EliminationWeight::Type EliminationWeight::MAX_VALUE;
const EliminationWeight::Type EliminationWeight::MIN_VALUE;

这看起来像是进行初始化的理想场所。将第86和87行更改为包含以下代码:
const EliminationWeight::Type EliminationWeight::MAX_VALUE = std::numeric_limits< EliminationWeight::Type >::max();
const EliminationWeight::Type EliminationWeight::MIN_VALUE = -std::numeric_limits< EliminationWeight::Type >::max();

还添加
#include <limits>

main.cpp,因为我们现在正在使用该 header 中的std::numeric_limits类。

collect2:错误:ld返回1退出状态

现在可以进行编译,但是链接失败,并显示一条错误消息。 (我已经介绍了there这个问题,但在这里将重复要点。)

所有错误将与Boost::Regex的链接有关。这是我的机器上的完整错误消息:
g++ -lboost_regex -lboost_iostreams -o main main.o
main.o: In function `boost::re_detail::perl_matcher, std::allocator > >, boost::regex_traits > >::unwind_extra_block(bool)':
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE18unwind_extra_blockEb[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE18unwind_extra_blockEb]+0x2c): undefined reference to `boost::re_detail::put_mem_block(void*)'
main.o: In function `void boost::re_detail::raise_error > > >(boost::regex_traits_wrapper > > const&, boost::regex_constants::error_type)':
main.cpp:(.text._ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE[_ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE]+0x7d): undefined reference to `boost::re_detail::get_default_error_string(boost::regex_constants::error_type)'
main.cpp:(.text._ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE[_ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE]+0xb1): undefined reference to `boost::re_detail::raise_runtime_error(std::runtime_error const&)'
main.cpp:(.text._ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE[_ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE]+0xcb): undefined reference to `boost::re_detail::get_default_error_string(boost::regex_constants::error_type)'
main.o: In function `__gnu_cxx::__normal_iterator boost::re_detail::re_is_set_member, char, boost::regex_traits >, unsigned int>(__gnu_cxx::__normal_iterator, __gnu_cxx::__normal_iterator, boost::re_detail::re_set_long const*, boost::re_detail::regex_data > > const&, bool)':
main.cpp:(.text._ZN5boost9re_detail16re_is_set_memberIN9__gnu_cxx17__normal_iteratorIPKcSsEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEjEET_SB_SB_PKNS0_11re_set_longIT2_EERKNS0_10regex_dataIT0_T1_EEb[_ZN5boost9re_detail16re_is_set_memberIN9__gnu_cxx17__normal_iteratorIPKcSsEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEjEET_SB_SB_PKNS0_11re_set_longIT2_EERKNS0_10regex_dataIT0_T1_EEb]+0x17b): undefined reference to `boost::re_detail::cpp_regex_traits_implementation::transform_primary(char const*, char const*) const'
main.cpp:(.text._ZN5boost9re_detail16re_is_set_memberIN9__gnu_cxx17__normal_iteratorIPKcSsEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEjEET_SB_SB_PKNS0_11re_set_longIT2_EERKNS0_10regex_dataIT0_T1_EEb[_ZN5boost9re_detail16re_is_set_memberIN9__gnu_cxx17__normal_iteratorIPKcSsEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEjEET_SB_SB_PKNS0_11re_set_longIT2_EERKNS0_10regex_dataIT0_T1_EEb]+0x4c0): undefined reference to `boost::re_detail::cpp_regex_traits_implementation::transform(char const*, char const*) const'
main.o: In function `boost::re_detail::perl_matcher, std::allocator > >, boost::regex_traits > >::extend_stack()':
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE12extend_stackEv[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE12extend_stackEv]+0x18): undefined reference to `boost::re_detail::get_mem_block()'
main.o: In function `boost::re_detail::perl_matcher, std::allocator > >, boost::regex_traits > >::match_imp()':
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv]+0xc): undefined reference to `boost::re_detail::get_mem_block()'
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv]+0x19e): undefined reference to `boost::re_detail::verify_options(unsigned int, boost::regex_constants::_match_flags)'
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv]+0x254): undefined reference to `boost::re_detail::put_mem_block(void*)'
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv]+0x3c6): undefined reference to `boost::re_detail::put_mem_block(void*)'
main.o: In function `bool boost::regex_match, std::allocator, std::allocator > >, char, boost::regex_traits > >(std::basic_string, std::allocator > const&, boost::match_results, std::allocator >::const_iterator, std::allocator > > >&, boost::basic_regex > > const&, boost::regex_constants::_match_flags)':
main.cpp:(.text._ZN5boost11regex_matchISt11char_traitsIcESaIcESaINS_9sub_matchIN9__gnu_cxx17__normal_iteratorIPKcSsEEEEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbRKSbIT2_T_T0_ERNS_13match_resultsINSJ_14const_iteratorET1_EERKNS_11basic_regexISG_T3_EENS_15regex_constants12_match_flagsE[_ZN5boost11regex_matchISt11char_traitsIcESaIcESaINS_9sub_matchIN9__gnu_cxx17__normal_iteratorIPKcSsEEEEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbRKSbIT2_T_T0_ERNS_13match_resultsINSJ_14const_iteratorET1_EERKNS_11basic_regexISG_T3_EENS_15regex_constants12_match_flagsE]+0xe9): undefined reference to `boost::re_detail::perl_matcher, std::allocator > >, boost::regex_traits > >::construct_init(boost::basic_regex > > const&, boost::regex_constants::_match_flags)'
main.o: In function `void Command::createVector(std::string const&, std::vector >&, double)':
main.cpp:(.text._ZN7Command12createVectorIdEEvRKSsRSt6vectorIT_SaIS4_EES4_[_ZN7Command12createVectorIdEEvRKSsRSt6vectorIT_SaIS4_EES4_]+0x4a): undefined reference to `boost::basic_regex > >::do_assign(char const*, char const*, unsigned int)'
main.cpp:(.text._ZN7Command12createVectorIdEEvRKSsRSt6vectorIT_SaIS4_EES4_[_ZN7Command12createVectorIdEEvRKSsRSt6vectorIT_SaIS4_EES4_]+0x7b): undefined reference to `boost::basic_regex > >::do_assign(char const*, char const*, unsigned int)'
main.cpp:(.text._ZN7Command12createVectorIdEEvRKSsRSt6vectorIT_SaIS4_EES4_[_ZN7Command12createVectorIdEEvRKSsRSt6vectorIT_SaIS4_EES4_]+0xac): undefined reference to `boost::basic_regex > >::do_assign(char const*, char const*, unsigned int)'
main.o: In function `void Command::createVector(std::string const&, std::vector >&, unsigned int)':
main.cpp:(.text._ZN7Command12createVectorIjEEvRKSsRSt6vectorIT_SaIS4_EES4_[_ZN7Command12createVectorIjEEvRKSsRSt6vectorIT_SaIS4_EES4_]+0x48): undefined reference to `boost::basic_regex > >::do_assign(char const*, char const*, unsigned int)'
main.cpp:(.text._ZN7Command12createVectorIjEEvRKSsRSt6vectorIT_SaIS4_EES4_[_ZN7Command12createVectorIjEEvRKSsRSt6vectorIT_SaIS4_EES4_]+0x79): undefined reference to `boost::basic_regex > >::do_assign(char const*, char const*, unsigned int)'
main.o:main.cpp:(.text._ZN7Command12createVectorIjEEvRKSsRSt6vectorIT_SaIS4_EES4_[_ZN7Command12createVectorIjEEvRKSsRSt6vectorIT_SaIS4_EES4_]+0xaa): more undefined references to `boost::basic_regex > >::do_assign(char const*, char const*, unsigned int)' follow
main.o: In function `boost::re_detail::perl_matcher, std::allocator > >, boost::regex_traits > >::match_match()':
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE11match_matchEv[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE11match_matchEv]+0x371): undefined reference to `boost::match_results, std::allocator > > >::maybe_assign(boost::match_results, std::allocator > > > const&)'
main.o: In function `boost::re_detail::perl_matcher, std::allocator > >, boost::regex_traits > >::match_dot_repeat_slow()':
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE21match_dot_repeat_slowEv[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE21match_dot_repeat_slowEv]+0x229): undefined reference to `boost::re_detail::get_mem_block()'
collect2: error: ld returned 1 exit status
make: *** [main] Error 1

It seems like Boost::Regex is not found at all. Note that this is one of the Boost libraries that need to be compiled and linked into your application to work. The following is under the assumption that you have obtained a compiled version and it is in your system's library directory.

When linking, the order in which libraries and object files are passed to GCC via command line parameters is important. For some reason what seems to have worked with GCC in 2009 is no longer working now. This can be fixed by changing the order in which make is passing the parameters to GCC.

Locate Makefile in the project root directory and find line 6:

$(CXX) $(LINK) -o $@ $^ $(LIBS)

在这里,您可以看到make在目标文件之前传递了包含Boost库的链接器开关。 (如果不能,请不要担心。理解此说明并不需要了解Makefile。)为了在当前的GCC中使用此功能,我们将更改参数的顺序。 Makefile的第6行应如下所示:
$(CXX) -o $@ $^ $(LIBS) $(LINK)

保存它,然后再次运行make。编译和链接现在应该继续进行而不会出现错误。

警告:#warning此文件至​​少包含一个不推荐使用或过时的 header

您仍然会看到编译器警告,告诉您您正在使用不建议使用的头文件。您可以忽略此警告。但是,此头文件可能没有较新版本的GCC,并且编译会失败。

如果要修复它,这是方法。

问题出在io/serialize.h,其中包括<ext/hash_map>。 C++ 11标准replaces this with unordered_map 。因此,我们修复了使用该代码的问题。第128行将是
typedef __gnu_cxx::hash_map<key_type, data_type> HashMap;

将其更改为使用unordered_map,如下所示:
typedef std::unordered_map<key_type, data_type> HashMap;

现在我们还需要修复标题。删除<ext/hash_map>的包含并将第26行替换为
#include <algorithm>
#include <unordered_map>

需要<algorithm> include是因为显然<ext/hash_map>还为std::sort提供了std::reversehash_map。这些函数现在位于<algorithm> header 中,并且代码依赖于此文件包含这些事实。

由于这是C++ 11的功能,因此我们需要告诉GCC我们希望对此提供支持。转到项目根目录中的compiler.make并找到第6行
CXXFLAGS = $(DEBUG) $(WARNING) $(OPTIMIZER)

在该行的末尾添加使用C++ 11的开关:
CXXFLAGS = $(DEBUG) $(WARNING) $(OPTIMIZER) -std=c++11

再次运行make cleanmake,您的代码应编译无任何错误或警告。

10-07 12:29
查看更多