0

我正在使用 Ubuntu 14.04。我想用于这里libSerial描述的项目。我使用.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)'
4

1 回答 1

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;

有多种方法可以限定类型。一种是在定义类型的命名空间using 声明之后放置定义:

#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; 

另一种方法是将类型名明确限定为属于特定命名空间:

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构建的,因此,如果您从源代码构建和安装它,它会在安装过程中提供链接说明。

指令有点像这样:

----------------------------------------------------------------------
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.
----------------------------------------------------------------------
于 2015-08-17T14:46:00.967 回答