Thread: Compiling C function with VC++ for Windows version

Compiling C function with VC++ for Windows version

From
Edwin Quijada
Date:
Hi!
I am trying to create a C function for windows in Postgres compiling with VC++ 2008 Express Edition. I did this function for Linux without problem using gcc and tried to do the same in Windows but in Windows was imposible. The function compile fine but when I tried to run from Postgres it down the server in windows but in Linux the same function run perfect.

The function just try to get a string value and pass to postgres. Reading somebody told me that I need to compile the function using VC++ not gcc in Windows so I downloaded VC++ 2008 Express but I dont know what directives compilation I need as gcc in windows and Linux.

Somebody here has compiled any function for working in Windows for Postgres using VC++?

Any help will be so appreciated.
 
*-------------------------------------------------------*
*-Edwin Quijada
*-Developer DataBase
*-JQ Microsistemas
*-Soporte PostgreSQL
*-www.jqmicrosistemas.com
*-809-849-8087
*-------------------------------------------------------*

Re: Compiling C function with VC++ for Windows version

From
Craig Ringer
Date:
On 12/01/12 11:06, Edwin Quijada wrote:
Hi!
I am trying to create a C function for windows in Postgres compiling with VC++ 2008 Express Edition. I did this function for Linux without problem using gcc and tried to do the same in Windows but in Windows was imposible. The function compile fine but when I tried to run from Postgres it down the server in windows but in Linux the same function run perfect.

You haven't provided enough information for anyone to help you.

How are you compiling your function? Using pgxs, or some other way?

What's the full source code of the function?

Where does the server crash when it crashes? You might need to get a backtrace of the server crash to help you figure out why your function crashes it. See this documentation on getting a backtrace of a Pg backend under windows:

http://wiki.postgresql.org/wiki/Getting_a_stack_trace_of_a_running_PostgreSQL_backend_on_Windows


The function just try to get a string value and pass to postgres. Reading somebody told me that I need to compile the function using VC++ not gcc in Windows so I downloaded VC++ 2008 Express but I dont know what directives compilation I need as gcc in windows and Linux.

If you are using a standard binary distribution of PostgreSQL then yes, you should use Visual C++, preferably the same version of Visual C++ that was used for that version of PostgreSQL. Specify your version of Pg.

--
Craig Ringer

Re: Compiling C function with VC++ for Windows version

From
Edwin Quijada
Date:




Date: Thu, 12 Jan 2012 13:45:06 +0800
From: ringerc@ringerc.id.au
To: listas_quijada@hotmail.com
CC: pgsql-general@postgresql.org
Subject: Re: [GENERAL] Compiling C function with VC++ for Windows version

On 12/01/12 11:06, Edwin Quijada wrote:
Hi!
I am trying to create a C function for windows in Postgres compiling with VC++ 2008 Express Edition. I did this function for Linux without problem using gcc and tried to do the same in Windows but in Windows was imposible. The function compile fine but when I tried to run from Postgres it down the server in windows but in Linux the same function run perfect.

You haven't provided enough information for anyone to help you.

How are you compiling your function? Using pgxs, or some other way?

What's the full source code of the function?

Where does the server crash when it crashes? You might need to get a backtrace of the server crash to help you figure out why your function crashes it. See this documentation on getting a backtrace of a Pg backend under windows:

http://wiki.postgresql.org/wiki/Getting_a_stack_trace_of_a_running_PostgreSQL_backend_on_Windows


The function just try to get a string value and pass to postgres. Reading somebody told me that I need to compile the function using VC++ not gcc in Windows so I downloaded VC++ 2008 Express but I dont know what directives compilation I need as gcc in windows and Linux.

If you are using a standard binary distribution of PostgreSQL then yes, you should use Visual C++, preferably the same version of Visual C++ that was used for that version of PostgreSQL. Specify your version of Pg.
---------------

Ok.
This is the way that I compile. 
Linux this way works fine and I use the modules compiled. of course, Linux I compile from source and I use gcc.

C:\mingw\bin\gcc -g -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -fno-strict-aliasing -fwrapv  -I "C:/Program Files/PostgreSQL/8.3/include/" -I "C:/Program Files/PostgreSQL/8.3/include/server" -I "C:/Program Files/PostgreSQL/8.3/include/server/port/win32" -DEXEC_BACKEND "-I C:/Program Files/PostgreSQL/8.3/include/server/port/win32" -c pg2.c -o pg2.o
C:\mingw\bin\gcc  -shared -o pg_server_function.dll pg2.o -L "C:\Program Files\PostgreSQL\8.3\lib" -lpostgres

It is using gcc in windows version

This is the module that I want to compile

<code>
 #include "postgres.h"
#include "fmgr.h"
#include <stdio.h>
#include <string.h>

#include "libstd.h"

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif


PG_FUNCTION_INFO_V1(pg_server_id);

Datum pg_server_id(PG_FUNCTION_ARGS)
{
   char mac[30];
   text *new_t = (text *) palloc(30);
   int32 arg = PG_GETARG_INT32(0);
//
  getmacwindows(mac);
//
  SET_VARSIZE(new_t, strlen(mac) + VARHDRSZ);
  memcpy(VARDATA(new_t),(text *)mac,strlen(mac));
  
  PG_RETURN_TEXT_P(new_t);
}

 

PG_FUNCTION_INFO_V1(pg_serverid);
Datum
pg_serverid(PG_FUNCTION_ARGS)
{  float8 mac;
  int32   arg = PG_GETARG_INT32(0);
  //
  arg++;
   mac = getmacwindows2num();
  //
  PG_RETURN_FLOAT8(mac);
}               



PG_FUNCTION_INFO_V1(one);
Datum one(PG_FUNCTION_ARGS)
{   
   int32   arg = PG_GETARG_INT32(0);
int32 x= ++arg; //mac2num();
//getmacwindows(mac);
   PG_RETURN_INT32(x);
}
</code>

The function is simple just get the mac address of the PC. Now, I am trying to use VC++ 2008 Express to compile this module but I dont know how to pass all these parameters using VC++

In this example is 8.3 but I tried with 9.0 too

This compile fine the problem at running timme this crash down my server..:(
Any help will be so apreciated.

--
Craig Ringer

Re: Compiling C function with VC++ for Windows version

From
Craig Ringer
Date:
On 13/01/2012 1:55 AM, Edwin Quijada wrote:
>
> Ok.
> This is the way that I compile.
> Linux this way works fine and I use the modules compiled. of course,
> Linux I compile from source and I use gcc.

Yep... but Windows isn't Linux.

In particular, the distributions of PostgreSQL for Windows are compiled
using Microsoft Visual C++, not gcc. It is possible to use gcc (mingw)
to compile shared libraries that are compatible with programs compiled
with MSVC++ but I've generally found it simpler to just use MSVC++ to
compile libraries on Windows.

If you prefer to use mingw there's still usually no problem with that,
you'll just have to figure out why your function is crashing the server.
Things to try:

- Delete code until it stops crashing; or
- Get a backtrace of the server to see where it crashes and why.

See how I'm repeating "get a backtrace of the crash"? Check out the link
I sent in my last post for info on how to do it using windbg or vc++
express.

--
Craig Ringer

Re: Compiling C function with VC++ for Windows version

From
Edwin Quijada
Date:
 
> Date: Fri, 13 Jan 2012 14:44:24 +0800
> From: ringerc@ringerc.id.au
> To: listas_quijada@hotmail.com; pgsql-general@postgresql.org
> Subject: Re: [GENERAL] Compiling C function with VC++ for Windows version
>
> On 13/01/2012 1:55 AM, Edwin Quijada wrote:
> >
> > Ok.
> > This is the way that I compile.
> > Linux this way works fine and I use the modules compiled. of course,
> > Linux I compile from source and I use gcc.
>
> Yep... but Windows isn't Linux.
>
> In particular, the distributions of PostgreSQL for Windows are compiled
> using Microsoft Visual C++, not gcc. It is possible to use gcc (mingw)
> to compile shared libraries that are compatible with programs compiled
> with MSVC++ but I've generally found it simpler to just use MSVC++ to
> compile libraries on Windows.
>
> If you prefer to use mingw there's still usually no problem with that,
> you'll just have to figure out why your function is crashing the server.
> Things to try:
>
> - Delete code until it stops crashing; or
> - Get a backtrace of the server to see where it crashes and why.
>
> See how I'm repeating "get a backtrace of the crash"? Check out the link
> I sent in my last post for info on how to do it using windbg or vc++
> express.
>
I am reading the backtrace to Postgres. What is the way to compile using VC++?
I mean what parameters I need to compile?

> --
> Craig Ringer
>
> --
> Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general