我的程序需要与串行设备通信。我正在Ubuntu14.04LTS上开发它。我找到了libserial,并尝试使用它,但它不编译。我下载、编译和安装了0.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发布于 2015-07-13 18:20:03
您需要将库链接到代码。
试着使用
g++ test.cpp -L<PATH_TO_LIB_SERIAL_LIBRARY_DIR> -lserial假设您在/usr/local/lib (liberial.a或libserial.so位于/usr/local/lib中)中安装了libserial,您将使用以下命令编译和链接您的程序
g++ test.cpp -L/usr/local/lib -lserial您发布的错误输出显示错误发生在链接阶段。
请参阅What is an undefined reference/unresolved external symbol error and how do I fix it?
有关可能导致链接错误的原因的更多信息。
在这种情况下,您似乎没有链接到库。
https://stackoverflow.com/questions/31390013
复制相似问题