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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3893701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:34:34  来源:igfitidea点击:

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

c++visual-studiosqlitedll

提问by Mike Pateras

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. 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.

我从下载页面下载了 SQLiteDLL-3 ,提取了它的内容(一个 DLL 和一个 .h 文件),并在其上运行 lib.exe 以生成一个 .lib 文件。然后我将包含 .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. I added #include to my main file, and then added sqlite3.dll as an Additional Dependency in Linker >> Input.

然后我从下载页面下载了 SQLiteSource-3,并将 SQLite3.h 文件解压到包含 .Lib 和 .DLL 文件的目录中,并将该目录添加为 C/C++ >> General 下的 Additional Include Directory。我在主文件中添加了 #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. 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.

我尝试了很多方法来纠正它,包括在 x86 和 x64 下构建 .lib 文件,以及在附加依赖项列表中包含 .lib 文件的完整路径。我得到的总是那个错误。似乎至少可以找到 .h 文件,因为如果我弄乱了包含中的名称,我会收到“找不到文件”错误,因此该部分似乎是正确的。

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. Now I'm getting unresolved linker errors:

更新:通过将 .lib 文件添加到附加依赖项列表来修复“无效或损坏的文件”问题,而不是 .dll 文件。现在我收到未解决的链接器错误:

error LNK2019: unresolved external symbol _sqlite3_exec referenced in function _main

错误 LNK2019:未解析的外部符号 _sqlite3_exec 在函数 _main 中引用

error LNK2019: unresolved external symbol _sqlite3_open referenced in function _main

错误 LNK2019:未解析的外部符号 _sqlite3_open 在函数 _main 中引用

fatal error LNK1120: 2 unresolved externals

致命错误 LNK1120:2 个未解析的外部

回答by Pierre

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.

在解决方案资源管理器窗口中,右键单击您的项目,添加“sqlite3.lib”文件。

(You'd think the SQLite gang could include a .lib in the download?)

(您认为 SQLite 团伙可以在下载中包含 .lib 吗?)

回答by Jay

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 manglingissues.

包装声明?您可能遇到名称修改问题。

回答by Pierre

Update: SQLite 3.28 doesn't seem to ship with a .DEF file. To create the DEF and LIB, do:

更新:SQLite 3.28 似乎没有附带 .DEF 文件。要创建 DEF 和 LIB,请执行以下操作:

:: Dump SQLite.DLL exports

set DUMPBIN="C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.16.27023\bin\Hostx86\x86\dumpbin.exe"
set LIBX="C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.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%