在 Windows 上编译 psqlODBC

本页介绍如何在 Windows 上构建 PostgreSQL ODBC 驱动程序。有三种构建方法:使用 nmake makefile 从命令行构建,使用 MSBuild 文件从 Powershell 构建,以及从 Visual Studio IDE 构建。

构建需要以下第三方软件

使用 Powershell 和 MSBuild 构建

  Using Powershell and MSBuild is recommended. 
  In fact the binaries of official release are built using this mothod.

  Currently 4 Windows Powershell scripts are provided for developers.

  winbuild/BuildAll.ps1          - build all dlls for psqlodbc drivers using
				   MSBuild.
  winbuild/editConfiguration.ps1 - a GUI tool to set Build environment
  winbuild/regress.ps1           - build regression test programs and run
  installer/buildInstallers.ps1  - build installers(.msi and setup.exe)

  Use Powershell console or Command Prompt to invoke scripts:

  For example, to build the driver:

  C:\psqlodbc\winbuild\> (Powershell) ./BuildAll.ps1 <options>
 
    or you can use the same functionality from Command Prompt using Windows
    helper batch at the parent folder (..\). See ..\readme_winbuild.txt.
 
  C:\psqlodbc\> (Commnd Prompt) .\BuildAll.bat <options>

  1. Please start a powershell console and set the ExecutionPolicy of
     Powershell to RemoteSigned or Unrestricted.

     You can get the ExecutionPolicy by typing

	Get-ExecutionPolicy

     When the ExectionPolicy is "Restricted" or "AllSigned" then type e.g.

	Set-ExecutionPolicy RemoteSigned

     To see details about ExecutionPolicy, type

	Get-Help about_Execution_Policies

  2. You have to install one of the following.

   . Visual Studio 2015 non-Express edtion or Express 2015 for Windows
	Desktop
   . Visual Studio 2013 non-Express edtion or Express 2013 for Windows
	Desktop
   . Visual Studio 2012 non-Express edtion or Express 2012 for Windows
	Desktop
   . Full Microsoft Visual C++ 2010
   . Windows SDK 7.1

     You have to include x64 development tools (bin, lib, include) as
     well as x86 ones for the installation.

     You can install multiple versions of VC++ and use them.
     You can easily switch by specifying VCVersion parameter.

  3. Setup Build environment

     Please type

	.\editConfiguration(.ps1)

     and edit the setting of your environment especially the folders
     you placed libpq related include/lib/bin files.

		

  4. Build

     Please type

	.\BuildAll(.ps1)

     to invoke build operations.

     If you installed both VC10 and VC12 and you'd like to compile
     under VC10 environment, type

        .\BuildAll(.ps1) -V(CVersion) 10.0

     or set the value 10.0 to vcversion using ./editConfiguration.

     To see details about the use of BuildAll, type

	Get-Help .\BuildAll(.ps1) [-Detailed | -Examples | -Full]

  5. Outputs of Build

     The build can produce output in up to four directories for each of
     the debug and release configurations:

     - x64_Unicode_Release     the Unicode driver, 64-bit
     - x86_ANSI_Release        the ANSI driver, 64-bit
     - x86_Unicode_Release     the ANSI driver, 32-bit
     - x86_ANSI_Release        the Unicode driver, 32-bit

     For debug builds (-Configuration Debug) the directories are named with
     Debug instead of Release but otherwise the same.

     pgxalib.dll is only built for the multibyte/unicode version, as it is
     the same for both unicode and ansi drivers.
  
  6. How to use drivers.

     You can't use psqlodbc drivers at once you build the drivers.
     Usually you have to install drivers using installers made by
     installer/buildInstallers.ps1. buildInstallers.ps1 bundles
     libpq and related libraries like ssleay32, libeay32 from the
     PostgreSQL bin directory and MSVC runtime libraries compiled with.

     However, it is painful for developers to build binaries, build
     installers and install each time the source files are changed. 
     It is recommended to use a special installation-less driver
     (postgres_devw) registered by regress.ps1

  7. Regression test in place

     After BuildAll(.ps1), please type

	.\regress(.ps1)

     You have to neither install nor copy binaries.
     By default, build 32-bit binaries from test sources and run the tests.
     If you'd like to test 64-bit version, please type

	.\regress(.ps1) -p(latform) x64

     Please note the outputs(obj, exe etc) generated by build operations and
     results of tests are placed in the directory winbuild/test_x86(test_x64
     in case of 64-bit).

  8. Installer
     
     To build the .msi installer file:

     C:\psqlodbc\installer\> (Powershell) ./buildInstallers.ps1 <options>
        or
     C:\psqlodbc\> (Command Prompt) .\buildInstallers.bat <options>

     By default, buildInstallers.ps1 builds bootstrapper program
     psqlodbc-setup.exe together.

     See ../installer/readme.txt in the source directory for details.

Troubleshooting:

     Some documentation on dealing with Windows SDK installation issues
     can be found on the related pg_build_win page:
     https://github.com/2ndQuadrant/pg_build_win#troubleshooting

使用 nmake.exe 构建

使用 NMAKE.exe 为当前活动目标构建驱动程序

C:\psqlodbc\> nmake /f win64.mak <options>

尽管名称如此,win64.mak 用于构建 32 位和 64 位二进制文件。使用 Microsoft Windows SDK 命令提示符,您可以使用 setenv /x86setenv /x64 命令在 32 位和 64 位目标架构之间切换。如果使用 Visual Studio 命令提示符,您可以从“开始”菜单启动 x86 或 x64 版本,或者使用适当的选项运行 vcvarsall.bat 来切换架构。

构建 .msi 安装程序文件

C:\psqlodbc\> nmake /f win64.mak installer

生成的安装程序文件位于 installer/x64 或 installer/x86 目录中。

在一个命令中构建 32 位和 64 位版本

C:\psqlodbc\> nmake /f win64.mak world

以下构建选项可以使用,可以在命令行中使用,也可以通过创建一个名为“windows-local.mak”的文件来使用。

变量
CFG Release(默认)或 Debug
PG_INC $(PROGRAMFILES)\PostgreSQL\9.3\include
PG_LIB $(PROGRAMFILES)\PostgreSQL\9.3\lib
SSL_INC C:\OpenSSL-Win32\include
SSL_LIB C:\OpenSSL-Win32\lib
ANSI_VERSION no(如果设置为“yes”,则将输出 dll 名称更改为 psqlodbc30a.dll)
MSDTC yes

  1. UNICODE 为默认值(psqlodbc35w.dll),除非选择了 ANSI_VERSION(psqlodbc30a.dll)
  2. libpq.dll 是必需的。
  3. 如果 MSDTC == Yes,则会创建“pgxalib.dll”。
  4. 如果启用了 MSDTC,ANSI_VERSION 还会创建“pgenlista.dll”,或 UNICODE 创建“pgenlist.dll”。

IDE 方法

源代码树中包含一个 Microsoft Visual Studio 项目文件(和工作区)。

psqlodbc.dsp 可与 Microsoft Visual C++ 6.0 一起使用。

psqlodbc.projpsqlodbc.sln 可与 Microsoft Visual C++ 2005 版(包括 Express 版,可以从 http://msdn.microsoft.com/vstudio/express/visualc/usingpsdk/default.aspx 免费下载)一起使用。

构建前的准备

  1. PostgreSQL 的 Windows 发行版会将所需的头文件和库安装到 C:\Program Files\PostgreSQL\9.2
  2. OpenSSL 应该安装到 C:\OpenSSL 目录下(OpenSSL 二进制文件可以从 http://www.slproweb.com/products/Win32OpenSSL.html 下载)。

如果您想更改安装路径,则需要编辑项目设置。

注意

默认的构建设置将创建以下驱动程序

  1. UNICODE 支持。
  2. libpq 是必需的。
  3. 驱动程序文件名是 "psqlodbc35w.dll"。