简体   繁体   English

io_context的未定义符号:最新boost库的链接错误

[英]Undefined symbols for io_context: linking error for the latest boost library

My code used to copmile well previously, until boost library got updated with changes in asio . 我的代码以前曾经很好地完成过copmile,直到boost库更新了asio更改 In my code, I define a variable: boost::shared_ptr<Face> face(boost::make_shared<ThreadsafeFace>(io_service)); 在我的代码中,我定义了一个变量: boost::shared_ptr<Face> face(boost::make_shared<ThreadsafeFace>(io_service)); which, as can be seen, takes io_service for the constructor. 可以看出,它需要io_service作为构造函数。 Face and ThreadsafeFace are a library classes, my app links to. FaceThreadsafeFace是我的应用程序链接到的类。 The problem occurs at linking stage of my binary, where I get undefined symbols error: 问题发生在我的二进制文件的链接阶段,在那里我得到undefined symbols错误:

Undefined symbols for architecture x86_64:
  "ndn::ThreadsafeFace::ThreadsafeFace(boost::asio::io_context&)", referenced from:
      boost::detail::sp_if_not_array<ndn::ThreadsafeFace>::type boost::make_shared<ndn::ThreadsafeFace, boost::asio::io_context&>(boost::asio::io_context&&&) in ndnrtc_client-main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

As can be seen, linker can't find a constructor for ThreadsafeFace that takes boost::asio::io_context& argument. 可以看出,链接器找不到使用boost::asio::io_context&参数的ThreadsafeFace构造函数。 And it won't -- because library does not provide one. 而且不会 -因为图书馆没有提供图书馆。 The only one library does provide -- is with io_service argument. 唯一提供的库-带有io_service参数。 Now, I don't quite understand, where does this constructor definition come from, as neither my code, nor library's code have this definition. 现在,我不太明白这个构造函数定义的来源,因为我的代码和库代码都没有这个定义。

This makes me think that with new boost (I'm linking against 1.67 using homebrew, macOS), io_service gets replaced by io_context automatically (during preprocessing?), thus leading to the problem. 这使我认为有了新的提升(我正在使用自制软件,macOS与1.67链接), io_service自动(在预处理过程中)被io_context替换,从而导致了问题。

I tried providing -DBOOST_ASIO_ENABLE_OLD_SERVICES when compiling my code, but it didn't help either. 我在编译代码时尝试提供-DBOOST_ASIO_ENABLE_OLD_SERVICES ,但它也无济于事。

Shall I downgrade boost version until library gets updated? 我应该降级Boost版本,直到更新库吗?

UPDATE I've ran clang for preprocessing ( clang++ -E ... ) and found this in the output: 更新我已经运行了clang进行预处理( clang++ -E ... ),并在输出中找到了它:

# 21 "/usr/local/include/boost/asio/io_service.hpp" 2 3

namespace boost {
namespace asio {



typedef io_context io_service;


}
}

Which confirms that all io_service variables will in fact be io_context and guarantee headaches. 证实了所有的io_service变量实际上都是io_context并确保令人头疼。

"Which confirms that all io_service variables will in fact be io_context and guarantee headaches" “确认所有io_service变量实际上都是io_context并保证令人头痛”

In fact that guarantees no headaches. 实际上,这不会造成任何麻烦。 Typedefs are aliases : they're exactly the same. Typedef是别名 :它们完全相同。 So io_service becomes just another way to refer to the same type, even if the spelling happens to be different in some spots. 因此,即使某些地方的拼写恰好不同, io_service还是引用同一类型的另一种方法。 This is actually what you need. 这实际上是您所需要的。

Reading the message: 阅读消息:

Undefined symbols for architecture x86_64: "ndn::ThreadsafeFace::ThreadsafeFace(boost::asio::io_context&)", referenced from: boost::detail::sp_if_not_array::type boost::make_shared(boost::asio::io_context&&&) in ndnrtc_client-main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) 架构x86_64的未定义符号:“ ndn :: ThreadsafeFace :: ThreadsafeFace(boost :: asio :: io_context&)”,引用自:boost :: detail :: sp_if_not_array :: type boost :: make_shared(boost :: asio :: io_context &&&&& )在ndnrtc_client-main.o中ld:找不到体系结构x86_64铛的符号:错误:链接器命令失败,退出代码为1(使用-v查看调用)

This tells you that ndn::ThreadsafeFace does provide the required constructor, because it's referenced from your code. 这告诉您ndn::ThreadsafeFace 确实提供了所需的构造函数,因为它是从您的代码中引用的。 If it weren't provided, it would have been a compile error, not a link error. 如果未提供,那将是编译错误,而不是链接错误。

So your problem is different. 所以你的问题就不同了。 You either lack a linker input, or the library object you link against was compiled /differently/ in such a way that it doesn't provide the definition of the constructor that is declared when you include the header that declares ThreadsafeFace . 您要么缺少链接器输入,要么/链接到/不同地编译了要链接的库对象,使得它不提供包含声明ThreadsafeFace的标头时所声明的构造函数的定义。

Usually, this happens if namespaces change, or when you (ab)used compiler defines to change the meaning of the code (did you perhaps mess around with something like #define io_context io_service ? Because that is a recipe for headaches). 通常,如果命名空间发生更改,或者当您(滥用)的编译器定义更改代码的含义时,就会发生这种情况(您可能是在#define io_context io_service类的东西了吗?因为那令人头痛的秘诀)。

Note linker errors can even result when you use different compiler versions/flags when compiling your code versus when compiling the library. 注意在编译代码和编译库时使用不同的编译器版本/标志时,甚至可能导致链接器错误。

For far more troubleshooting tips see: What is an undefined reference/unresolved external symbol error and how do I fix it? 有关更多的故障排除提示,请参见: 什么是未定义的引用/未解决的外部符号错误,如何解决?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM