Linker Errors When Attempting to Run Example Program for MySQL Connector/C++ 8.0.27 (Windows)

216 Views Asked by At

Introduction

I have been trying to successfully build and run a test of the MySQL Connector/C++ 8.0.27 for several days, and I feel like I am losing my mind because nothing I do seems to work. I have gone through the documentation for every step and still I come up against the same issues.

Problem

I am running this test code, completely unmodified. I only ever intend to use the JDBC interface with the connector. These are the errors I receive after it compiles:

1>main.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) void __cdecl check(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (__imp_?check@@YAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>main.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) void __cdecl check(class std::map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > > const &)" (__imp_?check@@YAXAEBV?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@@std@@@2@@std@@@Z)
1>main.obj : error LNK2001: unresolved external symbol __imp__get_driver_instance_by_name

I get this error whether I use the pre-built 8.0.27 Connector/C++ libraries, or the libraries I built myself from the 8.0.27 Windows source code.

This is my current cmake configure command ("<path>" is just the rest of the full path):

cmake .. -DBUILD_STATIC=ON -DWITH_JDBC=ON -DCMAKE_INSTALL_PREFIX="<path>/install/mysql_connector" -DWITH_SSL="<path>/install/OpenSSL" -DWITH_BOOST="<path>/boost_1_75_0" -DWITH_MYSQL="<path>/install/MySQL"

My understanding is that the linker errors imply that the linker cannot find objects within mysqlcppconn-static.lib, though the "dllimport" seems to imply this program is looking for dynamic libraries, even though I link only to mysqlcppconn-static.lib NOT mysqlcppconn.lib


System Info

  • OS: Windows 10 Pro, 21H1
  • IDE: Microsoft Visual Studio Community 2019 Preview, Version 16.11.1 Preview 1.0
  • Platform Toolset: Visual Studio 2019 (v142)
  • Windows SDK Version: 10.0
1

There are 1 best solutions below

0
M H On

Sigh. Well, I solved it. I figured writing out my problem would lead to an answer, and I was right! And of course its embarrassingly simple and I have no idea how I didn't see it. So for the sake of potential future Googlers, here's the answer:

I missed a single, very simple step in the MySQL connector documentation right at the bottom before the "notes" section:

Prior to Connector/C++ 8.0.16, for applications that use the legacy JDBC API, define the CPPCONN_PUBLIC_FUNC macro as an empty string. To ensure this, define the macro as CPPCONN_PUBLIC_FUNC=, not as CPPCONN_PUBLIC_FUNC.

I added that preprocessor definition and now everything works perfectly. I must have read that page 15 times and somehow my brain just totally skimmed over that step.