链接器多重定义错误:为什么<thread>似乎定义了我的函数?

时间:2017-04-20 11:49:41

标签: c++ linker multiple-definition-error

构建项目时,我收到以下错误消息。不幸的是,我无法理解它。

CMakeFiles/xxx.dir/xxx/send_dummy_events.cc.o: In function `std::chrono::duration_values<long>::zero()':
/usr/include/c++/6/thread:311: multiple definition of `POLONAISE::xxx::dispatch_event(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
CMakeFiles/xxx.dir/xxx/main.cc.o:/home/manuel/projects/projectname/build_x86_64/package/xxx/send_dummy_events.cc:10: first defined here

CMakeFiles/xxx.dir/xxx/send_dummy_events.cc.o: In function `std::chrono::duration<long, std::ratio<1l, 1000000000l> >::count() const':
/usr/include/c++/6/thread:311: multiple definition of `POLONAISE::xxx::send_dummy_events()'
CMakeFiles/xxx.dir/xxx/main.cc.o:/home/manuel/projects/projectname/build_x86_64/package/xxx/send_dummy_events.cc:16: first defined here

对我而言,/usr/include/c++/6/thread似乎会重新定义我的函数名dispatch_event()send_dummy-event()

send_dummy_events.cc的来源是:

#include <chrono>
#include <thread>
#include "POLONAISE/logging.h"
#include "POLONAISE/EventQueue.h"


namespace POLONAISE {
    namespace xxx {
        void dispatch_event(std::string serialized_uirequest __attribute__((unused)))
        {

        }           


        void send_dummy_events()
        {
            std::chrono::seconds delay(3);
            while (1) {
                    std::this_thread::sleep_for(delay);
            }
        }
    }
}

usr/include/c++/6/thread的来源是:(不要被行号混淆,我忘了如何在Vim中关闭它们。)

     /// sleep_for
  2     template<typename _Rep, typename _Period>
  1       inline void
311       sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
  1       {
  2         if (__rtime <= __rtime.zero())
  3           return;
  4         auto __s = chrono::duration_cast<chrono::seconds>(__rtime);
  5         auto __ns = chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
  6 #ifdef _GLIBCXX_USE_NANOSLEEP
  7         __gthread_time_t __ts =
  8           {
  9             static_cast<std::time_t>(__s.count()),
 10             static_cast<long>(__ns.count())
 11           };
 12         while (::nanosleep(&__ts, &__ts) == -1 && errno == EINTR)
 13           { }
 14 #else
 15         __sleep_for(__s, __ns);
 16 #endif
 17       }

1 个答案:

答案 0 :(得分:0)

我找到了原因,现在回答我自己的问题:

根本原因在于main.cc#include d send_dummy_events.cc。由于这只是一个临时测试存根,几周前我没有看到任何问题。但是现在我为应用程序的一个新部分复制了我现有的代码,并且在更新CMakeLists.txt时,我确保在那里注意到每个源文件。所以我添加了send_dummy_events.cc

Voilà,因为链接器现在有两次相同的符号,他正确地抱怨它。

现在我听说有关于此的规则:从不包含源文件。嗯,知道我知道原因。

我仍然感到困惑,为什么错误信息会像/usr/include/c++/6/thread中那样发生第二次定义,但我想这只是另一个“令人困惑的错误消息™”GCC而闻名。