c++ - 如何在 Linux 上的 C++ 程序中使用 yaml-cpp?

标签 c++ yaml yaml-cpp

我最近决定使用 yaml 作为我的配置文件技术,并且正在 OpenSuse 11.3 上编写一个 c++ linux 应用程序。

问题是,即使在成功安装 cmake 后,如 yaml-cpp 文档页面所示编译 yaml-cpp,我仍然无法编译 yaml-cpp 页面上的简单演示程序 here .

例如,当我尝试编译 monster.yaml and main.cpp example

我的编译器发出命令 gcc main.cpp ,它发出以下错误:

main.cpp:24:25: error: ‘YAML’ does not name a type
main.cpp:24:35: error: expected unqualified-id before ‘&’ token
main.cpp:24:35: error: expected ‘)’ before ‘&’ token
main.cpp:24:35: error: expected initializer before ‘&’ token
main.cpp:30:25: error: ‘YAML’ does not name a type
main.cpp:30:35: error: expected unqualified-id before ‘&’ token
main.cpp:30:35: error: expected ‘)’ before ‘&’ token
main.cpp:30:35: error: expected initializer before ‘&’ token
main.cpp:35:25: error: ‘YAML’ does not name a type
main.cpp:35:35: error: expected unqualified-id before ‘&’ token
main.cpp:35:35: error: expected ‘)’ before ‘&’ token
main.cpp:35:35: error: expected initializer before ‘&’ token

我尝试更改来自 #include "yaml-cpp/yaml.h" 的 include 指令至 #include <yaml.h> ,因为我已经安装了 yaml 库,但这并没有解决任何问题。

那我做错了什么?

这是从第 24 行到第 40 行粘贴的有问题的代码:

void operator >> (const YAML::Node& node, Vec3& v) {
   node[0] >> v.x;
   node[1] >> v.y;
   node[2] >> v.z;
}

void operator >> (const YAML::Node& node, Power& power) {
   node["name"] >> power.name;
   node["damage"] >> power.damage;
}

void operator >> (const YAML::Node& node, Monster& monster) {
   node["name"] >> monster.name;
   node["position"] >> monster.position;
   const YAML::Node& powers = node["powers"];
   for(unsigned i=0;i<powers.size();i++) {
      Power power;
      powers[i] >> power;
      monster.powers.push_back(power);
   }
}

这里是 sudo make install 输出的转储在我运行 make 之后命令:

[ 81%] Built target yaml-cpp
[ 96%] Built target run-tests
[100%] Built target parse
Install the project...
-- Install configuration:      "Release"                                                                                                                                              
-- Installing: /usr/local/lib/libyaml-cpp.so.0.2.6
-- Up-to-date: /usr/local/lib/libyaml-cpp.so.0.2
-- Up-to-date: /usr/local/lib/libyaml-cpp.so
-- Up-to-date: /usr/local/include/yaml-cpp/aliasmanager.h
-- Up-to-date: /usr/local/include/yaml-cpp/anchor.h
-- Up-to-date: /usr/local/include/yaml-cpp/conversion.h
-- Up-to-date: /usr/local/include/yaml-cpp/dll.h
-- Up-to-date: /usr/local/include/yaml-cpp/emitfromevents.h
-- Up-to-date: /usr/local/include/yaml-cpp/emitter.h
-- Up-to-date: /usr/local/include/yaml-cpp/emittermanip.h
-- Up-to-date: /usr/local/include/yaml-cpp/eventhandler.h
-- Up-to-date: /usr/local/include/yaml-cpp/exceptions.h
-- Up-to-date: /usr/local/include/yaml-cpp/iterator.h
-- Up-to-date: /usr/local/include/yaml-cpp/ltnode.h
-- Up-to-date: /usr/local/include/yaml-cpp/mark.h
-- Up-to-date: /usr/local/include/yaml-cpp/node.h
-- Up-to-date: /usr/local/include/yaml-cpp/nodeimpl.h
-- Up-to-date: /usr/local/include/yaml-cpp/nodereadimpl.h
-- Up-to-date: /usr/local/include/yaml-cpp/nodeutil.h
-- Up-to-date: /usr/local/include/yaml-cpp/noncopyable.h
-- Up-to-date: /usr/local/include/yaml-cpp/null.h
-- Up-to-date: /usr/local/include/yaml-cpp/ostream.h
-- Up-to-date: /usr/local/include/yaml-cpp/parser.h
-- Up-to-date: /usr/local/include/yaml-cpp/stlemitter.h
-- Up-to-date: /usr/local/include/yaml-cpp/stlnode.h
-- Up-to-date: /usr/local/include/yaml-cpp/traits.h
-- Up-to-date: /usr/local/include/yaml-cpp/yaml.h
-- Up-to-date: /usr/local/include/yaml-cpp/anchordict.h
-- Up-to-date: /usr/local/include/yaml-cpp/graphbuilder.h
-- Installing: /usr/local/lib/pkgconfig/yaml-cpp.pc

可能有一些我必须附加到 gcc 的特殊指令/选项吗?使用libyaml编译时的命令?类似 gcc main.cpp -libyaml 的东西?

编译器的更多输出(gcc version 4.5.0 20100604 [gcc-4_5-branch revision 160292] (SUSE Linux)):

/tmp/ccYltArL.o: In function `operator>>(YAML::Node const&, Monster&)':
main.cpp:(.text+0x1a8): undefined reference to `YAML::Node::size() const'
/tmp/ccYltArL.o: In function `main':
main.cpp:(.text+0x1fe): undefined reference to `std::basic_ifstream<char,      std::char_traits<char> >::basic_ifstream(char const*, std::_Ios_Openmode)'
main.cpp:(.text+0x215): undefined reference to   `YAML::Parser::Parser(std::basic_istream<char, std::char_traits<char> >&)'
main.cpp:(.text+0x224): undefined reference to `YAML::Node::Node()'
main.cpp:(.text+0x23e): undefined reference to    `YAML::Parser::GetNextDocument(YAML::Node&)'
main.cpp:(.text+0x29c): undefined reference to `std::cout'

还有比放在这里更多的东西 ,最后以:

/tmp/ccYltArL.o:(.rodata._ZTIN4YAML14BadDereferenceE[typeinfo for YAML::BadDereference]+0x0): undefined reference to `vtable for    __cxxabiv1::__si_class_type_info'
/tmp/ccYltArL.o:(.rodata._ZTIN4YAML11KeyNotFoundE[typeinfo for YAML::KeyNotFound]+0x0): undefined reference to `vtable for  __cxxabiv1::__si_class_type_info'
/tmp/ccYltArL.o:(.rodata._ZTIN4YAML13InvalidScalarE[typeinfo for YAML::InvalidScalar]+0x0): more undefined references to `vtable for __cxxabiv1::__si_class_type_info' follow
/tmp/ccYltArL.o:(.rodata._ZTIN4YAML9ExceptionE[typeinfo for YAML::Exception]+0x8): undefined reference to `typeinfo for std::runtime_error'
/tmp/ccYltArL.o:(.eh_frame+0x18f): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

-- 最后--

用 Chris 的 soln(见下文)解决了它,尽管我实际上发现了一种更简洁的预加载“自定义”库的方法,即使用 ldconfig在我将库的路径添加到 *.conf 后配置库搜索路径的命令文件在 /etc/ld.so.conf.d/ .查看detailed guide here...

最佳答案

gcc 没有被指示查看/usr/local。您需要明确地执行此操作。另外,那真的应该是 g++ 而不是 gcc。所以首先确保你的包含看起来像这样:

#include "yaml-cpp/yaml.h"

然后像这样编译它:

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

关于c++ - 如何在 Linux 上的 C++ 程序中使用 yaml-cpp?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5693686/

相关文章:

c++ - ranges::views 如何实现 O(1) 复杂度?

c++ - 使用 yaml-cpp 转换为模板类

ruby - 当我用 Ruby 解析 YAML 时,它改变了我的 anchor 名称

c++ - 如何在 YAML-CPP 中将版本字符串写为文字(而非字符串)?

c++ - 链接 Yaml-cpp 和 Armadillo 共享库的 CMake 项目

c++ - 替换 Qt 中的小部件

c++ - 在声明期间初始化引用

c++ - N选K、K-N、K-2N等,递归中递归

tags - 我不明白 YAML 标签是什么

c++ - 使用yaml-cpp错误解析YAML文件