2015-08-16 78 views
0

我使用的是Ubuntu 14.04。 我想使用libSerial進行項目描述here。 我使用sudo apt-get install libserial-dev安裝了庫。'SerialStream'沒有指定類型

我寫了一個小程序(當然,這不是一個真正的程序):

#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)' 
+2

你嘗試把'SerialStream ardu;''在使用命名空間後LibSerial;'聲明? –

+0

@Galik; sry,錯字,不會改變問題... – PEAR

+1

@PEAR你的編輯沒有使任何更清晰的?試試我告訴你要做的,或者寫'LibSeria :: SerialStream ardu;'。 –

回答

1

首先,你已經從命名空間LibSerial沒有正確預選賽定義SerialStream類型的對象它:

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

另一種方式是資格類型名稱明確地作爲在一個特定的命名空間屬於:

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