图文说明 Visual Studio 2013/2015 编译 libpqxx 步骤以及常见编译错误

编译步骤(x86版本)


1.下载并安装postgresql,点击进入下载地址。下载界面如下:




目前最新版本是9.5.1,我下载了 Win x86-32 的最新版本。安装后目录如下:





2.下载并解压缩libpqxx,点击进入下载地址下载界面如下:




目前最新版本是4.0.1,我下载了libpqxx-4.0.1.tar.gz 压缩包。解压后目录如下:





3.编译环境准备。安装或解压路径请以实际的为准,本文路径仅供参考

  • 进入libpqxx的解压路径 D:\Program Files (x86)\libpqxx-4.0.1\win32 ,将该路径下的common-sample文件另存为common文件

  • 打开并删除上述common文件原有全部内容,然后将以下代码复制到common文件中并保存。
       *意:复制后请将第53行 PGSQLSRC 的值修改为你实际安装postgresql的根目录,同时根目录两端的双引号不能省略!
[plain] view plain copy
  1. # Common options for Visual C++ makefiles to build libpqxx and its regression  
  2. # test.  
  3. # THE ORIGINAL OF THIS FILE IS CALLED common-sample; DON'T EDIT THE ORIGINAL or  
  4. # you may lose your changes when you upgrade your libpqxx sources.  
  5. # Instead, copy "common-sample" to "common" and edit just the file "common".  
  6.   
  7. # Caution: on Windows, some environments, such as the "make" that comes with  
  8. # Visual C++, expect you to use backslashes as path separators (for example,  
  9. # "C:\src\libpqxx") whereas others like MinGW want you to use more conventional  
  10. # slashes (as in "C:/src/libpqxx").  
  11.   
  12. # Standard library namespace.  
  13. # There are only two reasons why you might want to change this:  
  14. #  
  15. # 1. Your compiler doesn't define the standard library in the "std" namespace  
  16. # but in the global namespace, in which case PGSTD should be the empty string.  
  17. # However, a compiler with that problem is not likely to be good enough to  
  18. # compile libpqxx.  
  19. #  
  20. # 2. You are using a third-party standard library implementation such as  
  21. # STLport, which lives in another namespace to avoid clashes with the  
  22. # implementation that came with the compiler.  
  23. #  
  24. # In the latter case, PGSTD must be set to the namespace used by your preferred  
  25. # implementation.  In all other cases, just leave this set to "std".  
  26.   
  27. STD="std"  
  28.   
  29. # If you are using a 3rd-party STL like STLport, remember to check your path  
  30. # and be sure the STLport is included before the MSVC includes. VCVARS32.BAT  
  31. # doesn't know anything about the directories as defined in the IDE. I use  
  32. #  
  33. # set INCLUDE=\Utility\Code\STLport-4.5\stlport;%INCLUDE%  
  34. #  
  35. # ...and set STD to "_STL".  
  36.   
  37. # Depending on your STL library min/max need to be defined.  Using STLport  
  38. # there is no need for libpqxx to redefine these for me.  
  39.   
  40. # The next line gives us the directory under which all PostgreSQL include  
  41. # directories, DLLs and LIB files can be found.  
  42. #  
  43. # If you built PostgreSQL from source, this is probably the only line you  
  44. # will need to change.  
  45. #  
  46. # If you installed PostgreSQL using the One Click Installer from EnterpriseDB,  
  47. # comment out the following line and uncomment the line after it.  
  48. #  
  49. # Edit the appropriate line to match your specific installation.  
  50.   
  51. #PGSQLSRC="C:\Sources\postgresql-9.1.1\src"  
  52.   
  53. PGSQLSRC="D:\Program Files (x86)\PostgreSQL\9.5"  
  54.   
  55. # This is the directory where the Postgres header files, e.g. postgres_ext.h,  
  56. # are found.  
  57.   
  58. PGSQLINC=$(PGSQLSRC)\include  
  59.   
  60. # This is the directory where the libpq header files, e.g. libpq-fe.h,  
  61. # are found.  If you used the One Click Installer, comment out the next line  
  62. # and uncomment the one following it:  
  63.   
  64. #LIBPQINC=$(PGSQLSRC)\interfaces\libpq  
  65.   
  66. LIBPQINC=$(PGSQLSRC)\include  
  67.   
  68. # This is the directory where the release build of the libpq DLL  
  69. # and its corresponding LIB file are found, as well as the names of  
  70. # those two files.  
  71. #  
  72. # If you installed PostgreSQL using the One Click Installer from EnterpriseDB,  
  73. # comment out the next three lines and uncomment the following three:  
  74.   
  75. #LIBPQPATH=$(PGSQLSRC)\interfaces\libpq\Release  
  76. #LIBPQDLL=libpq.dll  
  77. #LIBPQLIB=libpqdll.lib  
  78.   
  79. LIBPQPATH=$(PGSQLSRC)\lib  
  80. LIBPQDLL=libpq.dll  
  81. LIBPQLIB=libpq.lib  
  82.   
  83. # This is the directory where the debug build of the libpq DLL  
  84. # and its corresponding LIB file are found, as well as the names of  
  85. # those two files.  
  86. #  
  87. # NOTE: If you don't have access to a debug build of libpq, just  
  88. # provide the same information as above here. The debug builds of  
  89. # libpqxx will reference the release version of libpq, which will  
  90. # only limit your ability to debug libpq sources but otherwise should  
  91. # work just fine.  
  92. #  
  93. # If you installed PostgreSQL using the One Click Installer from EnterpriseDB,  
  94. # comment out the next three lines and uncomment the following three.  
  95.   
  96. #LIBPQDPATH=$(PGSQLSRC)\interfaces\libpq\Debug  
  97. #LIBPQDDLL=libpqd.dll  
  98. #LIBPQDLIB=libpqddll.lib  
  99.   
  100. LIBPQDPATH=$(PGSQLSRC)\lib  
  101. LIBPQDDLL=libpq.dll  
  102. LIBPQDLIB=libpq.lib  
  • 进入libpqxx的解压路径 D:\Program Files (x86)\libpqxx-4.0.1\config\sample-headers\compiler\VisualStudio2010\pqxx ,将该路径下的全部头文件拷贝到目录 D:\Program Files (x86)\libpqxx-4.0.1\include\pqxx中
  • 进入libpqxx的解压路径 D:\Program Files (x86)\libpqxx-4.0.1\config\sample-headers\libpq\9.0\pqxx ,将该路径下的全部头文件拷贝到目录 D:\Program Files (x86)\libpqxx-4.0.1\include\pqxx中
  • 进入libpqxx的解压路径 D:\Program Files (x86)\libpqxx-4.0.1\src ,打开 largeobject.cxx 源文件,在第34行加入头文件#include <algorithm>
              

4.开始编译。安装或解压路径请以实际的为准,本文路径仅供参考
  • 在开始菜单选择Visual Studio 2013的开发人员命令提示工具,并以管理员方式运行,然后直接运行命令:vcvars32.bat
              
  • 将命令提示工具定位到 libpqxx 的解压路径: D:\Program Files (x86)\libpqxx-4.0.1
       
  • 最后在命令提示工具中直接运行命令:nmake /f win32/vc-libpqxx.mak ALL,即可开始编译。
          
  • 编译成功后,所生成的静态库以及动态库,包括DEBUG版本和RELEASE版本,全部都在如下目录中:
             


常见编译错误

  • 提示缺少头文件。
              请查看common文件中的 PGSQLSRC 的值是否已经修改为你实际安装postgresql的根目录,并确保已经按照上述步骤拷贝全部所需头文件。
  • 提示类似错误:error U1077: 'copy' : return code '0x1'。
             请查看common文件中的 PGSQLSRC 的值是否已经修改为你实际安装postgresql的根目录,并确保根目录两端的双引号存在。
  • 提示类似错误: min() 或者 max() 不是std 的标准函数。
             请确保 largeobject.cxx 源文件第34行已经加入头文件 #include <algorithm>。
  • 提示一堆无法解析的外部函数。
              如果编译的是 x86 版本的 libpqxx ,请确保安装的 postgresql 也是 x86 版本的;同样 x64 版本的也是一个道理。


补充:

  • 如果是编译64位的libpqxx, 请使用文本编辑器编译打开win32\vc-libpqxx.mak 文件,将
  • LINK_FLAGS_BASE=kernel32.lib ws2_32.lib advapi32.lib /nologo /dll /machine:I386 shell32.lib secur32.lib wldap32.lib
  • 修改为
  • LINK_FLAGS_BASE=kernel32.lib ws2_32.lib advapi32.lib /nologo /dll /machine:x64 shell32.lib secur32.lib wldap32.lib
  • 如果选择的是Visual Studio 2015, 可使用 VS2015 开发人员命令提示,当然还可以使用X86,x64兼容工具命令提示符,并以管理员方式运行
  • 运行命令:
  • "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64\nmake.exe" /f  win32/vc-libpqxx.mak ALL.


关于 libpgxx 更多的编译选项及编译说明,请参照 libpqxx 解压路径 Win32 目录下的common,INSTALL.TXT 以及 vc-libpgxx.mak 等文件。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章