How to use yaml-cpp in a c ++ program on linux?

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

The problem is that even after successfully installing cmake, compiling yaml-cpp, as shown on the yaml-cpp docs page, I still can’t compile simple demos on the yaml-cpp page here .

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

my compiler, when issuing the gcc main.cpp command, it throws the following errors:

main.cpp: 24: 25: error: "YAML does not name type
main.cpp: 24: 35: error: expected unqualified identifier before & token
main.cpp: 24: 35: error: expected ') to' & marker
main.cpp: 24: 35: error: expected initializer before & token
main.cpp: 30: 25: error: "YAML does not name type
main.cpp: 30: 35: error: expected unqualified identifier before & token
main.cpp: 30: 35: error: expected ') to' & marker
main.cpp: 30: 35: error: expected initializer before & token
main.cpp: 35: 25: error: "YAML does not name type
main.cpp: 35: 35: error: expected unqualified identifier before & token
main.cpp: 35: 35: error: expected ') to' & marker
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 installed yaml lib, but that didn't solve anything.

what did I do wrong?

Here is the problematic code inserted from lines 24 through 40:

 void operator >> (const YAML::Node& node, Vec3& v) { node[0] >> vx; node[1] >> vy; node[2] >> vz; } 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 the output dump of sudo make install after running the 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 

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

To get more results 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 that can fit here, ending:

 /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 soln (see below), although I did find a cleaner way to preload the β€œcustom” libraries using the ldconfig command to configure the library search path after adding the path to my libs to the *.conf file in /etc/ld.so.conf.d/ . See the detailed manual here for more details.

+8
c ++ yaml yaml-cpp
source share
2 answers

gcc is not requested to search in / usr / local. You need to do this explicitly. Also, it really should be g ++, not gcc. So first make sure your inclusion looks like this:

 #include "yaml-cpp/yaml.h" 

Then compile it as follows:

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

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

 #include "yaml-cpp/yaml.h" 

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

Does an error message appear in the include statement line? (Can you post the full compiler output?)

0
source share

All Articles