Reputation: 706
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
.
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
Any help?
EDIT:
After placing the SerialStream ardu
after the namespace-line the problem is even more strange:
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)'
Upvotes: 0
Views: 2320
Reputation: 48635
First of all you have defined an object of type SerialStream
from the namespace LibSerial
without properly qualifying it:
#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:
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.
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:
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.
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.
----------------------------------------------------------------------
Upvotes: 1