0

我正在尝试在 C++ 中使用 libconfig 运行我的第一个基本程序。我用g++ testing.cpp -o testing.

#include <iostream>
#include <cstdlib>
#include <libconfig.h++>

using namespace std;
using namespace libconfig;

int main(){

    Config cfg;

    // Read the file. If there is an error, report it and exit.
    //try
    //{
    cfg.readFile("/tmp/eg-report-hutapp02q-161017-08-26/eg.cfg");
    //}
    /*catch(const FileIOException &fioex)
    {
            cerr << "I/O error while reading file." << endl;
            return(EXIT_FAILURE);
    }
    catch(const ParseException &pex)
    {
            cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
                      << " - " << pex.getError() << std::endl;
            return(EXIT_FAILURE);
    }*/

    cout << "This compiled" << endl;

    return 0;
}

当我在 rhel6 上运行时,出现以下错误(仅第一行):

testing.cpp:(.text+0x13): undefined reference to ``libconfig::Config::Config()'

当我在 Mac Darwin Kernel 15.5.0 上运行时,我收到此错误:

Undefined symbols for architecture x86_64: "libconfig::Config::readFile(char const*)", referenced from: _main in testing-59bdf7.o "libconfig::Config::Config()", referenced from: _main in testing-59bdf7.o "libconfig::Config::~Config()", referenced from: _main in testing-59bdf7.o "typeinfo for libconfig::ParseException", referenced from: GCC_except_table0 in testing-59bdf7.o "typeinfo for libconfig::FileIOException", referenced from: GCC_except_table0 in testing-59bdf7.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

这只是下载的 libconfig-1.5.tar.gz 文件中的一个示例。我在mac上完成了makefile过程,得到了那个错误,然后brew安装了libconfig-1.6,仍然收到同样的错误。我在这里不知所措。任何帮助表示赞赏。

4

1 回答 1

0

您需要将库链接到您的可执行文件。

如文档中所述,添加-lconfig++到您的编译器标志中,它应该可以工作。

基本上,这告诉编译器在哪里可以找到库源(类、函数等),以便在需要时可以将它们正确地导入程序中。

于 2016-10-21T15:13:06.207 回答