Visual Studio 链接器未加载库

Posted

技术标签:

【中文标题】Visual Studio 链接器未加载库【英文标题】:Visual Studio Linker not loading library 【发布时间】:2018-11-05 19:02:55 【问题描述】:

我刚刚通过下载here 并将include 的内容移动到/usr/include 并将lib64 的内容移动到/usr/lib 来安装mysql 连接器/C++。我还在 Visual Studio 的命令行链接器选项中添加了链接器选项-lmysqlcppconn8

代码:

/* Other includes... */
#include<mysqlx/xdevapi.h>

int main(const int argc, const char* argv[]) 
    mysqlx::Session sess("localhost", 33060, "root", "MY_PW");
    return EXIT_SUCCESS;

当我现在在我的项目中包含 mysqlx/xdevapi.h 并按照 here 的描述初始化 Session 时,我收到以下错误:

1>Validating architecture
1>Starting remote build
1>Compiling sources:
1>Linking objects
1>C:\Users\Paul\source\repos\LSTGA\LSTGA\obj\x64\Debug\Main.o : error : In function `mysqlx::common::Value::Value(std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&)':
1>/usr/include/mysqlx/common/value.h(119): error : undefined reference to `vtable for mysqlx::common::Value'
1>/usr/include/mysqlx/common/value.h(119): error : undefined reference to `vtable for mysqlx::common::Value'
1>C:\Users\Paul\source\repos\LSTGA\LSTGA\obj\x64\Debug\Main.o : error : In function `mysqlx::common::Value::Value(unsigned long)':
1>/usr/include/mysqlx/common/value.h(129): error : undefined reference to `vtable for mysqlx::common::Value'
1>/usr/include/mysqlx/common/value.h(129): error : undefined reference to `vtable for mysqlx::common::Value'
1>C:\Users\Paul\source\repos\LSTGA\LSTGA\obj\x64\Debug\Main.o : error : In function `mysqlx::common::Value::~Value()':
1>/usr/include/mysqlx/common/value.h(56): error : undefined reference to `vtable for mysqlx::common::Value'
1>C:\Users\Paul\source\repos\LSTGA\LSTGA\obj\x64\Debug\Main.o : error : /usr/include/mysqlx/common/value.h:56: more undefined references to `vtable for mysqlx::common::Value' follow
1>C:\Users\Paul\source\repos\LSTGA\LSTGA\obj\x64\Debug\Main.o : error : In function `mysqlx::string::string(char const*)':
1>/usr/include/mysqlx/devapi/common.h(100): error : undefined reference to `mysqlx::string::Impl::from_utf8(mysqlx::string&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
1>C:\Users\Paul\source\repos\LSTGA\LSTGA\obj\x64\Debug\Main.o : error : In function `mysqlx::string::string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
1>/usr/include/mysqlx/devapi/common.h(105): error : undefined reference to `mysqlx::string::Impl::from_utf8(mysqlx::string&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
1>C:\Users\Paul\source\repos\LSTGA\LSTGA\obj\x64\Debug\Main.o : error : In function `mysqlx::string::operator std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >[abi:cxx11]() const':
1>/usr/include/mysqlx/devapi/common.h(115): error : undefined reference to `mysqlx::string::Impl::to_utf8[abi:cxx11](mysqlx::string const&)'
1>C:\Users\Paul\source\repos\LSTGA\LSTGA\obj\x64\Debug\Main.o : error : In function `mysqlx::DbDoc::DbDoc()':
1>/usr/include/mysqlx/devapi/document.h(153): error : undefined reference to `vtable for mysqlx::DbDoc'
1>C:\Users\Paul\source\repos\LSTGA\LSTGA\obj\x64\Debug\Main.o : error : In function `mysqlx::DbDoc::~DbDoc()':
1>/usr/include/mysqlx/devapi/document.h(127): error : undefined reference to `vtable for mysqlx::DbDoc'
1>C:\Users\Paul\source\repos\LSTGA\LSTGA\obj\x64\Debug\Main.o : error : In function `mysqlx::Value::print(std::ostream&) const':
1>/usr/include/mysqlx/devapi/document.h(507): error : undefined reference to `mysqlx::common::Value::print(std::ostream&) const'
1>C:\Users\Paul\source\repos\LSTGA\LSTGA\obj\x64\Debug\Main.o : error : In function `mysqlx::common::Value::Value(mysqlx::common::Value&&)':
1>/usr/include/mysqlx/common/value.h(56): error : undefined reference to `vtable for mysqlx::common::Value'
1>/usr/include/mysqlx/common/value.h(56): error : undefined reference to `vtable for mysqlx::common::Value'
1>C:\Users\Paul\source\repos\LSTGA\LSTGA\obj\x64\Debug\Main.o : error : In function `mysqlx::common::Value::Value(mysqlx::common::Value const&)':
1>/usr/include/mysqlx/common/value.h(56): error : undefined reference to `vtable for mysqlx::common::Value'
1>/usr/include/mysqlx/common/value.h(56): error : undefined reference to `vtable for mysqlx::common::Value'
1>C:\Users\Paul\source\repos\LSTGA\LSTGA\obj\x64\Debug\Main.o : error : In function `mysqlx::internal::Session_detail::~Session_detail()':
1>/usr/include/mysqlx/devapi/detail/session.h(369): error : undefined reference to `mysqlx::internal::Session_detail::close()'
1>C:\Users\Paul\source\repos\LSTGA\LSTGA\obj\x64\Debug\Main.o : error : In function `mysqlx::Session::Session(mysqlx::SessionSettings)':
1>/usr/include/mysqlx/xdevapi.h(1101): error : undefined reference to `mysqlx::internal::Session_detail::Session_detail(mysqlx::common::Settings_impl&)'
1>C:\Users\Paul\source\repos\LSTGA\LSTGA\obj\x64\Debug\Main.o : error : In function `void mysqlx::internal::Settings_detail<mysqlx::internal::Settings_traits>::set<true, mysqlx::SessionOption, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, mysqlx::SessionOption, unsigned int&, mysqlx::SessionOption, mysqlx::string const&>(mysqlx::SessionOption, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, mysqlx::SessionOption&&, unsigned int&, mysqlx::SessionOption&&, mysqlx::string const&)':
1>/usr/include/mysqlx/devapi/detail/settings.h(67): error : undefined reference to `mysqlx::internal::Settings_detail<mysqlx::internal::Settings_traits>::do_set(std::__cxx11::list<std::pair<int, mysqlx::common::Value>, std::allocator<std::pair<int, mysqlx::common::Value> > >&&)'
1>C:\Users\Paul\source\repos\LSTGA\LSTGA\obj\x64\Debug\Main.o : error : In function `void mysqlx::internal::Settings_detail<mysqlx::internal::Settings_traits>::set<true, mysqlx::SessionOption, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(mysqlx::SessionOption, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&)':
1>/usr/include/mysqlx/devapi/detail/settings.h(67): error : undefined reference to `mysqlx::internal::Settings_detail<mysqlx::internal::Settings_traits>::do_set(std::__cxx11::list<std::pair<int, mysqlx::common::Value>, std::allocator<std::pair<int, mysqlx::common::Value> > >&&)'
1>C:\Users\Paul\source\repos\LSTGA\LSTGA\obj\x64\Debug\Main.o : error : In function `void mysqlx::internal::Settings_detail<mysqlx::internal::Settings_traits>::set<true, mysqlx::SessionOption, mysqlx::string const&>(mysqlx::SessionOption, mysqlx::string const&)':
1>/usr/include/mysqlx/devapi/detail/settings.h(67): error : undefined reference to `mysqlx::internal::Settings_detail<mysqlx::internal::Settings_traits>::do_set(std::__cxx11::list<std::pair<int, mysqlx::common::Value>, std::allocator<std::pair<int, mysqlx::common::Value> > >&&)'
1>C:\Users\Paul\source\repos\LSTGA\LSTGA\obj\x64\Debug\Main.o : error : (.rodata._ZTCN6mysqlx5ValueE0_NS_6common5ValueE[_ZTVN6mysqlx5ValueE]+0x18): undefined reference to `typeinfo for mysqlx::common::Value'
1>C:\Users\Paul\source\repos\LSTGA\LSTGA\obj\x64\Debug\Main.o : error : (.rodata._ZTCN6mysqlx5ValueE0_NS_6common5ValueE[_ZTVN6mysqlx5ValueE]+0x20): undefined reference to `mysqlx::common::Value::print(std::ostream&) const'
1>C:\Users\Paul\source\repos\LSTGA\LSTGA\obj\x64\Debug\Main.o : error : (.rodata._ZTIN6mysqlx5ValueE[_ZTIN6mysqlx5ValueE]+0x28): undefined reference to `typeinfo for mysqlx::common::Value'
1>collect2 : error : ld returned 1 exit status
1>Done building project "LSTGA.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

其他信息:

远程操作系统:Ubuntu 16.04 LTS x64 MySQL 服务器:8.0.13(启用 XPlugin) gcc / g++: 5.4.0(与 MySQL 连接器的 BUILDINFO.txt 相同)

提前致谢。 -介意

【问题讨论】:

是否有任何选项可以显示 Visual Studio 尝试运行的链接器命令? @Hitobat 我找不到,它只在链接器 -> 命令行下显示-o"C:\Users\Paul\source\repos\LSTGA\LSTGA\bin\x64\Debug\LSTGA.out" "3600000" -Wl,-z,relro -Wl,-z,noexecstack -Wl,--no-undefined "g++" -Wl,-z,now,但稍后添加-lmysqlcppconn8 【参考方案1】:

我现在可以通过将mysqlcppconn8 放入Library Dependencies 而不是将-lmysqlcppconn8 放入其他选项(都在项目>属性>链接器下)让它工作)。 Visual Studio 生成 -l"mysqlcppconn8" 并且运行良好。

我不知道为什么以前的方法不起作用,但可能是因为库名称中的数字需要引号。无论哪种方式,使用 Library Dependencies 选项都是一个更清洁的解决方案。

希望这会有所帮助。 -介意

【讨论】:

以上是关于Visual Studio 链接器未加载库的主要内容,如果未能解决你的问题,请参考以下文章

Visual Studio - 项目缺少库/链接器问题?

Visual Studio Community 2017 cl 链接器不会链接 GTK3 库?

visual studio 2008 c++ 中怎样添加动态链接库?

XAML 设计器未显示在 Visual Studio 版本 16.10.2 中

尝试将ImageMagick用作Visual Studio编译器的静态库时出现链接器错误

Visual Studio 2015 探查器未显示我的代码中的任何内容