user2249396
user2249396

Reputation: 31

Undefined reference ... But all is linked

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?

Upvotes: 3

Views: 839

Answers (2)

Gnat
Gnat

Reputation: 2921

Make sure that you are building your code for the same architecture as the library. By default, LASlib is built for 32-bit, but your compiler may default to 64-bit, so the compiler won't link them due to different method signatures.

You could try to either rebuild LASlib for 64-bit, or compile your own software for 32-bit.

Unfortunately your linker is hiding the problem somewhat, as it is demangling the method names. If you can set the compiler to output mangled names, this may help as it can highlight differences between 32-bit and 64-bit binaries. For example, decoding a mangled name (e.g. using https://demangler.com) can indicate where the linker is expecting a method returning a 64-bit pointer, but where the library has defined a method with the same name but which returns a 32-bit pointer.

Upvotes: 0

Richard
Richard

Reputation: 61479

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

Upvotes: 1

Related Questions