0
votes

I cannot figure out how to get sqlite3.dll (or any dll for that matter) to work with my C++ project in Visual Studio.

The error message I get is LNK2001 unresolved external symbol sqlite3_open

Here's what I did so far:

  • I put the .dll in myProjectFoler/Resources/sqlite3.dll
  • I generated sqlite3.lib using Visual Studio Developer Command Prompt using a command lib /def:sqlite3.def
  • I referenced the directory with the lib in Project -> Preferences -> Linker -> General -> Additional Library directories, and then referenced the .lib in Project -> Preferences -> Linker -> Input-> Additional dependencies
  • I placed sqlite3.h in my project and #included it

(pretty much followed the instructions seen here)

Thank you for help

2

2 Answers

3
votes

The issue is that by default the header file assumes that sqlite is linked statically, as opposed to dynamic linking to a dll.

This part of sqlite3.h is responsible for that:

#ifndef SQLITE_API
# define SQLITE_API
#endif

If you set a per-project define in project properties:

SQLITE_API=__declspec(dllimport)

this should resolve your link error. Alternatively, you can put

#define SQLITE_API __declspec(dllimport)

right before where you #include sqlite3.h.

0
votes

It's difficult to tell exactly why the problem occurred. There are lots of reasons which could cause LNK2001 error. MSDN contains good check list.

You could try to use /VERBOSE option to determine which files the linker references. Put this option in Project -> Preferences -> Linker -> Command Line -> Additional Options. Output should contains similar strings:

Searching e:\SQLite\sqlite-dll-win32-x86-3150100\sqlite3.lib:
      Found _sqlite3_open
         Referenced in ConsoleApplication2.obj
         Loaded sqlite3.lib(sqlite3.dll)

Pay attention to the VS runtime libraries, there should be no mixup between Debug and Release libraries.