0

I am attempting to connect to a MySQL database using ODBC driver using nanodbc library a C++ wrapper for ODBC but I am getting error LNK2019: unresolved external symbol

I have added the path to installed library directory in additional library directory where nanodbc.lib is located. I even copied nanodbc.lib in my source directory but still no luck.

I had added nanodbc.lib to Properties->Linker->Input->Additional Directories in case #pragma comment(lib, "nanodbc.lib") was not working but its still not working.

The worst part is the same code works in an existing project (having different connection string).

Tooling : Microsoft Visual Studio Community 2017 Version 15.9.11

Package Manager : vcpkg

OS : Windows 10 Professional 64-bit

Language Standard : C++17

Code :

#include <iostream>
#include <nanodbc/nanodbc.h>

#pragma comment(lib, "nanodbc.lib")

int main()
{
    nanodbc::string dns = "PLC_Interface";
    nanodbc::string user_name = "root";
    nanodbc::string password = "rooot";
    nanodbc::connection conn(dns, user_name, password);
    std::cout << conn.connected() << std::endl;
    //std::cout << "Database Name : " <<conn.database_name() << std::endl;
    //std::cout << "DBMS Name : " <<conn.dbms_name() << std::endl;
    //std::cout << "DBMS Version : " <<conn.dbms_version() << std::endl;
    conn.disconnect();
    std::cout << conn.connected() << std::endl;
}

Error :

error LNK2019: unresolved external symbol "public: __cdecl nanodbc::connection::connection(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 &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,long)" (??0connection@nanodbc@@QEAA@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@00J@Z) referenced in function main
Dark Sorrow
  • 1,681
  • 14
  • 37
  • Have you tried passing `nanodbc.lib` to the linker directly instead of using the `pragma`? I didn't even know you could do that with a `pragma`! – Indiana Kernick Apr 20 '19 at 08:13
  • There should be two steps. First, pass the directory that contains `nanodbc.lib` to the linker. Second, pass the file name `nanodbc.lib` to the linker. I have no experience with VS but it sounds like you're only doing the first step. – Indiana Kernick Apr 20 '19 at 08:22
  • Yes, as mentioned in my post I passed nanodbc.lib in Properties->Linker->Input->Additional Directories. I have also added the path to directory as mentioned. – Dark Sorrow Apr 20 '19 at 08:23
  • You put a **file** in the Additional **Directories** field? – Indiana Kernick Apr 20 '19 at 08:24
  • I added the path of directory where nanoodbc.lib was located. – Dark Sorrow Apr 20 '19 at 08:25
  • That's the first step. The `pragma` is a weird way of doing the second step. There should be a way to pass the file `nanobc.lib` as well as the directory that contains it. – Indiana Kernick Apr 20 '19 at 08:27
  • Trying to rebuild everything from scratch including libraries. I have even removed pragma and I am adding everything in project properties. – Dark Sorrow Apr 20 '19 at 08:29
  • According to [this answer](https://stackoverflow.com/a/23882710/4093378), you need to put `nanodbc.lib` in **Linker->Input->Additional Dependencies**. – Indiana Kernick Apr 20 '19 at 08:31
  • Possible duplicate of [How to add the static libraries to the project in Visual studio](https://stackoverflow.com/questions/23882112/how-to-add-the-static-libraries-to-the-project-in-visual-studio) – Indiana Kernick Apr 20 '19 at 08:33
  • I had referred that article and followed the mentioned steps. – Dark Sorrow Apr 20 '19 at 08:34

1 Answers1

0

Solved the problem. The issue was with library created by vcpkg package manager. When I built the library myself manually, the issue got resolved. No code change was necessary (even working with #pragma).


Wanting to know is their any way to compare the libraries to find out why the issue was caused in the first place?

Dark Sorrow
  • 1,681
  • 14
  • 37