Skip to content

Undefined reference ... But all is linked

An answer to this question on Stack Overflow.

Question

I have a link problem with CodeBlocks IDE. But I think everything should be fine.

I want to work with laslib library. They provide a .lib file which I've linked in CodeBlocks IDE (such i usually did ...). Then I've included the "inc" folder which containing .hpp includes.

Atm, I did the minimal requirement to include an external library.

Then, in my "main" function, I've included the necessary files, the CodeBlocks intellisense provide me the prototypes of functions (so links are good I guess).

Here is my code :

#include <iostream>
#include <vector>
#include "lasreader.hpp"
#include "laswriter.hpp"
#include "lasdefinitions.hpp"
#define FILENAME "D:\\las.las"
#define FILEOUT "D:\\out"
int main(int argc, char** argv)
{
// Assume that's correct for simplicity @TODO : check argv
std::string filename = FILENAME;//argv[1];
std::string file_out = FILEOUT; //argv[2];
LASreadOpener lasreadopener;
LASwriteOpener laswriteopener;
lasreadopener.set_file_name(filename.c_str());
laswriteopener.set_file_name(file_out.c_str());
lasreadopener.parse(0, NULL); // parse without args
// Declare lasreader
LASreader* lasreader;
// Open the file
lasreader = lasreadopener.open();
//  Create and open the writer
LASwriter* laswriter = laswriteopener.open(&lasreader->header);
// Loop through the points (note they will already be filtered)
while (lasreader->read_point())
{
    // Show coordinates
    std::cout << lasreader->point.get_x() << ", " << lasreader->point.get_y() << ", " << lasreader->point.get_z() << std::endl;
    // Write point
    laswriter->write_point(&lasreader->point);
    // Add it to the inventory (keeps track of min/max values for the header)
    laswriter->update_inventory(&lasreader->point);
}
laswriter->update_header(&lasreader->header, TRUE);
laswriter->close();
lasreader->close();
delete laswriter;
delete lasreader;
}

And errors are :

main.cpp|17|undefined reference to `LASreadOpener::LASreadOpener()'|
main.cpp|18|undefined reference to `LASwriteOpener::LASwriteOpener()'|

etc ...

I didnt see what I've did wrong ... I've already tried the "hard" method with copying .hpp and .lib in mingw corresponding folders ...

Any ideas?

Answer

With my file structure as follows:

./LAStools
./myproject

From within ./myproject I compiled like so:

g++ main.cpp -I../LAStools/LASlib/inc/ -I../LAStools/LASzip/src -llas -L.

This resulted in an error free compilation of the code:

#include "lasreader.hpp"
#include "laswriter.hpp"
int main(int argc, char *argv[]){
  LASreadOpener lasreadopener;
  lasreadopener.set_file_name("original.las");
  LASreader* lasreader = lasreadopener.open();
  while (lasreader->read_point()) {}
  lasreader->close();
  delete lasreader;
  return 0;
}