简体   繁体   English

如何在我的C ++项目中包含SQLite DLL?

[英]How do I include a SQLite DLL in my C++ project?

I'm trying to add SQLite to my project via a DLL. 我正在尝试通过DLL将SQLite添加到我的项目中。

I downloaded SQLiteDLL-3 from the download page , extracted its contents (a DLL and a .h file), and ran lib.exe on it to produce a .lib file. 我从下载页面下载了SQLiteDLL-3,提取了其内容(DLL和.h文件),并在其上运行了lib.exe以生成.lib文件。 I then set the directory containing the .lib and the .dll files to be an Additional Library Directory, in the project settings, under Linker >> General. 然后,我在项目设置中的链接器>>常规下将包含.lib和.dll文件的目录设置为附加库目录。

Then I downloaded SQLiteSource-3 from the download page, and extracted the SQLite3.h file to the directory with the .Lib and .DLL files, and added that directory as an Additional Include Directory under C/C++ >> General. 然后我从下载页面下载了SQLiteSource-3,并将SQLite3.h文件解压缩到包含.Lib和.DLL文件的目录,并将该目录添加为C / C ++ >> General下的附加包含目录。 I added #include to my main file, and then added sqlite3.dll as an Additional Dependency in Linker >> Input. 我在主文件中添加了#include,然后在链接器>>输入中添加了sqlite3.dll作为附加依赖项。

Basically I followed this , but when I run it, I get an error saying: 基本上我跟着这个 ,但是当我运行它时,我得到一个错误说:

fatal error LNK1107: invalid or corrupt file: cannot read at 0x2B8

I tried a number of things to correct it, including building the .lib file under both x86 and x64, and including the full path to the .lib file in the Additional Dependencies list. 我尝试了很多方法来纠正它,包括在x86和x64下构建.lib文件,并在Additional Dependencies列表中包含.lib文件的完整路径。 It's always that error that I get. 我得到的总是那个错误。 It seems to at least be finding the .h file okay, because if I mess with the name in the include, I get a "cannot find the file" error, so that part appears to be correct. 它似乎至少可以找到.h文件,因为如果我在include中弄乱了名字,我会得到一个“找不到文件”的错误,因此该部分似乎是正确的。

Can someone see what I might be doing incorrectly and how to correct the issue? 有人可以看到我可能做错了什么以及如何纠正这个问题?

Update: Fixed the "invalid or corrupt file" issue by adding the .lib file to the Additional Dependies list, as opposed to the .dll file. 更新:通过将.lib文件添加到Additional Dependies列表而不是.dll文件来修复“无效或损坏的文件”问题。 Now I'm getting unresolved linker errors: 现在我得到了未解决的链接器错误:

error LNK2019: unresolved external symbol _sqlite3_exec referenced in function _main 错误LNK2019:函数_main中引用了未解析的外部符号_sqlite3_exec

error LNK2019: unresolved external symbol _sqlite3_open referenced in function _main 错误LNK2019:函数_main中引用了未解析的外部符号_sqlite3_open

fatal error LNK1120: 2 unresolved externals 致命错误LNK1120:2个未解决的外部因素

Here's what worked for me: 这对我有用:

In Visual Studio 2010, click on "Tools, VS Command Prompt" 在Visual Studio 2010中,单击“工具,VS命令提示符”

In the command line window, enter, for example: 在命令行窗口中,输入,例如:

lib /DEF:"C:\SQLite\sqlite3.def" /OUT:"C:\SQLite\sqlite3.lib"

In the Solution Explorer window, right-click on your project, add the "sqlite3.lib" file. 在Solution Explorer窗口中,右键单击您的项目,添加“sqlite3.lib”文件。

(You'd think the SQLite gang could include a .lib in the download?) (您认为SQLite团队可能在下载中包含.lib吗?)

If I remember right sqlite is written in C. Does the sqlite3.h file have 如果我记得正确的sqlite是用C编写的.sqlite3.h文件是否有

extern "C" {}

wrapping the declarations? 包装声明? You might be having name mangling issues. 您可能有名称错误问题。

Update: SQLite 3.28 doesn't seem to ship with a .DEF file. 更新:SQLite 3.28似乎没有附带.DEF文件。 To create the DEF and LIB, do: 要创建DEF和LIB,请执行以下操作:

:: Dump SQLite.DLL exports

set DUMPBIN="C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx86\x86\dumpbin.exe"
set LIBX="C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx86\x86\lib.exe"
set SQL="C:\SQLite\sqlite3.dll"
set DEF="C:\SQLite\sqlite3.def"
set LIB="C:\SQLite\sqlite3.lib"

:: [1] run dumpbin
%DUMPBIN% /EXPORTS /NOLOGO /OUT:%DEF% %SQL%

:: [2] manually edit .DEF file to look proper
:: [3] run LIB
%LIBX% /DEF:%DEF% /OUT:%LIB%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM