2014-03-26 56 views
1

我想編譯我的類,我得到以下結果。如何解決架構x86_64的未定義符號

g++ test.cpp -lconfig++ -stdlib=libstdc++ 
Undefined symbols for architecture x86_64: 
    "CommandParser::parseCommand(std::string)", referenced from: 
     _main in test-8f6e3f.o 
    "CommandParser::CommandParser()", referenced from: 
     _main in test-8f6e3f.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

這是我想要編譯類:

#include "commandparser.h" 

using namespace libconfig; 

CommandParser::CommandParser(string configFile){ 
    this->configFile = configFile; 
} 


CommandParser::CommandParser(){ 
    this->configFile = CONFIG_FILE_NAME; 
} 

string CommandParser::parseCommand(string cmd){ 
    Config cfg; 

    try{ 
    cfg.readFile(this->configFile.c_str()); 
    } 
    catch (const FileIOException &fi){ 
    cerr << "I/O error while reading file." << std::endl; 
    exit(1); 
    } 
    catch (const ParseException &pe){ 
    cerr << "Parse error at " << pe.getFile() << ":" << pe.getLine() 
       << " - " << pe.getError() << endl; 
    exit(1); 
    } 

    try{ 
    string path = cfg.lookup(cmd); 
    cout << "The path to the script is: " << path << endl; 
    return path; 
    } 
    catch(const SettingNotFoundException &e){ 
    cerr << "No command with the name '"<<cmd<<"'"<<endl; 
    exit(1); 
    } 
} 

我怎樣才能解決這個問題,並讓代碼編譯? 我正在運行Mac OSX 10.9。

+0

並聲明所有這些在你的.h然後包括你的主要的.H? –

回答

3

我只是在這裏猜測,而且這個猜測是你顯示的代碼不是test.cpp文件的一部分,而是在一個單獨的文件中。這意味着您的構建命令

$ g++ test.cpp -lconfig++ -stdlib=libstdc++ 

只會建立test.cpp。它不會自動將其他源文件添加到編譯過程中。

你必須明確地與所有相關的源文件建立,像

$ g++ test.cpp commandparser.cpp -lconfig++ -stdlib=libstdc++ 
+0

謝謝你解決了! 我忘了添加像你說的源文件。 你碰巧知道我怎樣才能讓Eclipse鏈接所有相關的源文件?如果我手動編譯它似乎工作,但我仍然在日食中得到錯誤。 – Shookie

+0

@Shookie我曾經在Eclipse上工作過一段時間,但是不應該在項目中添加文件嗎? –

+0

它在項目中,但我仍然得到這個錯誤 – Shookie

相關問題