使用Boost.asio创建包装类时,库链接器错误

时间:2016-03-21 16:48:25

标签: 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
serial.o: serial.cpp serial.h
    g++ -c serial.cpp
clean:
    rm *.o main

我收到此错误。如果您愿意,我可以通过makefile启用详细信息。

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

导致此错误并编译的代码体如下

#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;
}

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

和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();
}

非常感谢你的帮助。再一次,抱歉我无法提供更多相关细节。

编辑:新错误

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.

我不确定为什么它不能将成员构造函数识别为有效

0 个答案:

没有答案