如何解决架构x86_64的未定义符号

问题描述:

我想编译我的类,我得到以下结果。如何解决架构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? –

我只是在这里猜测,而且这个猜测是你显示的代码不是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