“SerialStream”没有命名类型

时间:2022-03-19 01:11:24

I'm using Ubuntu 14.04. I want to use libSerial for project like described here. I installed the library using sudo apt-get install libserial-dev.

我使用Ubuntu 14.04。我想将libSerial用于这里描述的项目。我使用sudo apt-get安装libserial-dev安装了库。

I've written a little program (well, it's not really a program):

我写了一个小程序(嗯,它不是一个真正的程序):

#include "SerialStream.h"
#include <iostream>

#define PORT "/dev/ttyUSB0"

SerialStream ardu;

using namespace std;
using namespace LibSerial;

But the compiler says: ‘SerialStream’ does not name a type

但是编译器说:“SerialStream”没有命名类型

Any help?

任何帮助吗?

EDIT: After placing the SerialStream ardu after the namespace-line the problem is even more strange:

编辑:在命名空间行之后放置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)'

1 个解决方案

#1


1  

First of all you have defined an object of type SerialStream from the namespace LibSerial without properly qualifying it:

首先,您已经从名称空间LibSerial定义了一个类型为SerialStream的对象,但没有对其进行适当的限定:

#include "SerialStream.h"
#include <iostream>

#define PORT "/dev/ttyUSB0"

SerialStream ardu; // error this type is unqualified

using namespace std;
using namespace LibSerial;

There are a number of ways to qualify the type. One is by placing the definition after the using declaration of the namespace the type is defined in:

有许多方法可以限定类型。一种方法是在定义类型为:

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

Another way is to qualify the typename explicitly as belonging in a specific namespace:

另一种方法是将typename显式限定为特定名称空间中的所属:

LibSerial::SerialStream ardu; // fully qualifies name

Having done that, your second problem is that the compiler needs to know where the libserial library is located in the file system in order to be able to link to it.

这样做之后,您的第二个问题是编译器需要知道lib串行库位于文件系统中的什么位置,以便能够链接到它。

Assuming you installed the library to a default place then this maybe enough:

假设您将库安装到默认位置,那么这可能就足够了:

g++ main.cpp -o ArduCom -lserial

Otherwise you may have to specify where the library is using the -L switch:

否则,您可能必须指定库使用-L开关的位置:

g++ main.cpp -o ArduCom -Wl,-rpath,/path/to/library/folder -L/path/to/library/folder -lserial

The libserial library is built using Autotools so, if you built and installed it from source it provides linking instructions as part of the install process.

libserial库是使用Autotools构建的,因此,如果您从源代码构建并安装它,它将作为安装过程的一部分提供链接指令。

The instructions are somewhat like this:

说明是这样的:

----------------------------------------------------------------------
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.
----------------------------------------------------------------------

#1


1  

First of all you have defined an object of type SerialStream from the namespace LibSerial without properly qualifying it:

首先,您已经从名称空间LibSerial定义了一个类型为SerialStream的对象,但没有对其进行适当的限定:

#include "SerialStream.h"
#include <iostream>

#define PORT "/dev/ttyUSB0"

SerialStream ardu; // error this type is unqualified

using namespace std;
using namespace LibSerial;

There are a number of ways to qualify the type. One is by placing the definition after the using declaration of the namespace the type is defined in:

有许多方法可以限定类型。一种方法是在定义类型为:

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

Another way is to qualify the typename explicitly as belonging in a specific namespace:

另一种方法是将typename显式限定为特定名称空间中的所属:

LibSerial::SerialStream ardu; // fully qualifies name

Having done that, your second problem is that the compiler needs to know where the libserial library is located in the file system in order to be able to link to it.

这样做之后,您的第二个问题是编译器需要知道lib串行库位于文件系统中的什么位置,以便能够链接到它。

Assuming you installed the library to a default place then this maybe enough:

假设您将库安装到默认位置,那么这可能就足够了:

g++ main.cpp -o ArduCom -lserial

Otherwise you may have to specify where the library is using the -L switch:

否则,您可能必须指定库使用-L开关的位置:

g++ main.cpp -o ArduCom -Wl,-rpath,/path/to/library/folder -L/path/to/library/folder -lserial

The libserial library is built using Autotools so, if you built and installed it from source it provides linking instructions as part of the install process.

libserial库是使用Autotools构建的,因此,如果您从源代码构建并安装它,它将作为安装过程的一部分提供链接指令。

The instructions are somewhat like this:

说明是这样的:

----------------------------------------------------------------------
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.
----------------------------------------------------------------------