2016-10-20 112 views
0

我想在C++中使用libconfig運行我的第一個基本程序。我編了g++ testing.cpp -o testinglibconfig未定義的體系結構x86_64的符號

#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內核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,仍然收到相同的錯誤。我在這裏不知所措。任何幫助表示讚賞。

+0

你可以添加你使用的編譯器命令嗎?你使用什麼旗幟? – ShadowMitia

+0

@ShadowMitia我將命令添加到開頭。我只用了一個簡單的'g ++ testing.cpp -o testing'。所以沒有特殊的標誌。 – TriHard8

回答

0

您需要將庫鏈接到您的可執行文件。

documentation中所述,將-lconfig++添加到您的編譯器標誌,它應該工作。

基本上,它告訴編譯器在哪裏找到庫源(類,函數等),以便它們可以在需要時正確導入到程序中。

相關問題