2012-03-01 90 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