未定义对YAML :: Load的引用

时间:2019-06-27 18:14:28

标签: gcc yaml-cpp

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

我已经尝试过thisthis中介绍的解决方案,但它们都不起作用。

我的cmake命令:
CC=$(which gcc) CXX=$(which 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++11 sample_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

1 个答案:

答案 0 :(得分:0)

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