我想要做的是一个C++代码,它利用boost库并做一个简单的RS232通信。我得到了这样的代码如下:如何用boost库建立C++代码
#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;
}
和我建立了代码与下面的命令:
c++ -Iboost_1_64_0 -Lboost_1_64_0/libs/ -stdlib=libc++ PortConfig.cpp -o PortConfig
但终端不断给我的错误:
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)
任何人都可以帮助我在那?提前致谢。
链接所需ibraries:'-lboost_system'或此类。 – user0042