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

发布于 2024-11-01 13:41:44 字数 5950 浏览 4 评论 0原文

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

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

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

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

main.cpp:24:25:错误:“YAML”未命名类型
main.cpp:24:35: 错误: '&' 之前预期有不合格的 id令牌
main.cpp:24:35: 错误:在“&”之前应有“)”令牌
main.cpp:24:35: 错误: '&' 之前的预期初始化程序令牌
main.cpp:30:25:错误:“YAML”未命名类型
main.cpp:30:35: 错误: '&' 之前预期有不合格的 id令牌
main.cpp:30:35: 错误:在“&”之前应有“)”令牌
main.cpp:30:35: 错误: '&' 之前的预期初始化程序令牌
main.cpp:35:25:错误:“YAML”未命名类型
main.cpp:35:35: 错误: '&' 之前预期有不合格的 id令牌
main.cpp:35:35: 错误:在“&”之前应有“)”令牌
main.cpp:35:35:错误:预期在“&”之前初始化令牌

我尝试将 include 指令从 #include "yaml-cpp/yaml.h" 更改为 #include,因为我安装了 yaml lib,但这并没有解决任何问题。

那么我做错了什么?

以下是从第 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);
   }
}

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

[ 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

可能有使用 libyaml 编译时必须附加到 gcc 命令的一些特殊指令/选项?像 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 的解决方案解决了(见下文),尽管我实际上找到了一种更干净的预加载“自定义”库的方法,通过使用 在将库的路径添加到 /etc/ld.so.conf.d/*.conf 文件中后,使用 ldconfig 命令配置库搜索路径代码>.请参阅此处的详细指南...

I have recently decided to use yaml as my configuration file technology, and am writing a c++ linux application on OpenSuse 11.3.

The problem is that even after successfully installing cmake, compilling yaml-cpp as shown on the yaml-cpp docs page, I still can't compile the simple demo programs on the yaml-cpp page here.

For example, when I try to compile the the monster.yaml and main.cpp example

my compiler on issuing the command gcc main.cpp, it issues the following errors:

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

I tried changing the include directive from #include "yaml-cpp/yaml.h" to #include <yaml.h>, since I'd installed the yaml lib, but this didn't solve anything.

so what did I do wrong?

Here is the problematic code pasted from lines 24 to 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);
   }
}

And here is a dump of the output of the sudo make install after I run the make command:

[ 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

Could there probably be some special directive / option that I have to append to the gcc command when compiling with libyaml? something like gcc main.cpp -libyaml ?

For more output from the compiler (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'

and much more stuff than can fit here
, finally ending in:

/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

-- finally --

Solved it with Chris's soln (see below), though I actually found an even cleaner way of pre-loading 'custom' libraries, by using the ldconfig command to configure the library search path after i add the path to my libs into a *.conf file in /etc/ld.so.conf.d/. See the detailed guide here...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明月松间行 2024-11-08 13:41:44

没有指示 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

gcc isn't being instructed to look in /usr/local. You need to do it explicitly. Also, that really should be g++ and not gcc. So first make sure your include looks like this:

#include "yaml-cpp/yaml.h"

Then compile it like this:

g++ -I/usr/local/include -L/usr/local/lib -lyaml-cpp -o testprogram main.cpp
梦明 2024-11-08 13:41:44

看起来它找不到 yaml-cpp 标头。首先,include 指令应该是

#include "yaml-cpp/yaml.h"

因为,正如您所看到的,标头被安装到 /usr/local/include/yaml-cpp/

您在 include 语句行中是否收到错误消息? (你能发布完整的编译器输出吗?)

It looks like it can't find the yaml-cpp headers. First of all, the include directive should be

#include "yaml-cpp/yaml.h"

since, as you can see, the headers are installed to /usr/local/include/yaml-cpp/.

Do you get an error message on the line of the include statement? (Can you post the full compiler output?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文