TriHard8 TriHard8 - 1 month ago 15
C++ Question

libconfig Undefined symbols for architecture x86_64

I'm trying to run my first basic program with libconfig in c++. I compiled with

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


When I run on rhel6, I get the following error (only the first line):

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


When I run on Mac Darwin Kernel 15.5.0, I get this error:

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)


This is just an example from downloaded libconfig-1.5.tar.gz file. I went through the makefile process on the mac, got that error, then brew installed libconfig-1.6 and still received the same error. I'm at a loss here. Any help is appreciated.

Answer

You need to link the library to your executable. Look at the section entitled "Linking with libraries".

As stated in the documentation add -lconfig++ to your compiler flags and it should work.

Basicly this tells the compiler where to find the library sources (classes, functions, etc) so that they can be properly imported in your program when needed.