【问题标题】:yaml-cpp: Linking failedyaml-cpp:链接失败
【发布时间】:2012-03-01 10:11:34
【问题描述】:

我想使用 yaml-cpp 库来读取一些 YAML 字符串。为此,我尝试编译以下 C++ 代码:

#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 的输出:

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

ls -l /usr/local/lib的输出:

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

我目前使用的是 0.3.0 版本,但我还检查了当前存储库的副本。

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

【问题讨论】:

    标签: linker-errors yaml-cpp


    【解决方案1】:

    几种可能性:

    1. yaml-cpp 的共享库版本的旧版本是否有可能以某种方式在您的库路径中?

    2. 如果您尝试在命令行中首先使用test.cpp 进行编译会发生什么,例如,

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

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

    【讨论】:

    • 谢谢!我的 /usr/lib 中有一个非常旧的 libyaml。删除后,一切正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多