来自不需要的库中的未定义引用错误
Undefined reference errors from an unneeded library
我在ld中遇到了一些"未定义的引用"错误,并且不知道是什么导致了它们。
我的生成文件使用如下命令构建多个可执行文件:
g++ -ogui_program1 -Lpath/to/MyLibs gui_program1.o -lMyUI -lMyBusinessLogic
-lMyUtil -lboost_regex
g++ -ogui_program2 -Lpath/to/MyLibs gui_program2.o -lMyUI -lMyBusinessLogic
-lMyUtil -lboost_regex
g++ -ocli_program1 -Lpath/to/MyLibs cli_program1.o -lMyUI -lMyBusinessLogic
-lMyUtil -lboost_regex
g++ -ocli_program2 -Lpath/to/MyLibs cli_program2.o -lMyUI -lMyBusinessLogic
-lMyUtil -lboost_regex
等等。 (实际上,还有比这更多的库,但这是一般的想法。
MyUI
、MyBusinessLogic
和 MyUtil
都是我已经构建的动态库。 为了使编写生成文件更简单,GUI 和命令行程序使用相同的库列表,即使命令行程序不需要 libMyUI.so
。
当我尝试链接 Boost.Regex 符号时,一个也是唯一一个命令行程序给出了许多关于未定义引用的错误,即使我-lboost_regex
与每个二进制文件链接:
libMyBusinessLogic.so: undefined reference to `boost::re_detail::perl_matcher >, boost::regex_traits > >::construct_init(boost::basic_regex > > const&, boost::regex_constants::_match_flags)'
libMyBusinessLogic.so: undefined reference to `boost::cpp_regex_traits::toi(char const*&, char const*, int) const'
libMyBusinessLogic.so: undefined reference to `boost::re_detail::perl_matcher, std::allocator > >, std::allocator, std::allocator > > > >, boost::regex_traits > >::match()'
libMyBusinessLogic.so: undefined reference to `boost::re_detail::perl_matcher, std::allocator > >, std::allocator, std::allocator > > > >, boost::regex_traits > >::construct_init(boost::basic_regex > > const&, boost::regex_constants::_match_flags)'
libMyBusinessLogic.so: undefined reference to `boost::re_detail::perl_matcher, std::allocator > >, std::allocator, std::allocator > > > >, boost::regex_traits > >::find()'
libMyBusinessLogic.so: undefined reference to `boost::basic_regex > >::do_assign(char const*, char const*, unsigned int)'
libMyBusinessLogic.so: undefined reference to `boost::re_detail::perl_matcher >, boost::regex_traits > >::match()'
链接所有其他程序工作正常。 如果我从一个命令行程序中删除-lMyUI
,那么即使MyUI
没有显示在错误列表中的任何地方,它也能正常工作。
命令末尾添加 -lboost_regex
时,ld 找不到 Boost.Regex 符号? 为什么删除看似无关的库可以修复它? 为什么其他程序链接没有任何问题?
我至少已经找到了大部分答案。 由于我的 makefile 规则有些草率,libMyUI.so
与 boost_regex
相关联,但libMyBusinessLogic.so
不是。 我猜,结果,链接MyUI
导致boost_regex
过早地被拉入,在链接器知道MyBusinessLogic
需要从它那里得到的所有符号之前。
只要我保持一致 - 要么My*.so
都与boost_regex
链接,要么他们都没有 - 一切都有效。 我不确定这些解决方案中的哪一个最受欢迎,但至少我有一个解决方法。
相关文章:
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 对 Scalar ::Scalar() 的未定义引用
- 对复制 CTOR 和 CTOR 的未定义引用
- 对显式实例化的模板函数的未定义引用
- TensorRT (C++ API) 对"createNvOnnxParser_INTERNAL"的未定义引用
- 2个模板化类的非模板友元函数未定义引用错误
- 编译 libfluid 样本控制器时对"event_base_del_virtual"的未定义引用
- 获取对function_name的未定义引用
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- 对结构方法的未定义引用
- 使用内联函数 c++ 的未定义引用
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 对 DLOPEN 的未定义引用
- QT C++中对全局变量的未定义引用
- 快速数学导致对"__pow_finite"的未定义引用
- 对 boost::system::d etail::system_category_instance 的未定义引用,从
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用