0

I'm trying to get LibSerial to work but I am getting some linker errors when I try to compile my app using LibSerial. The errors are as follows:

/tmp/ccnVfHa2.o: In function `main':
index.cpp:(.text+0x26): undefined reference to `LibSerial::SerialStream::SerialStream()'
index.cpp:(.text+0x7c): undefined reference to `LibSerial::SerialStream::Open(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::_Ios_Openmode)'
index.cpp:(.text+0xa9): undefined reference to `LibSerial::SerialStream::Close()'
index.cpp:(.text+0xb8): undefined reference to `LibSerial::SerialStream::~SerialStream()'
index.cpp:(.text+0x108): undefined reference to`LibSerial::SerialStream::~SerialStream()'
collect2: error: ld returned 1 exit status

My code for a small setup app is:

#include <iostream>
#include <SerialStream.h>

using namespace LibSerial;

int main() {
    SerialStream sStream;
    sStream.Open("/dev/ttyUSB0");
    sStream.Close();
}

I am compiling with:

g++ -Wall -lserial index.cpp

But also tried:

g++ -l:libserial.so.0 index.cpp  

And the response from dpkg -L libserial0 is:

/.
/usr
/usr/share
/usr/share/doc
/usr/share/doc/libserial0
/usr/share/doc/libserial0/copyright
/usr/share/doc/libserial0/changelog.Debian.gz
/usr/lib
/usr/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu/libserial.so.0.0.0
/usr/lib/x86_64-linux-gnu/libserial.so.0

Thanks for taking a look!

Jlegend
  • 531
  • 1
  • 6
  • 19
  • Maybe similar to this: http://stackoverflow.com/questions/335928/ld-cannot-find-an-existing-library. Probably you need a `/usr/lib/x86_64-linux-gnu/libserial.so`. But it's missing. Try to create it `ln -s /usr/lib/x86_64-linux-gnu/libserial.so{.0,}` – stas.yaranov Dec 07 '16 at 11:51
  • @stas.yaranov It seems `/usr/lib/x86_64-linux-gnu/libserial.so` does exist. Not sure why dpkg doesn't show it but it is there. – Jlegend Dec 07 '16 at 11:57
  • 1
    Try changing the link order: `g++ -Wall index.cpp -lserial` – Paul R Dec 07 '16 at 12:03
  • 1
    Could you show what `ldconfig -p | grep libserial` prints. – stas.yaranov Dec 07 '16 at 12:06
  • 1
    @PaulR Was correct. I can't believe that was the problem. Does anyone know why that order matters? – Jlegend Dec 07 '16 at 13:45
  • 1
    @JLegendre: I believe it's due to the fact that the gcc linker by default just makes a single pass through the object files and libraries, in the order that they are given. This problem comes up quite regularly, so there are a lot of similar questions here on SO - I've closed this now as a duplicate of one of them. – Paul R Dec 07 '16 at 13:48
  • 1
    @PaulR Thanks for the help! – Jlegend Dec 07 '16 at 14:02

0 Answers0