简体   繁体   中英

Compiling Windows PostgreSQL 9.5 64bit C-language functions

I want to create native C extensions to PostgreSQL 9.5 64bit on Windows.

I would love to build them with MinGW-w64 if that's possible, to get my build chain as clean as possible. But I am using the EnterpriseDB build of PostgreSQL, and MinGW build crashes it.

It would also be okay if there is another free compiler that I can use in this commercial project.

I know how to get this to work with Visual Studio 2003 Express but that doesn't seem to be a solution because of License issues.

Addition to the Main Answer

In the article linked below , you can read that it is possible to write C modules using Mingw or Cygwin, and some guidelines regarding the same. However I highly discourage doing so, mostly because of the reasons listed below, and also because that page mentions that those Windows configurations have special needs and receive lighter testing than most; expect a greater incidence of build problems

Here is the full excerpt on Unix-like Platform:

Unix-like Platforms

PGXS originated on Unix-like systems, and it is easy to use there. Unpack the extension module archive and run these commands in the resulting directory:

make PG_CONFIG=path_to_postgresql_installation/bin/pg_config make PG_CONFIG=path_to_postgresql_installation/bin/pg_config install

You may omit the PG_CONFIG overrides if running type pg_config in your shell locates the correct PostgreSQL installation. Subject to the ownership of the existing PostgreSQL installation directories, the second command will often require root privileges. These instructions also apply when building for Windows using the MinGW or Cygwin compilers. However, those Windows configurations have special needs and receive lighter testing than most; expect a greater incidence of build problems.

A common mistake is to specify the PG_CONFIG=... on the command line before the 'make', which does not work as the value is then overridden by the inner workings of makefiles.


It's important to know that different compilers are not compatible with each other. Each of them have different runtime libraries. So it is very risky to compile extensions with a different compiler other than the one used to build the software you are using.

Postgresql's Windows build uses Visual Studio, same with EnterpriseDB (as far as I know). You will need to use the same compiler to build your extensions.

(For more information please refer: Building and Installing PostgreSQL Extension Modules and Postgres Enterprise Manager Installation Guide - EnterpriseDB (PDF) )

This should explain why your extensions compiled with Mingw-w64 crash.

Luckily, there are two solutions you could choose:

  1. Use Microsoft Visual Studio Community . It is completely free for individual developers, however there are restrictions for businesses. It should compile proper modules for your Postgresql build.

  2. Rebuild the compleat Postgresql binary with Mingw-w64 or any other compiler of your choice (LLVM/Clang maybe), and then compile extensions with the same.

Doing either of these should help you. And this applies to all platforms, languages, and other softwares too. If you want to build extensions for a software, you need the same compiler, on the same platform used to build the software being used.

So, if you want to build extensions for Postgresql on Linux, you need the same compiler (probably GCC) to build extensions.

Happy coding =)

@Swith give you link to docs how to build PostgreSQL Extension Modules with Visual Studio and as you can read:

These instructions also apply when building for Windows using the MinGW or Cygwin compilers. However, those Windows configurations have special needs and receive lighter testing than most; expect a greater incidence of build problems.

A common mistake is to specify the PG_CONFIG=... on the command line before the 'make', which does not work as the value is then overridden by the inner workings of makefiles.

Did you check this ?

Also you can read other docs Building PostgreSQL With MinGW - maybe this help you more.



It would also be okay if there is another free compiler that I can use in this commercial project.

Did you check C compliers list on wiki - in particular, you can check Cygwin compiler - it's mentioned in docs above and it's free.



I know how to get this to work with Visual Studio 2003 Express but that doesn't seem to be a solution because of License issues.

What kind of issues do you see ?


Maybe check Visual Studio 2015 Community Edition (not Express) like @Simon Mourier suggest.

There is quite different licencing between Express and Community editions - I'm not sure about details, but as far I know Community Edition is more flexible for using in commercial projects:

For organizations:
An unlimited number of users within an organization can use Visual Studio Community for the following scenarios: in a classroom learning environment, for academic research, or for contributing to open source projects.

For all other usage scenarios:
In non-enterprise organizations, up to five users can use Visual Studio Community. In enterprise organizations (meaning those with >250 PCs or >$1 Million US Dollars in annual revenue), no use is permitted beyond the open source, academic research, and classroom learning environment scenarios described above.

For more information, see the Visual Studio Community license terms .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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