C++ 使用Boost.asio创建包装类时出现库链接器错误

C++ 使用Boost.asio创建包装类时出现库链接器错误,c++,serialization,boost,makefile,C++,Serialization,Boost,Makefile,我遇到一个错误,我的可执行文件没有正确链接项目中的所有文件。我遇到这个问题的部分原因是错误消息非常有趣和复杂,我很难弄清楚问题本身是什么。我很抱歉没有提供详细的错误,但我只是不确定发生了什么。在我的简单makefile中,我指定构建一个可执行文件 main: main.o serial.o g++ -o main main.o serial.o -Wall -lboost_system main.o: main.cpp serial.h g++ -c main.cpp s

我遇到一个错误,我的可执行文件没有正确链接项目中的所有文件。我遇到这个问题的部分原因是错误消息非常有趣和复杂,我很难弄清楚问题本身是什么。我很抱歉没有提供详细的错误,但我只是不确定发生了什么。在我的简单makefile中,我指定构建一个可执行文件

    main: main.o serial.o
    g++ -o main main.o serial.o -Wall -lboost_system
main.o: main.cpp serial.h
    g++ -c main.cpp
serial.o: serial.cpp serial.h
    g++ -c serial.cpp
clean:
    rm *.o main
我得到这个错误。如果您愿意,我可以通过makefile启用verbose

Undefined symbols for architecture x86_64:
  "Serial::open(std::string)", referenced from:
      _main in main-783b4e.o
  "Serial::Serial(std::string, boost::asio::io_service*)", referenced from:
      _main in main-783b4e.o
  "Serial::~Serial()", referenced from:
      _main in main-783b4e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [all] Error 1
iMats-2:DroneOS_test wfehrnstrom$ make
g++ -o main main.cpp -Wall -lboost_system 
Undefined symbols for architecture x86_64:
  "Serial::open(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
      _main in main-1e75b1.o
  "Serial::Serial(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, boost::asio::io_service*)", referenced from:
      _main in main-1e75b1.o
  "Serial::~Serial()", referenced from:
      _main in main-1e75b1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [all] Error 1
架构x86_64的未定义符号: “串行::打开(标准::字符串)”,引用自: _main in main-783b4e.o主管道 “串行::串行(std::字符串,boost::asio::io_服务*)”,引用自: _main in main-783b4e.o主管道 “序列::~Serial()”,引用自: _main in main-783b4e.o主管道 ld:找不到架构x86_64的符号 叮当声:错误:链接器命令失败,退出代码为1(使用-v查看调用) make:**[all]错误1 iMats-2:无人机测试wfehrnstrom$make g++-o main.cpp-Wall-lboost\u系统 架构x86_64的未定义符号: “串行::打开(标准::基本字符串)”,引用自: _干管中的干管-1e75b1.o “串行::串行(std::uu 1::基本_字符串,boost::asio::io_服务*)”,引用自: _干管中的干管-1e75b1.o “序列::~Serial()”,引用自: _干管中的干管-1e75b1.o ld:找不到架构x86_64的符号 叮当声:错误:链接器命令失败,退出代码为1(使用-v查看调用) make:**[all]错误1 导致此错误并已编译的代码体如下

#ifndef IOSTREAM_H
#include <iostream>
#endif

#ifndef SERIAL_H
#include "serial.h"
#endif

//#include <boost/asio.hpp>
#define PORT "/dev/ttyy.usbmodem1411"

int main(){
  boost::asio::io_service io;
  Serial::Serial serial(PORT, &io);
  serial.open(PORT);
  return 0;
}
\ifndef IOSTREAM\u H
#包括
#恩迪夫
#ifndef系列
#包括“serial.h”
#恩迪夫
//#包括
#定义端口“/dev/ttyy.usbmodem1411”
int main(){
boost::asio::io_服务io;
串行::串行串行(端口和io);
串行。打开(端口);
返回0;
}
Serial.h定义为

#include <boost/asio.hpp>
#include <iostream>
#ifndef SERIAL_H
#define SERIAL_H
class Serial{
public:
  Serial(std::string port_name,boost::asio::io_service* io, int baud_rate, std::string flow_control, std::string parity, float stop_bits);
  Serial(std::string port_name, boost::asio::io_service* io);
  ~Serial();
  void write(std::string stringToBuffer);
  std::string read(int inputBufferLength, std::string delimiter);
  void cancel();
  void open(std::string port_name);
  void close();
  bool is_open();

private:
  boost::asio::serial_port port_;
};
#endif
#包括
#包括
#ifndef系列
#定义序列号
类序列号{
公众:
串行(std::字符串端口名称、boost::asio::io服务*io、int波特率、std::字符串流量控制、std::字符串奇偶校验、浮点停止位);
串行(std::string端口名称,boost::asio::io\u服务*io);
~Serial();
无效写入(标准::字符串stringToBuffer);
std::string read(int-inputBufferLength,std::string分隔符);
作废取消();
无效打开(标准::字符串端口名称);
无效关闭();
布尔是开着的;
私人:
boost::asio::串行端口;
};
#恩迪夫
以及Serial的完整版本,它是一个基于boost的asio库的简单包装类

#ifndef IOSTREAM_H
#include <iostream>
#endif
#include "serial.h"
#include <boost/algorithm/string.hpp>

Serial::Serial(std::string port_name, boost::asio::io_service* io):port_(io, port_name){
  //In default constructor, set baud rate to 9600
  port_.set_option(boost::asio::serial_port::baud_rate(9600));
  //Flow Control: allow the ability to slow down serial data in a wire and even stop it
  port_.set_option(boost::asio::serial_port::flow_control(boost::asio::serial_port::flow_control::none));
  //Specify whether to send a bit at the end of each binary transmission indicating whether the number of bits was odd
  //or even.
  port_.set_option(boost::asio::serial_port::parity(boost::asio::serial_port::parity::none));
  //Specify how many stop bits there are at the end of the serial transmission
  port_.set_option(boost::asio::serial_port::stop_bits(boost::asio::serial_port::stop_bits::one));
  //open the serial port at the device name
  port_.open(port_name);
}

Serial::Serial(std::string port_name, boost::asio::io_service* io, int baud_rate, std::string flow_control, std::string parity, float stop_bits):port_(io, port_name){
  port_.set_option(boost::asio::serial_port::baud_rate(baud_rate));
  //Configure parity and check for all cases
  //if parity == none
  if(parity == "none"){
    port_.set_option(boost::asio::serial_port::parity(boost::asio::serial_port::parity::none));
  }
  //if parity is odd
  else if(parity == "odd"){
    port_.set_option(boost::asio::serial_port::parity(boost::asio::serial_port::parity::odd));
  }
  //if parity is even
  else if(parity == "even"){
    port_.set_option(boost::asio::serial_port::parity(boost::asio::serial_port::parity::even));
  }

  //Configure flow control
  if(flow_control ==  "software"){
    port_.set_option(boost::asio::serial_port::flow_control(boost::asio::serial_port::flow_control::software));
  }
  else if(flow_control=="hardware"){
    port_.set_option(boost::asio::serial_port::flow_control(boost::asio::serial_port::flow_control::hardware));
  }
  else{
    port_.set_option(boost::asio::serial_port::flow_control(boost::asio::serial_port::flow_control::none));
  }

  //Configure stop_bits
  if(stop_bits == 1.0){
    port_.set_option(boost::asio::serial_port::stop_bits(boost::asio::serial_port::stop_bits::one));
  }
  else if(stop_bits == 1.5){
    port_.set_option(boost::asio::serial_port::stop_bits(boost::asio::serial_port::stop_bits::onepointfive));
  }
  else{
    port_.set_option(boost::asio::serial_port::stop_bits(boost::asio::serial_port::stop_bits::two));
  }
}

Serial::~Serial(){
  std::cout << "Serial Port is being deleted" << std::endl;
}

void Serial::write(std::string in){
  try{
    port_.write_some(boost::asio::buffer(in));
  }
  catch(boost::system::system_error e){
    std::cout << "Serial Write Operation Failed" << std::endl;
  }
}

std::string Serial::read(int inputBufferLength,std::string delimiter){
  char readBuf[256];
  boost::asio::read(port_, boost::asio::buffer(readBuf, 256));
  std::string read(readBuf, 256);
  boost::algorithm::trim(read);
  return read;
}

void Serial::open(std::string port_name){
  port_.open(port_name);
}

void Serial::close(){
  port_.close();
}

bool Serial::is_open(){
  if(port_.is_open()){
    return true;
  }
  return false;
}

void Serial::cancel(){
  port_.cancel();
}
\ifndef IOSTREAM\u H
#包括
#恩迪夫
#包括“serial.h”
#包括
串行::串行(std::字符串端口名称,boost::asio::io\u服务*io):端口名称(io,端口名称){
//在默认构造函数中,将波特率设置为9600
端口设置选项(boost::asio::串行端口::波特率(9600));
//流量控制:允许降低串行数据传输速度,甚至停止串行数据传输
端口设置选项(boost::asio::串行端口::流量控制(boost::asio::串行端口::流量控制::无));
//指定是否在每个二进制传输结束时发送一个位,指示位数是否为奇数
//甚至。
端口\设置\选项(boost::asio::串行\端口::奇偶校验(boost::asio::串行\端口::奇偶校验::无));
//指定串行传输结束时有多少停止位
端口\设置\选项(boost::asio::串行\端口::停止\位(boost::asio::串行\端口::停止\位::一));
//打开设备名称处的串行端口
端口\打开(端口\名称);
}
串行::串行(std::字符串端口名称,boost::asio::io服务*io,整数波特率,std::字符串流量控制,std::字符串奇偶校验,浮点停止位):端口(io,端口名称){
端口设置选项(boost::asio::串行端口::波特率(波特率));
//为所有情况配置奇偶校验和检查
//如果奇偶性==无
如果(奇偶校验=“无”){
端口\设置\选项(boost::asio::串行\端口::奇偶校验(boost::asio::串行\端口::奇偶校验::无));
}
//如果奇偶校验是奇数
else if(奇偶校验=“奇数”){
端口\设置\选项(boost::asio::串行\端口::奇偶校验(boost::asio::串行\端口::奇偶校验::奇偶校验));
}
//如果奇偶是偶数
else if(奇偶性=“偶数”){
端口\设置\选项(boost::asio::串行\端口::奇偶校验(boost::asio::串行\端口::奇偶校验::偶数));
}
//配置流量控制
if(流量控制==“软件”){
端口设置选项(boost::asio::串行端口::流量控制(boost::asio::串行端口::流量控制::软件));
}
else if(流量控制==“硬件”){
端口设置选项(boost::asio::串行端口::流量控制(boost::asio::串行端口::流量控制::硬件));
}
否则{
端口设置选项(boost::asio::串行端口::流量控制(boost::asio::串行端口::流量控制::无));
}
//配置停止位
if(停止位==1.0){
端口\设置\选项(boost::asio::串行\端口::停止\位(boost::asio::串行\端口::停止\位::一));
}
else if(停止位==1.5){
端口设置选项(boost::asio::串行端口::停止位(boost::asio::串行端口::停止位::onepointfive));
}
否则{
端口设置选项(boost::asio::串行端口::停止位(boost::asio::串行端口::停止位::两位));
}
}
序列号::~Serial(){

std::cout当您像您一样使用命令进行构建时,必须显式列出要使用的所有源文件或对象文件。我已在makefile中指定了要使用的所有makefiles,但现在我收到了一个错误,当我在包装器类中实例化port_u时,如果您阅读了“注意事项”,则不应该出现该错误从第一条错误消息中,它会准确地告诉您出了什么问题以及需要做什么来修复它。
serial.cpp:7:68: error: no matching constructor for initialization of
      'boost::asio::serial_port' (aka 'basic_serial_port<>')
  ...port_name, boost::asio::io_service* io):port_(io, port_name){
                                             ^     ~~~~~~~~~~~~~
/usr/local/include/boost/asio/basic_serial_port.hpp:105:12: note: candidate
      constructor not viable: no known conversion from 'boost::asio::io_service
      *' to 'boost::asio::io_service &' for 1st argument; dereference the
      argument with *
  explicit basic_serial_port(boost::asio::io_service& io_service,
           ^
/usr/local/include/boost/asio/basic_serial_port.hpp:85:12: note: candidate
      constructor not viable: no known conversion from 'boost::asio::io_service
      *' to 'boost::asio::io_service &' for 1st argument; dereference the
      argument with *
  explicit basic_serial_port(boost::asio::io_service& io_service,
           ^
/usr/local/include/boost/asio/basic_serial_port.hpp:126:3: note: candidate
      constructor not viable: no known conversion from 'boost::asio::io_service
      *' to 'boost::asio::io_service &' for 1st argument; dereference the
      argument with *
  basic_serial_port(boost::asio::io_service& io_service,
  ^
/usr/local/include/boost/asio/basic_serial_port.hpp:69:12: note: candidate
      constructor not viable: requires single argument 'io_service', but 2
      arguments were provided
  explicit basic_serial_port(boost::asio::io_service& io_service)
           ^
/usr/local/include/boost/asio/basic_serial_port.hpp:47:7: note: candidate
      constructor (the implicit copy constructor) not viable: requires 1
      argument, but 2 were provided
class basic_serial_port
      ^
serial.cpp:21:146: error: no matching constructor for initialization of
      'boost::asio::serial_port' (aka 'basic_serial_port<>')
  ...flow_control, std::string parity, float stop_bits):port_(io, port_name){
                                                        ^     ~~~~~~~~~~~~~
/usr/local/include/boost/asio/basic_serial_port.hpp:105:12: note: candidate
      constructor not viable: no known conversion from 'boost::asio::io_service
      *' to 'boost::asio::io_service &' for 1st argument; dereference the
      argument with *
  explicit basic_serial_port(boost::asio::io_service& io_service,
           ^
/usr/local/include/boost/asio/basic_serial_port.hpp:85:12: note: candidate
      constructor not viable: no known conversion from 'boost::asio::io_service
      *' to 'boost::asio::io_service &' for 1st argument; dereference the
      argument with *
  explicit basic_serial_port(boost::asio::io_service& io_service,
           ^
/usr/local/include/boost/asio/basic_serial_port.hpp:126:3: note: candidate
      constructor not viable: no known conversion from 'boost::asio::io_service
      *' to 'boost::asio::io_service &' for 1st argument; dereference the
      argument with *
  basic_serial_port(boost::asio::io_service& io_service,
  ^
/usr/local/include/boost/asio/basic_serial_port.hpp:69:12: note: candidate
      constructor not viable: requires single argument 'io_service', but 2
      arguments were provided
  explicit basic_serial_port(boost::asio::io_service& io_service)
           ^
/usr/local/include/boost/asio/basic_serial_port.hpp:47:7: note: candidate
      constructor (the implicit copy constructor) not viable: requires 1
      argument, but 2 were provided
class basic_serial_port
      ^
2 errors generated.