我使用LibSerial构建了一些代码,但是当我编译它时,我有这样的错误消息:
/tmp/ccUpE4te.o: In function `main':
test.c:(.text+0x8d): undefined reference to `LibSerial::SerialStream::Open(std::string, std::_Ios_Openmode)'
test.c:(.text+0xbf): undefined reference to `LibSerial::SerialStream::SetBaudRate(LibSerial::SerialStreamBuf::BaudRateEnum)'
test.c:(.text+0xd3): undefined reference to `LibSerial::SerialStream::SetCharSize(LibSerial::SerialStreamBuf::CharSizeEnum)'
test.c:(.text+0xe7): undefined reference to `LibSerial::SerialStream::SetNumOfStopBits(short)'
test.c:(.text+0xfb): undefined reference to `LibSerial::SerialStream::SetFlowControl(LibSerial::SerialStreamBuf::FlowControlEnum)'
test.c:(.text+0x10f): undefined reference to `LibSerial::SerialStream::SetVTime(short)'
test.c:(.text+0x123): undefined reference to `LibSerial::SerialStream::SetVMin(short)'
test.c:(.text+0x1a1): undefined reference to `LibSerial::SerialStream::Open(std::string, std::_Ios_Openmode)'
test.c:(.text+0x1d3): undefined reference to `LibSerial::SerialStream::SetBaudRate(LibSerial::SerialStreamBuf::BaudRateEnum)'
test.c:(.text+0x1e7): undefined reference to `LibSerial::SerialStream::SetCharSize(LibSerial::SerialStreamBuf::CharSizeEnum)'
test.c:(.text+0x1fb): undefined reference to `LibSerial::SerialStream::SetNumOfStopBits(short)'
test.c:(.text+0x20f): undefined reference to `LibSerial::SerialStream::SetFlowControl(LibSerial::SerialStreamBuf::FlowControlEnum)'
test.c:(.text+0x223): undefined reference to `LibSerial::SerialStream::SetVTime(short)'
test.c:(.text+0x237): undefined reference to `LibSerial::SerialStream::SetVMin(short)'
/tmp/ccUpE4te.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[_ZTVN9LibSerial15SerialStreamBufE]+0x48): undefined reference to `LibSerial::SerialStreamBuf::showmanyc()'
/tmp/ccUpE4te.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[_ZTVN9LibSerial15SerialStreamBufE]+0x50): undefined reference to `LibSerial::SerialStreamBuf::xsgetn(char*, long)'
/tmp/ccUpE4te.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[_ZTVN9LibSerial15SerialStreamBufE]+0x58): undefined reference to `LibSerial::SerialStreamBuf::underflow()'
/tmp/ccUpE4te.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[_ZTVN9LibSerial15SerialStreamBufE]+0x68): undefined reference to `LibSerial::SerialStreamBuf::pbackfail(int)'
/tmp/ccUpE4te.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[_ZTVN9LibSerial15SerialStreamBufE]+0x70): undefined reference to `LibSerial::SerialStreamBuf::xsputn(char const*, long)'
/tmp/ccUpE4te.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[_ZTVN9LibSerial15SerialStreamBufE]+0x78): undefined reference to `LibSerial::SerialStreamBuf::overflow(int)'
collect2: error: ld returned 1 exit status
我相信他们正在联系错误,但我无法理解我如何解决我的问题。
答案 0 :(得分:2)
解决方案:" gcc -L / usr / local / lib -lserial test.c"
这个解决方案意味着什么?
gcc不了解您希望使用的库。你应该告诉他。
gcc标志" -l"告诉gcc您要链接到计算机上安装的库的方式。
在您的情况下,您应指定:" -lserial"
您需要做的另一件事是让g ++知道在哪里找到这个库。
假设你没有做任何特别的事情,可以在/ usr / local / lib下找到libaray,并命名为libserial.so
你可以通过添加另一个标志" -L"来告诉gcc保存库的位置。 (它应该是-L / usr / local / lib)或者您可以使用局部变量来执行此操作:
export LD_LIBRARY_PATH = / usr / local / lib:$ LD_LIBRARY_PATH