简体   繁体   English

未定义的对LibSerial的依据

[英]undefined referance to LibSerial

So i'm writing a serial transmision program, and have just changed over to using C++, it been a while since I used C++ (I've been working with C recently, and before that java) 所以我正在编写一个串行传输程序,并且刚刚转为使用C ++,因为我使用了C ++已经有一段时间了(我最近一直在使用C,之前是java)

Now I need to use LibSerial, (it seems much simpler to use than C's termios) 现在我需要使用LibSerial,(它似乎比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

This is obviously the linker complaining that i cannot find the functions reference by the libserial header file. 这显然是链接器抱怨我找不到libserial头文件的函数引用。 So if I look on my Linux system how the shared library is called: 因此,如果我在Linux系统上查看共享库的调用方式:

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

On my system this implies I would add -lserial as a g++ option (aka link with libserial.so) this would turn your compilation command into 在我的系统上,这暗示我将-lserial添加为g ++选项(也就是与libserial.so的链接),这会将您的编译命令转换为

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

Including the header file is not enough - you also need to link with the library that implements SerialStream. 包含头文件是不够的 - 您还需要链接实现SerialStream的库。 Assuming it is a static library called serstream.a (it is almost certainly actually called something else): 假设它是一个名为serstream.a的静态库(几乎可以肯定称为其他东西):

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

old thread, but i still use Libserial. 老线程,但我仍然使用Libserial。 here the completed answer My working setup. 这里完成的答案我的工作设置。

Ubuntu 18.04 g++ 7.3.0 Ubuntu 18.04 g ++ 7.3.0

1) Install package for libserial 1)安装libserial包

apt install libserial-dev

2) check for your headers(.h) and .so files 2)检查标题(.h)和.so文件

dpkg -l libserial0
dpkg -l libserial-dev

the first command give you the directory of shared library and the second gives you the headers location. 第一个命令为您提供共享库的目录,第二个命令为您提供标题位置。

3) Your code. 3)你的代码。

I have to change a little your code, first i delete the custom header and modifing the constuctor call to this. 我必须更改你的代码,首先我删除自定义标头并修改constuctor调用。

SerialStream theSerialStream;

4) compile with g++ 4)用g ++编译

Here my compiling command 这是我的编译命令

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

check for the -lpthread linking option, beacuse Libserial uses mutex. 检查-lpthread链接选项,因为Libserial使用互斥锁。

在Ubuntu / Debian中确保你必须安装libserial-dev包并使用gcc的'-lserial'标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM