matanc1
matanc1

Reputation: 7013

How to solve undefined symbol for architecture x86_64

I'm trying to compile my class and I get the following result.

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)

This is the class that I'm trying to compile:

#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);
  }
}

How can I fix this and get the code to compile? I'm running a Mac OSX 10.9.

Upvotes: 1

Views: 2685

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409136

I'm only guessing here, and that guess is that the code you shown is not part of the test.cpp file, but is in a separate file. That means your build command

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

will only build test.cpp. It will not automatically add other source files to the compilation process.

You have to explicitly build with all relevant source files, like

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

Upvotes: 3

Related Questions