所以我正在写一个串行传输程序,并且刚刚切换到使用C++,自从我使用C++已经有一段时间了(我最近一直在使用C,在java之前)
现在我需要使用LibSerial (它似乎比C的termios更容易使用)
我的代码是:
//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;
}当我编译输出时:
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发布于 2010-02-16 18:57:55
这是链接器抱怨它找不到由libserial头文件引用的函数。
如果我在我的Linux系统上查看如何调用共享库:
$ 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发布于 2010-02-16 18:46:27
仅包括头文件是不够的-您还需要链接到实现SerialStream的库。假设它是一个名为serstream.a的静态库(几乎可以肯定它实际上被称为其他名称):
g++ -Wall -o gen1 gen1.cpp string2num.o serstream.a发布于 2018-06-30 01:44:29
旧线程,但我仍然使用Libserial。这是我的工作设置的完整答案。
Ubuntu 18.04 g++ 7.3.0
1)安装libserial包
apt install libserial-dev2)检查头文件(.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使用互斥锁。
https://stackoverflow.com/questions/2272200
复制相似问题