Search code examples
c++gcclibserial

libserial undefined reference to symbol


I compiled libserial-0.6.0rc2 from source in Ubuntu 16.04 and am now trying to compile a simple test program:

#include <SerialStream.h>
#include <iostream>
using namespace LibSerial;
int main(int argc, char** argv)
{
    SerialStream serial_port;
    serial_port.Open("/dev/ttyS0");
    serial_port.SetBaudRate( SerialStreamBuf::BAUD_9600 );
    serial_port.SetCharSize( SerialStreamBuf::CHAR_SIZE_8 );
    serial_port.SetParity( SerialStreamBuf::PARITY_EVEN );
    serial_port.SetNumOfStopBits(1);

    serial_port.write( "s", 1 ) ;
}

I'm getting the following error:

karl@karl-T430u:~/scan/arduino$ gcc *.cpp -Wl,-
rpath -Wl,/usr/local/lib -L/usr/local/lib -lserial
/usr/bin/ld: /tmp/ccT3ucCm.o: undefined reference to symbol 
'_ZNSaIcED1Ev@@GLIBCXX_3.4'
//usr/lib/x86_64-linux-gnu/libstdc++.so.6: error adding symbols: DSO 
missing from command line
collect2: error: ld returned 1 exit status

It took me a while to compile libserial as there was apparently a required dependency on SIP which I didn't know about. I can build the examples using the provided makefile but I am not able to build my own examples with the installed libraries. Does anyone have any ideas?


Solution

  • You are attempting to compile and link a C++ program with the C compiler driver gcc. C and C++ are different languages. gcc does not by default link the Standard C++ library, libstdc++, so you have an undefined reference error to a symbol _ZNSaIcED1Ev (demangled = std::allocator<char>::~allocator()) that is defined in that library.

    To compile and link C++ programs use the C++ compiler driver, g++.