0

what I want to do is a c++ code which utilize boost library and do a simple RS232 communication. I got the code like following:

#include <boost/asio.hpp> // include boost
using namespace::boost::asio;  // save tons of typing
#include <iostream>
using std::cin;

// These are the values our port needs to connect
#ifdef _WIN32
// windows uses com ports, this depends on what com port your cable is plugged in to.
    const char *PORT = "COM3";
#else
// Mac OS ports
    const char *PORT = "/dev/tty.usbserial";
#endif
// Note: all the following except BAUD are the exact same as the default values

serial_port_base::baud_rate BAUD(19200);
serial_port_base::character_size C_SIZE( 8 );
serial_port_base::flow_control FLOW( serial_port_base::flow_control::none );
serial_port_base::parity PARITY( serial_port_base::parity::none );
serial_port_base::stop_bits STOP( serial_port_base::stop_bits::one );

int main()
{
    io_service io;
    serial_port port( io, PORT );
    port.set_option( BAUD );
    port.set_option( C_SIZE );
    port.set_option( FLOW );
    port.set_option( PARITY );
    port.set_option( STOP );

    unsigned char command[1] = {0};

    // read in user value to be sent to device
    int input;
    cin >> input;

    // The cast will convert too big numbers into range.
    while( input >= 0 )
    {
        // convert our read in number into the target data type
        command[0] = static_cast<unsigned char>( input );
        write( port, buffer( command, 1 ) );

        // read in the next input value
        cin >> input;
    }

    // all done sending commands
    return 0;
}

and I am building the code with following command:

c++ -Iboost_1_64_0 -Lboost_1_64_0/libs/ -stdlib=libc++ PortConfig.cpp -o PortConfig

but the terminal keeps giving me error:

Undefined symbols for architecture x86_64:
  "boost::system::system_category()", referenced from:
      boost::asio::error::get_system_category() in PortConfig-2187c6.o
      boost::system::error_code::error_code() in PortConfig-2187c6.o
      ___cxx_global_var_init.2 in PortConfig-2187c6.o
  "boost::system::generic_category()", referenced from:
      ___cxx_global_var_init in PortConfig-2187c6.o
      ___cxx_global_var_init.1 in PortConfig-2187c6.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

could anyone help me on that? Thanks in advance.

Dillion F
  • 3
  • 1

1 Answers1

0

The compiler option -Lboost_1_64_0/libs/ just tells the compiler to look in that directory for libraries. You still need to specify which libraries to link. According to the boost documentation you will need the boost_system library, so add -lboost_system to the compiler options.

The corrected compile command should look something like this

c++ -Iboost_1_64_0 -Lboost_1_64_0/libs/ -lboost_system -stdlib=libc++ PortConfig.cpp -o PortConfig
jodag
  • 19,885
  • 5
  • 47
  • 66
  • Please delete the line "Boost ASIO is a header-only library so you don't need to link anything specifically for it.". `boost asio` is header only, but it depends on `boost_system` which is why it requires `-lboost_system` to be added to the compiler options. – kenba Aug 10 '17 at 07:07
  • I tried your solution but gives me following error: ld: library not found for -lboost_system clang: error: linker command failed with exit code 1 (use -v to see invocation). but I do have boost installed under that location. – Dillion F Aug 10 '17 at 16:34
  • @DillionF Try changing the `boost_1_64_0/libs/` part of the `-Lboost_1_64_0/libs/` option to the absolute path where boost libs are located. There needs to be a file named `libboost_system.a` or `libboost_system.so`, or something along those lines in the folder indicated by `-L`. See the answer to [this question](https://stackoverflow.com/questions/32929709/the-l-option-in-gcc) which also also applies to clang. – jodag Aug 10 '17 at 17:12