如何在linux上编译libserial
How to compile libserial on linux?
我的程序需要与串行设备通信。我正在Ubuntu 14.04 LTS上开发它。我找到libserial,并试图使用它,但它不编译。我下载、编译并安装了v0.6.0rc2。是的,它是一个rc,但它在sourceforge上更受欢迎,而且稳定版本非常旧。如果你知道更好的图书馆,请在评论中告诉我。
根据文档给出的以下代码无法编译:
#include <SerialStream.h>
//
using namespace LibSerial ;
main()
{
SerialStream my_serial_stream ;
//
// Open the serial port for communication.
//
my_serial_stream.Open( "/dev/ttyS0" ) ;
}
这是我得到的错误:
dalton@dalton-SVF14212SNW:~$ g++ test.cpp
/tmp/ccyWIbXN.o: In function `main':
test.cpp:(.text+0x17): undefined reference to `LibSerial::SerialStream::SerialStream()'
test.cpp:(.text+0x6d): undefined reference to `LibSerial::SerialStream::Open(std::string, std::_Ios_Openmode)'
test.cpp:(.text+0x9a): undefined reference to `LibSerial::SerialStream::~SerialStream()'
test.cpp:(.text+0xd6): undefined reference to `LibSerial::SerialStream::~SerialStream()'
collect2: error: ld returned 1 exit status
我已经将有问题的行更改为my_serial_stream.Open( "/dev/ttyS0" , ios::out ) ;
并添加了名称空间std
。现在我得到这个错误:
dalton@dalton-SVF14212SNW:~$ g++ test.cpp
/tmp/ccUhSQVA.o: In function `main':
test.cpp:(.text+0x17): undefined reference to `LibSerial::SerialStream::SerialStream()'
test.cpp:(.text+0x5f): undefined reference to `LibSerial::SerialStream::Open(std::string, std::_Ios_Openmode)'
test.cpp:(.text+0x8c): undefined reference to `LibSerial::SerialStream::~SerialStream()'
test.cpp:(.text+0xc8): undefined reference to `LibSerial::SerialStream::~SerialStream()'
collect2: error: ld returned 1 exit status
您需要将库链接到您的代码。
尝试使用
g++ test.cpp -L<PATH_TO_LIB_SERIAL_LIBRARY_DIR> -lserial
假设您在/usr/local/lib中安装了libserial。或libserial。它在/usr/local/lib中)您将使用以下命令编译并链接您的程序
g++ test.cpp -L/usr/local/lib -lserial
您发布的错误输出显示错误发生在链接阶段。
参见什么是未定义的引用/未解析的外部符号错误以及如何修复它?
查看可能导致链接错误的更多信息。
在这种情况下,看起来你没有链接到库