简体   繁体   English

编译SQLite for Windows(64位)

[英]Compiling SQLite for Windows (64-bit)

I have MinGW and I wish to compile the SQLite amalgamation source into a 64-bit dll. 我有MinGW,我希望将SQLite合并源编译成64位dll。 I'm fairly new to this sort of compilation and my efforts so far have resulted in failure. 我对这种编辑很新,到目前为止我的努力导致了失败。 (I first started using the autoconf amalgamation and used the configure & make tool on Linux. But apparently that will never work for Windows binaries.) (我首先开始使用autoconf合并并在Linux上使用configure和make工具。但显然这对于​​Windows二进制文件永远不会起作用。)

Anyway, I've been told I need the following preprocessor defines: 无论如何,我被告知我需要以下预处理器定义:

Here are the compiler pre-processor defines I use for a 64-bit release build: 以下是我用于64位版本构建的编译器预处理器定义:

  • WIN64 NDEBUG WIN64 NDEBUG
  • _WINDOWS _视窗
  • _USRDLL _USRDLL
  • NO_TCL NO_TCL
  • _CRT_SECURE_NO_DEPRECATE _CRT_SECURE_NO_DEPRECATE
  • THREADSAFE=1 THREADSAFE = 1
  • TEMP_STORE=1 TEMP_STORE = 1
  • SQLITE_MAX_EXPR_DEPTH=0 SQLITE_MAX_EXPR_DEPTH = 0

Here are the compiler pre-processor defines I use for a 32-bit release build: 以下是我用于32位版本构建的编译器预处理器定义:

  • WIN32 WIN32
  • NDEBUG NDEBUG
  • _WINDOWS _视窗
  • _USRDLL _USRDLL
  • NO_TCL NO_TCL
  • _CRT_SECURE_NO_DEPRECATE _CRT_SECURE_NO_DEPRECATE
  • THREADSAFE=1 THREADSAFE = 1
  • TEMP_STORE=1 TEMP_STORE = 1
  • SQLITE_MAX_EXPR_DEPTH=0 SQLITE_MAX_EXPR_DEPTH = 0

I had no idea where to put these in. I eventually took an educated guess, made a new file (for neatness) called sqlite3w64.h and pasted in the following: 我不知道把它放在哪里。我最终做了一个有根据的猜测,做了一个名为sqlite3w64.h的新文件(为了整洁)并粘贴在下面:

#define WIN64 NDEBUG
#define _WINDOWS
#define _USRDLL
#define NO_TCL
#define _CRT_SECURE_NO_DEPRECATE
#define THREADSAFE 1
#define TEMP_STORE 1
#define SQLITE_MAX_EXPR_DEPTH 0

I then compiled the source with the following command: 然后我使用以下命令编译源代码:

gcc sqlitew64.h sqlite3.h sqlite3ext.h shell.c sqlite3.c -o sqlite_x64.dll

What resulted was a 733KB DLL file. 结果是一个733KB的DLL文件。 Nice! 太好了! Did it actually work? 它真的有效吗? Did it nuts - I got a BadImageFormatException. 它搞砸了 - 我得到了一个BadImageFormatException。 I also then tried doing an x86 compilation using the same method. 然后我也尝试使用相同的方法进行x86编译。 Once again, I got a 733KB DLL file (that's odd?) and once again, I got a BadImageFormatException. 再一次,我得到了一个733KB的DLL文件(这很奇怪?)再一次,我得到了一个BadImageFormatException。

Help. 救命。

Update 更新

Used the following command instead: 改为使用以下命令:

gcc -shared -DWIN64 -DNDEBUG -D_WINDOWS -D_USRDLL -DNO_TCL -D_CRT_SECURE_NO_DEPRECATE -DTHREADSAFE=1 -DTEMP_STORE=1 -DSQLITE_MAX_EXPR_DEPTH=0 -I. shell.c sqlite3.c -o sqlite_x64.dll -Wl,--out-implib,sqlite3.a

Resulted in a 740KB DLL file which still gives a BadImageFormatException. 导致740KB的DLL文件仍然产生BadImageFormatException。

Final Update 最后更新

Turns out my MinGW build was 32-bit only. 原来我的MinGW版本只有32位。 Getting a 64-bit version then allowed me to make SQLite for 64-bit. 获得64位版本然后允许我为64位制作SQLite。 Adding the flag -m64 sets the compiler into 64-bit mode. 添加标志-m64会将编译器设置为64位模式。

64-bit: 64位:

gcc -shared -DWIN64 -DNDEBUG -D_WINDOWS -D_USRDLL -DNO_TCL -D_CRT_SECURE_NO_DEPRECATE -DTHREADSAFE=1 -DTEMP_STORE=1 -DSQLITE_MAX_EXPR_DEPTH=0 -m64 -I. shell.c sqlite3.c -o sqlite3_x64.dll -Wl,--out-implib,sqlite3_x64.a

32-bit: 32位:

gcc -shared -DWIN32 -D_WINDOWS -D_USRDLL -DNO_TCL -D_CRT_SECURE_NO_DEPRECATE -DTHREADSAFE=1 -DTEMP_STORE=1 -DSQLITE_MAX_EXPR_DEPTH=0 -m32 -I. shell.c sqlite3.c -o sqlite3_x86.dll -Wl,--out-implib,sqlite3_x86.a

MinGW-64 Precompiled: http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Automated%20Builds/mingw-w64-bin_i686-mingw_20111220.zip/download?use_mirror=ignum MinGW-64预编译: http//sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Automated%20Builds/mingw-w64-bin_i686-mingw_20111220.zip/download? use_mirror= ignum

Installation Instructions: http://code.google.com/p/tonatiuh/wiki/InstallingMinGWForWindows64 安装说明: http//code.google.com/p/tonatiuh/wiki/InstallingMinGWForWindows64

You are compiling to an EXE. 你正在编译EXE。 Calling it a DLL won't magically make it a DLL. 将它称为DLL不会神奇地使它成为DLL。

You need to pass special linker options to gcc to make it create DLLs. 您需要将特殊的链接器选项传递给gcc以使其创建DLL。 Quoted from Mingw site: (Sort of, I replaced g++ with gcc) 引自Mingw网站:( 排序,我用gcc替换g ++)

gcc -c -DBUILDING_EXAMPLE_DLL example_dll.cpp
gcc -shared -o example_dll.dll example_dll.o -Wl,--out-implib,libexample_dll.a

The page also explains that functions you want your DLL to export, must be declared with __declspec(dllexport) . 该页面还解释了您希望DLL导出的函数必须使用__declspec(dllexport)声明。 (Further down, there is an example on how to export all global functions to the DLL, like usually happens in Unix.) (再往下,有一个关于如何将所有全局函数导出到DLL的示例,就像通常在Unix中一样。)

The -Wl argument to gcc is what tells gcc to pass on the further arguments --out-implib,libexample_dll.a to the linker. 轮候册参数GCC就是告诉GCC传递关于进一步论证--out-IMPLIB,libexample_dll.a给链接器。

I would also make 100% sure that the built DLL is actually a 64 bit DLL and not a 32 bit DLL. 我还要100%确定构建的DLL实际上是64位DLL而不是32位DLL。 Do you have any way to check that? 你有办法检查吗? On Linux you can run the "file" command. 在Linux上,您可以运行“file”命令。

You can also try adding the -m64 option to the gcc commandline, that should force gcc to target the amd64 target. 您还可以尝试将-m64选项添加到gcc命令行,这应该强制 gcc定位amd64目标。

If that doesn't work, you may have the wrong compiler altogether. 如果不起作用,您可能完全使用了错误的编译器。 Make sure you have the x86_64/amd64 version of the Mingw toolchain . 确保您拥有Mingw工具链的x86_64 / amd64版本 Installation is as simple as finding the right ZIP, unpacking it, and setting the path . 安装就像找到正确的ZIP,解压缩和设置路径一样简单。

If all of that fails, or if you just want to verify against a supposedly correctly compiled setup, try precompiled 64-bit binaries here or from here . 如果所有这些都失败了,或者您只想验证所谓的正确编译设置,请在此处从此处尝试预编译的64位二进制文​​件

What would work in your case is this single link-and-compile command: 在您的情况下,这个单链接和编译命令将起作用:

g++ -shared
-DWIN64
-DNDEBUG
-D_WINDOWS
-D_USRDLL
-DNO_TCL
-D_CRT_SECURE_NO_DEPRECATE
-DTHREADSAFE=1
-DTEMP_STORE=1
-DSQLITE_MAX_EXPR_DEPTH=0
-I.
shell.c sqlite3.c
-o sqlite_x64.dll
-Wl,--out-implib,libsqllite_x64.dll.a

The compile and link stage will be performed at once. 编译和链接阶段将立即执行。 The defined can be added on the commandline. 可以在命令行上添加已定义的内容。 The headers need not be compiled, but you need to pass the current directory as a header search directory, and specify the names of the dll and import file. 不需要编译头文件,但是您需要将当前目录作为头文件搜索目录传递,并指定dll和导入文件的名称。

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

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