5

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.

6
  • 2
    Visual Studio 2015 community is "free for individual developers, open source development, academic research, education and small professional teams": microsoft.com/en-us/download/details.aspx?id=48146 Commented May 30, 2016 at 6:15
  • Why do you not use gcc ? Commented May 30, 2016 at 10:07
  • 1
    @Boiethios: MinGW is gcc, just packed as a distribution for Windows. Commented May 31, 2016 at 11:50
  • 1
    Why the downvote? Isn't having spend 500 rep already enought loss for this specific question? Commented May 31, 2016 at 11:55
  • 1
    @ I accepted switches answer, althought it didn't answer my question to my satisfaction, but the points shouldn't be lost :). Commented Jun 4, 2016 at 20:20

2 Answers 2

5
+500

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 =)

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your answer. But my question specifically asks exactly for how I can avoid Visual Studio! I already tried to link with msvcr120.dll, like PostgreSQL also does, and my MinGW library loads, but everytime I use a function from msvcr120.dll the server crashes. I was looking for switches or macros that allow me to use MinGW.
@Daniel follow my second step. Compile the source code of postgresql with Mingw-w64. The link I provided to Postgresql wiki has some info regarding the same
@Daniel I have extended the answer to satisfy your needs =)
I didn't want to rebuild PostgreSQL with mingw, because in some performance comparisons I saw that those binaries are a bit slower than the EnterpriseDB builds. But I'd love to have my extensions build with the free toolchain, because we want to keep our build environment like our software free.
@Daniel As I said, it is a bad idea to use a different compiler. However, consider using other compilers like LLVM. As far as I know, GCCs binaries are more efficient than Visual Studios's, as shown in some benchmarks. But I'll check again.
2

@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.

3 Comments

I don't know exactly what the license for Visual Studio Expess is because I didn't find it on the net, and I assume is something like the community edition license.
Here is licence for express edition: visualstudio.com/en-us/mt171551.aspx
Main difference between editions is in functionality... Community editions is like as 'all-in-one', but express editions are split by web / mobile / desktop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.