2012-03-01 203 views
2

我想使用yaml-cpp库来读取一些YAML字符串。为了做到这一点,我尝试编译下面的C++代码:yaml-cpp:链接失败

#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; 
} 

但是,按预期它不工作,当我运行链接器引发以下错误消息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 

输出的pkg-config --cflags --libs yaml-cpp

输出 ls -l /usr/local/lib
-I/usr/local/include -L/usr/local/lib -lyaml-cpp 

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

我目前使用的是0.3.0版本,但我也检出了当前版本库的副本。

有谁知道如何解决这个问题?

回答

3

几个可能的原因:

  1. 是否有机会老版本的YAML-CPP的共享库的版本是在你的库路径不知何故?

  2. ,如果你试图用test.cpp首先在命令行编译什么happes,例如,

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

(顺便说一句,更多的谷歌搜索能力,这是一个静态链接的问题,而不是编译问题。)

+0

谢谢!/usr/lib中有一个很老的libyaml。我删除它后,一切正常。 – SecStone 2012-03-04 13:26:57