SecStone
SecStone

Reputation: 1753

yaml-cpp: Linking failed

I'd like to use the yaml-cpp library to read some YAML strings. In order to do so, I try to compile the following C++ code:

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <openssl/sha.h>
#include <yaml-cpp/yaml.h>

int main(){
    std::string yamlstr = "name: Test\ndescription: Test2";

    std::stringstream is(yamlstr);
    YAML::Parser parser(is);
    YAML::Node doc;
    parser.GetNextDocument(doc);

    return 0;
}

However, it doesn't work as expected and the linker throws the following error messages when I run g++ $(pkg-config --cflags --libs yaml-cpp) test.cpp -o test:

/tmp/ccEPCiDN.o: In function `main':
test.cpp:(.text+0x1e0): undefined reference to `YAML::Parser::Parser(std::basic_istream<char, std::char_traits<char> >&)'
test.cpp:(.text+0x1ef): undefined reference to `YAML::Node::Node()'
test.cpp:(.text+0x209): undefined reference to `YAML::Parser::GetNextDocument(YAML::Node&)'
test.cpp:(.text+0x218): undefined reference to `YAML::Node::~Node()'
test.cpp:(.text+0x227): undefined reference to `YAML::Parser::~Parser()'
test.cpp:(.text+0x403): undefined reference to `YAML::Node::~Node()'
test.cpp:(.text+0x412): undefined reference to `YAML::Parser::~Parser()'
collect2: ld returned 1 exit status

Output of pkg-config --cflags --libs yaml-cpp:

-I/usr/local/include  -L/usr/local/lib -lyaml-cpp 

Output of ls -l /usr/local/lib:

[...]
-rw-r--r-- 1 root root    843138 2012-03-01 10:38 libyaml-cpp.a
[...]

I currently use the version 0.3.0 but I also checked out a copy of the current repository.

Does anyone know how to solve this problem?

Upvotes: 2

Views: 3934

Answers (1)

Jesse Beder
Jesse Beder

Reputation: 34044

A couple of possibilities:

  1. Is there a chance an old version of the shared library version of yaml-cpp is in your library path somehow?

  2. What happes if you try to compile with test.cpp first in the command line, e.g.,

    g++ test.cpp $(pkg-config --cflags --libs yaml-cpp) -o test
    

(By the way, for more googling power, this is a static linking problem, not a compiling problem.)

Upvotes: 3

Related Questions