gcc - 对 YAML::Load 的 undefined reference

标签 gcc yaml-cpp

yaml-cpp 在编译简单的 cpp 文件时抛出常见错误。

我已经尝试过 this 中提出的解决方案和 this SO 帖子,但它们都不起作用。

我的cmake命令:
CC=$(哪个 gcc) CXX=$(哪个 g++) cmake -DBUILD_SHARED_LIBS=ON ..

然后我根据需要运行 makesudo make install

正确的文件已放置在 usr/local/includeusr/local/lib 文件夹中。

这是我尝试编译的 sample_yaml.cpp 文件中的代码。

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

int main()
{
  YAML::Node primes = YAML::Load("[2, 3, 5, 7, 11]");
  for (std::size_t i=0;i<primes.size();i++) {
    std::cout << primes[i].as<int>() << "\n";
  }

  return 0;
}

这是我的编译行:
g++ -I/usr/local/include -L/usr/local/lib -lyaml-cpp -std=c++11sample_yaml.cpp

这是它返回的错误:

/tmp/ccgMxlAA.o: In function `main':
sample_yaml.cpp:(.text+0x143): undefined reference to `YAML::Load(char const*)'
/tmp/ccgMxlAA.o: In function `YAML::Exception::Exception(YAML::Mark const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
sample_yaml.cpp:(.text._ZN4YAML9ExceptionC2ERKNS_4MarkERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN4YAML9ExceptionC5ERKNS_4MarkERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x5b): undefined reference to `vtable for YAML::Exception'
/tmp/ccgMxlAA.o: In function `YAML::RepresentationException::RepresentationException(YAML::Mark const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
sample_yaml.cpp:(.text._ZN4YAML23RepresentationExceptionC2ERKNS_4MarkERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN4YAML23RepresentationExceptionC5ERKNS_4MarkERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x2c): undefined reference to `vtable for YAML::RepresentationException'
/tmp/ccgMxlAA.o: In function `YAML::InvalidNode::InvalidNode(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
sample_yaml.cpp:(.text._ZN4YAML11InvalidNodeC2ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN4YAML11InvalidNodeC5ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x68): undefined reference to `vtable for YAML::InvalidNode'
/tmp/ccgMxlAA.o: In function `YAML::BadConversion::BadConversion(YAML::Mark const&)':
sample_yaml.cpp:(.text._ZN4YAML13BadConversionC2ERKNS_4MarkE[_ZN4YAML13BadConversionC5ERKNS_4MarkE]+0x74): undefined reference to `vtable for YAML::BadConversion'
/tmp/ccgMxlAA.o: In function `YAML::detail::memory_holder::create_node()':
sample_yaml.cpp:(.text._ZN4YAML6detail13memory_holder11create_nodeEv[_ZN4YAML6detail13memory_holder11create_nodeEv]+0x1c): undefined reference to `YAML::detail::memory::create_node()'
/tmp/ccgMxlAA.o: In function `YAML::detail::node_ref::mark_defined()':
sample_yaml.cpp:(.text._ZN4YAML6detail8node_ref12mark_definedEv[_ZN4YAML6detail8node_ref12mark_definedEv]+0x1c): undefined reference to `YAML::detail::node_data::mark_defined()'
/tmp/ccgMxlAA.o: In function `YAML::detail::node_ref::set_null()':
sample_yaml.cpp:(.text._ZN4YAML6detail8node_ref8set_nullEv[_ZN4YAML6detail8node_ref8set_nullEv]+0x1c): undefined reference to `YAML::detail::node_data::set_null()'
/tmp/ccgMxlAA.o: In function `YAML::detail::node_ref::set_scalar(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
sample_yaml.cpp:(.text._ZN4YAML6detail8node_ref10set_scalarERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN4YAML6detail8node_ref10set_scalarERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x2a): undefined reference to `YAML::detail::node_data::set_scalar(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/ccgMxlAA.o: In function `YAML::detail::node_ref::size() const':
sample_yaml.cpp:(.text._ZNK4YAML6detail8node_ref4sizeEv[_ZNK4YAML6detail8node_ref4sizeEv]+0x1c): undefined reference to `YAML::detail::node_data::size() const'
/tmp/ccgMxlAA.o: In function `YAML::Node::EnsureNodeExists() const':
sample_yaml.cpp:(.text._ZNK4YAML4Node16EnsureNodeExistsEv[_ZNK4YAML4Node16EnsureNodeExistsEv]+0x6c): undefined reference to `YAML::InvalidNode::~InvalidNode()'
sample_yaml.cpp:(.text._ZNK4YAML4Node16EnsureNodeExistsEv[_ZNK4YAML4Node16EnsureNodeExistsEv]+0x71): undefined reference to `typeinfo for YAML::InvalidNode'
/tmp/ccgMxlAA.o: In function `YAML::Node::Mark() const':
sample_yaml.cpp:(.text._ZNK4YAML4Node4MarkEv[_ZNK4YAML4Node4MarkEv]+0x6c): undefined reference to `YAML::InvalidNode::~InvalidNode()'
sample_yaml.cpp:(.text._ZNK4YAML4Node4MarkEv[_ZNK4YAML4Node4MarkEv]+0x71): undefined reference to `typeinfo for YAML::InvalidNode'
/tmp/ccgMxlAA.o: In function `YAML::Node::Type() const':
sample_yaml.cpp:(.text._ZNK4YAML4Node4TypeEv[_ZNK4YAML4Node4TypeEv]+0x6c): undefined reference to `YAML::InvalidNode::~InvalidNode()'
sample_yaml.cpp:(.text._ZNK4YAML4Node4TypeEv[_ZNK4YAML4Node4TypeEv]+0x71): undefined reference to `typeinfo for YAML::InvalidNode'
/tmp/ccgMxlAA.o: In function `YAML::Node::Scalar[abi:cxx11]() const':
sample_yaml.cpp:(.text._ZNK4YAML4Node6ScalarB5cxx11Ev[_ZNK4YAML4Node6ScalarB5cxx11Ev]+0x6c): undefined reference to `YAML::InvalidNode::~InvalidNode()'
sample_yaml.cpp:(.text._ZNK4YAML4Node6ScalarB5cxx11Ev[_ZNK4YAML4Node6ScalarB5cxx11Ev]+0x71): undefined reference to `typeinfo for YAML::InvalidNode'
sample_yaml.cpp:(.text._ZNK4YAML4Node6ScalarB5cxx11Ev[_ZNK4YAML4Node6ScalarB5cxx11Ev]+0x9d): undefined reference to `YAML::detail::node_data::empty_scalar[abi:cxx11]()'
/tmp/ccgMxlAA.o: In function `void YAML::Node::Assign<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
sample_yaml.cpp:(.text._ZN4YAML4Node6AssignINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvRKT_[_ZN4YAML4Node6AssignINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvRKT_]+0x70): undefined reference to `YAML::InvalidNode::~InvalidNode()'
sample_yaml.cpp:(.text._ZN4YAML4Node6AssignINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvRKT_[_ZN4YAML4Node6AssignINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvRKT_]+0x75): undefined reference to `typeinfo for YAML::InvalidNode'
/tmp/ccgMxlAA.o: In function `YAML::Node::size() const':
sample_yaml.cpp:(.text._ZNK4YAML4Node4sizeEv[_ZNK4YAML4Node4sizeEv]+0x6c): undefined reference to `YAML::InvalidNode::~InvalidNode()'
sample_yaml.cpp:(.text._ZNK4YAML4Node4sizeEv[_ZNK4YAML4Node4sizeEv]+0x71): undefined reference to `typeinfo for YAML::InvalidNode'
/tmp/ccgMxlAA.o: In function `YAML::Node YAML::Node::operator[]<unsigned long>(unsigned long const&)':
sample_yaml.cpp:(.text._ZN4YAML4NodeixImEES0_RKT_[_ZN4YAML4NodeixImEES0_RKT_]+0x74): undefined reference to `YAML::InvalidNode::~InvalidNode()'
sample_yaml.cpp:(.text._ZN4YAML4NodeixImEES0_RKT_[_ZN4YAML4NodeixImEES0_RKT_]+0x79): undefined reference to `typeinfo for YAML::InvalidNode'
/tmp/ccgMxlAA.o: In function `int YAML::Node::as<int>() const':
sample_yaml.cpp:(.text._ZNK4YAML4Node2asIiEET_v[_ZNK4YAML4Node2asIiEET_v]+0x6c): undefined reference to `YAML::InvalidNode::~InvalidNode()'
sample_yaml.cpp:(.text._ZNK4YAML4Node2asIiEET_v[_ZNK4YAML4Node2asIiEET_v]+0x71): undefined reference to `typeinfo for YAML::InvalidNode'
/tmp/ccgMxlAA.o: In function `YAML::TypedBadConversion<int>::~TypedBadConversion()':
sample_yaml.cpp:(.text._ZN4YAML18TypedBadConversionIiED2Ev[_ZN4YAML18TypedBadConversionIiED5Ev]+0x20): undefined reference to `YAML::BadConversion::~BadConversion()'
/tmp/ccgMxlAA.o: In function `YAML::detail::node& YAML::detail::node_data::get<unsigned long>(unsigned long const&, std::shared_ptr<YAML::detail::memory_holder>)':
sample_yaml.cpp:(.text._ZN4YAML6detail9node_data3getImEERNS0_4nodeERKT_St10shared_ptrINS0_13memory_holderEE[_ZN4YAML6detail9node_data3getImEERNS0_4nodeERKT_St10shared_ptrINS0_13memory_holderEE]+0xbd): undefined reference to `YAML::detail::node_data::convert_to_map(std::shared_ptr<YAML::detail::memory_holder>)'
sample_yaml.cpp:(.text._ZN4YAML6detail9node_data3getImEERNS0_4nodeERKT_St10shared_ptrINS0_13memory_holderEE[_ZN4YAML6detail9node_data3getImEERNS0_4nodeERKT_St10shared_ptrINS0_13memory_holderEE]+0xec): undefined reference to `YAML::BadSubscript::~BadSubscript()'
sample_yaml.cpp:(.text._ZN4YAML6detail9node_data3getImEERNS0_4nodeERKT_St10shared_ptrINS0_13memory_holderEE[_ZN4YAML6detail9node_data3getImEERNS0_4nodeERKT_St10shared_ptrINS0_13memory_holderEE]+0xf1): undefined reference to `typeinfo for YAML::BadSubscript'
sample_yaml.cpp:(.text._ZN4YAML6detail9node_data3getImEERNS0_4nodeERKT_St10shared_ptrINS0_13memory_holderEE[_ZN4YAML6detail9node_data3getImEERNS0_4nodeERKT_St10shared_ptrINS0_13memory_holderEE]+0x21b): undefined reference to `YAML::detail::node_data::insert_map_pair(YAML::detail::node&, YAML::detail::node&)'
/tmp/ccgMxlAA.o: In function `YAML::BadSubscript::BadSubscript<unsigned long>(unsigned long const&)':
sample_yaml.cpp:(.text._ZN4YAML12BadSubscriptC2ImEERKT_[_ZN4YAML12BadSubscriptC5ImEERKT_]+0x6d): undefined reference to `vtable for YAML::BadSubscript'
/tmp/ccgMxlAA.o: In function `YAML::detail::node& YAML::detail::node_data::convert_to_node<unsigned long>(unsigned long const&, std::shared_ptr<YAML::detail::memory_holder>)':
sample_yaml.cpp:(.text._ZN4YAML6detail9node_data15convert_to_nodeImEERNS0_4nodeERKT_St10shared_ptrINS0_13memory_holderEE[_ZN4YAML6detail9node_data15convert_to_nodeImEERNS0_4nodeERKT_St10shared_ptrINS0_13memory_holderEE]+0x65): undefined reference to `YAML::detail::memory_holder::merge(YAML::detail::memory_holder&)'
/tmp/ccgMxlAA.o:(.rodata._ZTIN4YAML18TypedBadConversionIiEE[_ZTIN4YAML18TypedBadConversionIiEE]+0x10): undefined reference to `typeinfo for YAML::BadConversion'
collect2: error: ld returned 1 exit status

最佳答案

g++ -I/usr/local/include -L/usr/local/lib -lyaml-cpp -std=c++11 sample_yaml.cpp

g++ -I/usr/local/include -L/usr/local/lib -std=c++11 sample_yaml.cpp -lyaml-cpp

更新:
-lyaml-cpp 移动到末尾。

Raspberry PI: make command not working with below makefile

关于gcc - 对 YAML::Load 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56796666/

相关文章:

c++ - yaml-cpp 读取项目中的序列

c++ - yaml-cpp、YAML::Node 和模板运算符 >>

c - 识别C文件的版本

c++ - 使用 gcc 编译更大的(~6MB)映射初始化 C++ 文件

c++ - 复制语义和比较

c++ - yaml-cpp 在保存时如何重新排序节点?

c++ - fatal error : yaml-cpp/yaml. h:没有那个文件或目录

gcc - 了解堆栈对齐强制

c++11 - gcc:切换后如何最好地处理有关(无法到达的)功能结束的警告?

c - 在 Linux 中构建系统调用。内核 > 3.x.x