简体   繁体   English

如何使用Boost库构建C ++代码

[英]how to build the c++ code with boost library

what I want to do is a c++ code which utilize boost library and do a simple RS232 communication. 我想做的是利用Boost库并进行简单RS232通信的C ++代码。 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. 提前致谢。

The compiler option -Lboost_1_64_0/libs/ just tells the compiler to look in that directory for libraries. 编译器选项-Lboost_1_64_0/libs/只是告诉编译器在该目录中查找库。 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. 根据boost文档,您将需要boost_system库,因此将-lboost_system添加到编译器选项中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM