我正在使用Ubuntu 14.04。
我想将libSerial
用于描述here之类的项目。
我使用sudo apt-get install libserial-dev
安装了库。
我写了一个小程序(好吧,它不是真正的程序):
#include "SerialStream.h"
#include <iostream>
#define PORT "/dev/ttyUSB0"
SerialStream ardu;
using namespace std;
using namespace LibSerial;
但是编译器说:‘SerialStream’ does not name a type
任何帮助?
编辑:
在命名空间行之后放置SerialStream ardu
之后问题更加奇怪:
g++ main.cpp -o ArduCom
/tmp/ccNzzINg.o: In Funktion `main':
main.cpp:(.text+0x7a): not defined reference to `LibSerial::SerialStream::Open(std::string, std::_Ios_Openmode)'
main.cpp:(.text+0x12d): not defined reference to `LibSerial::SerialStream::SetBaudRate(LibSerial::SerialStreamBuf::BaudRateEnum)'
main.cpp:(.text+0x181): not defined reference to `LibSerial::SerialStream::SetCharSize(LibSerial::SerialStreamBuf::CharSizeEnum)'
main.cpp:(.text+0x1d5): not defined reference to `LibSerial::SerialStream::SetParity(LibSerial::SerialStreamBuf::ParityEnum)'
main.cpp:(.text+0x229): not defined reference to `LibSerial::SerialStream::SetNumOfStopBits(short)'
main.cpp:(.text+0x27d): not defined reference to `LibSerial::SerialStream::SetFlowControl(LibSerial::SerialStreamBuf::FlowControlEnum)'
/tmp/ccNzzINg.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[_ZTVN9LibSerial15SerialStreamBufE]+0x48): not defined reference to `LibSerial::SerialStreamBuf::showmanyc()'
/tmp/ccNzzINg.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[_ZTVN9LibSerial15SerialStreamBufE]+0x50): not defined reference to `LibSerial::SerialStreamBuf::xsgetn(char*, long)'
/tmp/ccNzzINg.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[_ZTVN9LibSerial15SerialStreamBufE]+0x58): not defined reference to `LibSerial::SerialStreamBuf::underflow()'
/tmp/ccNzzINg.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[_ZTVN9LibSerial15SerialStreamBufE]+0x68): not defined reference to `LibSerial::SerialStreamBuf::pbackfail(int)'
/tmp/ccNzzINg.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[_ZTVN9LibSerial15SerialStreamBufE]+0x70): not defined reference to `LibSerial::SerialStreamBuf::xsputn(char const*, long)'
/tmp/ccNzzINg.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[_ZTVN9LibSerial15SerialStreamBufE]+0x78): not defined reference to `LibSerial::SerialStreamBuf::overflow(int)'
答案 0 :(得分:1)
首先,您已经从命名空间SerialStream
定义了LibSerial
类型的对象,而没有正确地限定:
#include "SerialStream.h"
#include <iostream>
#define PORT "/dev/ttyUSB0"
SerialStream ardu; // error this type is unqualified
using namespace std;
using namespace LibSerial;
有很多方法可以限定类型。一种方法是将定义
#include "SerialStream.h"
#include <iostream>
#define PORT "/dev/ttyUSB0"
using namespace std;
using namespace LibSerial;
// qualified by the compiler searching through the
// declared namespaces and finding it in `namespace LibSerial`.
SerialStream ardu;
另一种方法是将typename明确限定为属于特定命名空间的
:LibSerial::SerialStream ardu; // fully qualifies name
完成此操作后,您的第二个问题是编译器需要知道libserial
库在文件系统中的位置,以便能够链接到它。
假设您已将库安装到默认位置,那么这可能就足够了:
g++ main.cpp -o ArduCom -lserial
否则,您可能必须指定库使用-L开关的位置:
g++ main.cpp -o ArduCom -Wl,-rpath,/path/to/library/folder -L/path/to/library/folder -lserial
libserial
库是使用Autotools构建的,因此,如果您从源代码构建并安装它,则会在 install 过程中提供链接说明。
说明书有点像这样:
----------------------------------------------------------------------
Libraries have been installed in:
/path/to/libserial/lib
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the 'LD_RUN_PATH' environment variable
during linking
- use the '-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to '/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------