Undefined link to LibSerial

So, I am writing a serial transfer program and have just switched to using C ++, it has been a long time since I used C ++ (I recently worked with C, and before that java)

Now I need to use LibSerial, (it seems much easier to use than C termios)

my code is:

//gen1.cpp
#include "string2num.h" // a custom header
#include <iostream>
#include <SerialStream.h>
using namespace LibSerial;
//using namespace std;

int main(int argc, char*argv[])
{
        if (argc<2)
        {
                std::cout<<argv[0]<<"requires the device name eg \"dev/tty0\" as a parameter\nterminating.\n";
                return 1;
        }    
        SerialStream theSerialStream(argv[1]); //open the device
        return 0;
}

When I compile the output:

g++ -Wall -o gen1 gen1.cpp string2num.o
/tmp/cchPBWgx.o: In function `main':
gen1.cpp:(.text+0x121): undefined reference to `LibSerial::SerialStream::SerialStream(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::_Ios_Openmode)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x24): undefined reference to `LibSerial::SerialStreamBuf::showmanyc()'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x28): undefined reference to `LibSerial::SerialStreamBuf::xsgetn(char*, int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x2c): undefined reference to `LibSerial::SerialStreamBuf::underflow()'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x34): undefined reference to `LibSerial::SerialStreamBuf::pbackfail(int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x38): undefined reference to `LibSerial::SerialStreamBuf::xsputn(char const*, int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x3c): undefined reference to `LibSerial::SerialStreamBuf::overflow(int)'
collect2: ld returned 1 exit status
make: *** [gen1] Error 1
+4
source share
4 answers

This linker complains that it cannot find the functions referenced by the libserial header file.

If I look on my Linux system to see what the shared library is called:

$ dpkg -L libserial0
...
/usr/lib/libserial.so.0.0.0
/usr/lib/libserial.so.0

, -lserial g++ ( libserial.so),

g++ -Wall -lserial -o gen1 gen1.cpp string2num.o
+6

- , SerialStream. , , serstream.a( ):

g++ -Wall -o gen1 gen1.cpp string2num.o serstream.a
+2

, Libserial. .

Ubuntu 18.04 g++ 7.3.0

1) libserial

apt install libserial-dev

2) (.h) .so

dpkg -l libserial0
dpkg -l libserial-dev

, - .

3) .

, .

SerialStream theSerialStream;

4) g++

g++ -o test -I/usr/include test.cpp -L/usr/lib/x86_64-linux-gnu -lserial -lpthread

-lpthread, Libserial .

+1
source

On Ubuntu / Debian, make sure you have the libserial-dev package installed and use the -lerial flag for gcc.

0
source

Source: https://habr.com/ru/post/1732986/


All Articles