圖文說明 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 等文件。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章