gpt4 book ai didi

c++ - yaml-cpp 的编译器错误 - 未定义对 `YAML::detail::node_data::convert_to_map` 的引用

转载 作者:搜寻专家 更新时间:2023-10-31 00:10:49 31 4
gpt4 key购买 nike

完整日志如下:

/tmp/ccCvErNZ.o: In function `YAML::detail::node& YAML::detail::node_data::get<std::string>(std::string const&, std::shared_ptr<YAML::detail::memory_holder>)':
cricket.cpp:(.text._ZN4YAML6detail9node_data3getISsEERNS0_4nodeERKT_St10shared_ptrINS0_13memory_holderEE[_ZN4YAML6detail9node_data3getISsEERNS0_4nodeERKT_St10shared_ptrINS0_13memory_holderEE]+0x94): undefined reference to `YAML::detail::node_data::convert_to_map(std::shared_ptr<YAML::detail::memory_holder>)'
collect2: error: ld returned 1 exit status

我要编译的代码很简单

#include <iostream>
#include <yaml-cpp/yaml.h>

using namespace std;

int main() {
YAML::Node test = YAML::LoadFile("test.yaml");
if (test["date"]) {
cout << "HELLO";
}

return 0;
}

我使用的 YAML 是来自 http://www.yaml.org/start.html 的示例

如果我只是尝试加载 YAML,它可以正常加载。但是如果我尝试访问任何数据,我会得到同样的错误。所以这不是链接问题。

编辑:我可以做类似 cout << test 的事情和 cout << test.type()和其他功能。我认为问题在于使用基于字符串的映射来访问内部节点。

最佳答案

您似乎没有正确链接 yaml-cpp 库。编译时加入参数-lyaml-cpp。例如:

g++ -I/usr/local/include -L/usr/local/lib -lyaml-cpp -o test main.cpp

编辑

另一种选择是将此 cmake 包含到您的 CMakeLists.txt 中:

# attempt to find static library first if this is set
if(YAMLCPP_STATIC_LIBRARY)
set(YAMLCPP_STATIC libyaml-cpp.a)
endif()

# find the yaml-cpp include directory
find_path(YAMLCPP_INCLUDE_DIR yaml-cpp/yaml.h
PATH_SUFFIXES include
PATHS
~/Library/Frameworks/yaml-cpp/include/
/Library/Frameworks/yaml-cpp/include/
/usr/local/include/
/usr/include/
/sw/yaml-cpp/ # Fink
/opt/local/yaml-cpp/ # DarwinPorts
/opt/csw/yaml-cpp/ # Blastwave
/opt/yaml-cpp/
${YAMLCPP_DIR}/include/)

# find the yaml-cpp library
find_library(YAMLCPP_LIBRARY
NAMES ${YAMLCPP_STATIC} yaml-cpp
PATH_SUFFIXES lib64 lib
PATHS ~/Library/Frameworks
/Library/Frameworks
/usr/local
/usr
/sw
/opt/local
/opt/csw
/opt
${YAMLCPP_DIR}/lib)

# handle the QUIETLY and REQUIRED arguments and set YAMLCPP_FOUND to TRUE if all listed variables are TRUE
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(YAMLCPP DEFAULT_MSG YAMLCPP_INCLUDE_DIR YAMLCPP_LIBRARY)
mark_as_advanced(YAMLCPP_INCLUDE_DIR YAMLCPP_LIBRARY)

关于c++ - yaml-cpp 的编译器错误 - 未定义对 `YAML::detail::node_data::convert_to_map` 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36403287/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com