【问题标题】:Include issue: 'multiple definition', 'first defined here'包括问题:'多重定义','首先定义在这里'
【发布时间】:2017-08-14 04:16:02
【问题描述】:

我有三个文件:

main.cpp
MyClass.cpp
MyClass.hpp

我有一个库头文件"testLib.hpp",我想将它包含在MyClass.hpp 中,这样我就可以让testLib 的对象之一成为类属性。

我在MyClass.cppmain.cpp 中包含MyClass.hpp。尝试编译项目时,出现以下错误

MyClass.cpp   multiple definition of 'testLib::testLib::function1()
obj/Release/main.o:main.cpp first defined here
MyClass.cpp   multiple definition of 'testLib::testLib::function2()
obj/Release/main.o:main.cpp first defined here

等等。

main.cppMyClass.cpp 都包括 MyClass.hpp(其中包括 testLib.hpp)。从错误来看,MyClass.cpp 似乎正在尝试在库函数已被 main.cpp 包含之后包含它们。但是,我在MyClass.hpp 中包含了警卫,所以我不明白它是如何尝试包含MyClass.hpp 两次的。

代码如下:

MyClass.hpp

#ifndef THIS_HEADER_H
#define THIS_HEADER_H

#include <stdint.h>
#include <iostream>
#include "testLib/testLib.hpp"

class MyClass
{

public:
    void test();
    int foo;

private:
    uint32_t bar;
    //I want to include an object from the library as part of this class
    //TestLib::Device device;
};

#endif

MyClass.cpp

#include <stdio.h>
#include "MyClass.hpp"

void MyClass::test()
{

}

main.cpp

#include <iostream>
#include "MyClass.hpp"

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}

任何帮助将不胜感激!

编辑 我试图隐藏实际的文件名以使问题更笼统和清晰,但似乎问题可能是由我没有写的“testLib.hpp”引起的。该文件实际上是以下“sweep.hpp”文件。对于此文件中的每个公共函数,我都收到了“此处的多个定义/首次定义”错误:

sweep.hpp

#ifndef SWEEP_DC649F4E94D3_HPP
#define SWEEP_DC649F4E94D3_HPP

/*
 * C++ Wrapper around the low-level primitives.
 * Automatically handles resource management.
 *
 * sweep::sweep  - device to interact with
 * sweep::scan   - a full scan returned by the device
 * sweep::sample - a single sample in a full scan
 *
 * On error sweep::device_error gets thrown.
 */

#include <cstdint>
#include <memory>
#include <stdexcept>
#include <vector>

#include <sweep/sweep.h>

namespace sweep {

// Error reporting

struct device_error final : std::runtime_error {
  using base = std::runtime_error;
  using base::base;
};

// Interface

struct sample {
  const std::int32_t angle;
  const std::int32_t distance;
  const std::int32_t signal_strength;
};

struct scan {
  std::vector<sample> samples;
};

class sweep {
public:
  sweep(const char* port);
  sweep(const char* port, std::int32_t bitrate);
  void start_scanning();
  void stop_scanning();
  bool get_motor_ready();
  std::int32_t get_motor_speed();
  void set_motor_speed(std::int32_t speed);
  std::int32_t get_sample_rate();
  void set_sample_rate(std::int32_t speed);
  scan get_scan();
  void reset();

private:
  std::unique_ptr<::sweep_device, decltype(&::sweep_device_destruct)> device;
};

// Implementation

namespace detail {
struct error_to_exception {
  operator ::sweep_error_s*() { return &error; }

  ~error_to_exception() noexcept(false) {
    if (error) {
      device_error e{::sweep_error_message(error)};
      ::sweep_error_destruct(error);
      throw e;
    }
  }

  ::sweep_error_s error = nullptr;
};
}

sweep::sweep(const char* port)
    : device{::sweep_device_construct_simple(port, detail::error_to_exception{}), &::sweep_device_destruct} {}

sweep::sweep(const char* port, std::int32_t bitrate)
    : device{::sweep_device_construct(port, bitrate, detail::error_to_exception{}), &::sweep_device_destruct} {}

void sweep::start_scanning() { ::sweep_device_start_scanning(device.get(), detail::error_to_exception{}); }

void sweep::stop_scanning() { ::sweep_device_stop_scanning(device.get(), detail::error_to_exception{}); }

bool sweep::get_motor_ready() { return ::sweep_device_get_motor_ready(device.get(), detail::error_to_exception{}); }

std::int32_t sweep::get_motor_speed() { return ::sweep_device_get_motor_speed(device.get(), detail::error_to_exception{}); }

void sweep::set_motor_speed(std::int32_t speed) {
  ::sweep_device_set_motor_speed(device.get(), speed, detail::error_to_exception{});
}

std::int32_t sweep::get_sample_rate() { return ::sweep_device_get_sample_rate(device.get(), detail::error_to_exception{}); }

void sweep::set_sample_rate(std::int32_t rate) {
  ::sweep_device_set_sample_rate(device.get(), rate, detail::error_to_exception{});
}

scan sweep::get_scan() {
  using scan_owner = std::unique_ptr<::sweep_scan, decltype(&::sweep_scan_destruct)>;

  scan_owner releasing_scan{::sweep_device_get_scan(device.get(), detail::error_to_exception{}), &::sweep_scan_destruct};

  auto num_samples = ::sweep_scan_get_number_of_samples(releasing_scan.get());

  scan result;
  result.samples.reserve(num_samples);

  for (std::int32_t n = 0; n < num_samples; ++n) {
    auto angle = ::sweep_scan_get_angle(releasing_scan.get(), n);
    auto distance = ::sweep_scan_get_distance(releasing_scan.get(), n);
    auto signal = ::sweep_scan_get_signal_strength(releasing_scan.get(), n);

    result.samples.push_back(sample{angle, distance, signal});
  }

  return result;
}

void sweep::reset() { ::sweep_device_reset(device.get(), detail::error_to_exception{}); }

} // ns

#endif

【问题讨论】:

  • 您尝试过#pragma 一次吗
  • 包含守卫防止同一个标头被两次包含到同一个源中。它们不会阻止将相同的标头包含到多个源中(它们也不应该;这正是标头的预期用途)。主要的罪过是testLib.hpp 标头定义了几个函数。标头应该只声明函数(应该在某些源文件中定义);或者用inline关键字定义它们。
  • @IgorTandetnik 感谢您的回复。我在原始帖子中包含了我命名为“testLib.hpp”的实际库文件(sweep.hpp)。我没有写sweep.hpp。看起来他们确实声明了一些我遇到错误的函数。所以你是说错误完全来自sweep.hpp?
  • sweep.hpp 中的所有函数体都是错误的,不应该存在。您可以通过将它们全部更改为 inline 来解决问题
  • 请不要修改错误,发布编译器报告的实际消息。

标签: c++


【解决方案1】:

您的问题的简化版本:

buggy.hpp

int function() { return 0; }

main.cpp

 #include "buggy.hpp"
 int main() { return 0; }

其他.cpp

 #include "buggy.hpp"

问题在于buggy.hpp定义 function,而不仅仅是声明。一旦扩展了标头包含,这意味着在 main.cppother.cpp 中都声明了 function - 这是不允许的。

修复方法是将function 声明为inline,这允许在多个翻译单元中声明该函数。

inline int function() { return 0; }

事实上,允许多个定义是inline对C++标准唯一含义。编译器可能会将其视为函数体可以内联扩展的提示。好人不会;他们更擅长做出程序员那样的决定)。

【讨论】:

  • 非常感谢!你拯救了我的一天。 :)
  • 这很奇怪。我可以在我的 .hpp 文件中定义类,包括它们的方法,而不会触发这个问题。但不是免费功能。为什么不一致?
  • @interstar 1. 您可以(并且实际上需要)在它们使用的每个翻译单元中定义类型(包括类)。 2.如果你把struct foo { int bar(); }; int foo::bar() { return 0; }放在你的头文件中,你会遇到完全相同的问题。不同的是,如果你在一个类定义中定义了一个成员函数in,它会被隐式声明为inline
  • 这也拯救了我的一天,很好的答案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
  • 2021-01-13
  • 2021-02-22
  • 1970-01-01
相关资源
最近更新 更多