Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use yaml-cpp in a C++ program on Linux?
    primarykey
    data
    text
    <p>I have recently decided to use yaml as my configuration file technology, and am writing a c++ linux application on OpenSuse 11.3.</p> <p>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 <a href="http://code.google.com/p/yaml-cpp/wiki/HowToParseADocument" rel="nofollow">here</a>.</p> <p>For example, when I try to compile the the <a href="http://code.google.com/p/yaml-cpp/wiki/HowToParseADocument#A_Complete_Example" rel="nofollow">monster.yaml and main.cpp example</a></p> <p>my compiler on issuing the command <code>gcc main.cpp</code>, it issues the following errors:</p> <blockquote> <p>main.cpp:24:25: error: ‘YAML’ does not name a type<br> main.cpp:24:35: error: expected unqualified-id before ‘&amp;’ token<br> main.cpp:24:35: error: expected ‘)’ before ‘&amp;’ token<br> main.cpp:24:35: error: expected initializer before ‘&amp;’ token<br> main.cpp:30:25: error: ‘YAML’ does not name a type<br> main.cpp:30:35: error: expected unqualified-id before ‘&amp;’ token<br> main.cpp:30:35: error: expected ‘)’ before ‘&amp;’ token<br> main.cpp:30:35: error: expected initializer before ‘&amp;’ token<br> main.cpp:35:25: error: ‘YAML’ does not name a type<br> main.cpp:35:35: error: expected unqualified-id before ‘&amp;’ token<br> main.cpp:35:35: error: expected ‘)’ before ‘&amp;’ token<br> main.cpp:35:35: error: expected initializer before ‘&amp;’ token </p> </blockquote> <p>I tried changing the include directive from <code>#include "yaml-cpp/yaml.h"</code> to <code>#include &lt;yaml.h&gt;</code>, since I'd installed the yaml lib, but this didn't solve anything.</p> <p>so what did I do wrong?</p> <p>Here is the problematic code pasted from lines 24 to 40: </p> <pre><code>void operator &gt;&gt; (const YAML::Node&amp; node, Vec3&amp; v) { node[0] &gt;&gt; v.x; node[1] &gt;&gt; v.y; node[2] &gt;&gt; v.z; } void operator &gt;&gt; (const YAML::Node&amp; node, Power&amp; power) { node["name"] &gt;&gt; power.name; node["damage"] &gt;&gt; power.damage; } void operator &gt;&gt; (const YAML::Node&amp; node, Monster&amp; monster) { node["name"] &gt;&gt; monster.name; node["position"] &gt;&gt; monster.position; const YAML::Node&amp; powers = node["powers"]; for(unsigned i=0;i&lt;powers.size();i++) { Power power; powers[i] &gt;&gt; power; monster.powers.push_back(power); } } </code></pre> <p>And here is a dump of the output of the <code>sudo make install</code> after I run the <code>make</code> command:</p> <pre><code>[ 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 </code></pre> <p>Could there probably be some special directive / option that I have to append to the <code>gcc</code> command when compiling with libyaml? something like <code>gcc main.cpp -libyaml</code> ?</p> <p>For more output from the compiler (<code>gcc version 4.5.0 20100604 [gcc-4_5-branch revision 160292] (SUSE Linux)</code>) :</p> <pre><code>/tmp/ccYltArL.o: In function `operator&gt;&gt;(YAML::Node const&amp;, Monster&amp;)': 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&lt;char, std::char_traits&lt;char&gt; &gt;::basic_ifstream(char const*, std::_Ios_Openmode)' main.cpp:(.text+0x215): undefined reference to `YAML::Parser::Parser(std::basic_istream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;)' main.cpp:(.text+0x224): undefined reference to `YAML::Node::Node()' main.cpp:(.text+0x23e): undefined reference to `YAML::Parser::GetNextDocument(YAML::Node&amp;)' main.cpp:(.text+0x29c): undefined reference to `std::cout' </code></pre> <p>and much more stuff than can <em>fit here</em> , finally ending in:</p> <pre><code>/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 </code></pre> <p>-- finally --</p> <p><strong>Solved it</strong> with Chris's soln (see below), though I actually found an even cleaner way of pre-loading 'custom' libraries, by using the <code>ldconfig</code> command to configure the library search path after i add the path to my libs into a <code>*.conf</code> file in <code>/etc/ld.so.conf.d/</code>. See the <a href="http://www.cyberciti.biz/faq/linux-setting-changing-library-path/" rel="nofollow">detailed guide here...</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload