dbTalk Databases Forums  

Problem with ecpg include file

comp.databases.postgresql comp.databases.postgresql


Discuss Problem with ecpg include file in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Al Balmer
 
Posts: n/a

Default Problem with ecpg include file - 10-05-2006 , 11:56 AM






I'm porting a number of programs from Informix embedded SQL to
postgreSQL, and ran into the following problem. The optim_accounts.ec
program included the following:

<begin code>
EXEC SQL BEGIN DECLARE SECTION;
EXEC SQL INCLUDE acct_plan_web.h;
EXEC SQL END DECLARE SECTION;
<end code>

The acct_plan_web.h file is:

<begin code>
#ifndef H_ACCT_PLAN_WEB
#define H_ACCT_PLAN_WEB

typedef struct acct_plan_web_st {
long acct;
long plan;
long min_deposit;
long max_deposit;
} *user_t;

#endif
<end code>

The results are:

<begin output>
$ make optim_accounts.c
ecpg optim_accounts.ec
../acct_plan_web.h:3: ERROR: syntax error at or near "#define
H_ACCT_PLAN_WEB
"
<end output>

If I delete the header guard from acct_plan_web.h, it preprocesses
cleanly.

If I eliminate the declare section and use a more conventional

EXEC SQL INCLUDE acct_plan_web;

then I get:

<begin output>
$ make optim_accounts.c
ecpg optim_accounts.ec
optim_accounts.ec:482: ERROR: invalid datatype 'struct
acct_plan_web_st'
<end output>

Any ideas? Does ecpg not like typedefs? Am I going blind and missing
something obvious?

--
Al Balmer
Sun City, AZ

Reply With Quote
  #2  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: Problem with ecpg include file - 10-06-2006 , 04:18 AM






Al Balmer <albalmer (AT) att (DOT) net> wrote:
Quote:
I'm porting a number of programs from Informix embedded SQL to
postgreSQL, and ran into the following problem. The optim_accounts.ec
program included the following:

EXEC SQL BEGIN DECLARE SECTION;
EXEC SQL INCLUDE acct_plan_web.h;
EXEC SQL END DECLARE SECTION;
[...]

Quote:
If I delete the header guard from acct_plan_web.h, it preprocesses
cleanly.

If I eliminate the declare section and use a more conventional

EXEC SQL INCLUDE acct_plan_web;

then I get:

begin output
$ make optim_accounts.c
ecpg optim_accounts.ec
optim_accounts.ec:482: ERROR: invalid datatype 'struct
acct_plan_web_st'
end output

Any ideas? Does ecpg not like typedefs? Am I going blind and missing
something obvious?
I played around, and found that when you

EXEC SQL INCLUDE mumble;

ecpg will look for 'mumble' before looking for 'mumble.h'.

Is there maybe a file 'acct_plan_web' around?

Other than that, it should work.
I'll append some sample code that works on my PostgreSQL 8.1.4 system.

Yours,
Laurenz Albe


-------------------- begin ecpg.pgc ---------------------
#include <stdio.h>
#include <stdlib.h>

EXEC SQL BEGIN DECLARE SECTION;
EXEC SQL INCLUDE ecpg;
EXEC SQL END DECLARE SECTION;

int main(int argc, char** argv) {
EXEC SQL BEGIN DECLARE SECTION;
my_t my = (my_t)malloc(sizeof(struct mystruct));

EXEC SQL END DECLARE SECTION;

EXEC SQL CONNECT TO DEFAULT;

EXEC SQL SELECT 1, 2 INTO :my;

EXEC SQL DISCONNECT;

printf("my = { %ld, %ld }\n", my->a, my->b);

return 0;
}
------------------------ end ----------------------------

-------------------- begin ecpg.h -----------------------
typedef struct mystruct {
long a;
long b;
} *my_t;
------------------------ end ----------------------------

$ ecpg ecpg
$ gcc -Wall -g -o ecpgtest ecpg.c -I/magwien/postgres/include \
-L/magwien/postgres/lib -lecpg -Wl,-rpath,/magwien/postgres/lib
$ ./ecpgtest
my = { 1, 2 }


Reply With Quote
  #3  
Old   
Al Balmer
 
Posts: n/a

Default Re: Problem with ecpg include file - 10-06-2006 , 11:51 AM



On 06 Oct 2006 09:18:39 GMT, Laurenz Albe <invite (AT) spam (DOT) to.invalid>
wrote:

Quote:
Al Balmer <albalmer (AT) att (DOT) net> wrote:
I'm porting a number of programs from Informix embedded SQL to
postgreSQL, and ran into the following problem. The optim_accounts.ec
program included the following:

EXEC SQL BEGIN DECLARE SECTION;
EXEC SQL INCLUDE acct_plan_web.h;
EXEC SQL END DECLARE SECTION;

[...]

If I delete the header guard from acct_plan_web.h, it preprocesses
cleanly.

If I eliminate the declare section and use a more conventional

EXEC SQL INCLUDE acct_plan_web;

then I get:

begin output
$ make optim_accounts.c
ecpg optim_accounts.ec
optim_accounts.ec:482: ERROR: invalid datatype 'struct
acct_plan_web_st'
end output

Any ideas? Does ecpg not like typedefs? Am I going blind and missing
something obvious?

I played around, and found that when you

EXEC SQL INCLUDE mumble;

ecpg will look for 'mumble' before looking for 'mumble.h'.
That's a good thing to know, though it doesn't seem to be the problem
here. I notice that the include file in your sample doesn't have a
header guard, and my problem seems to go away if I remove the header
guard.

Searching the archives, I found some hints that there was a bug
involving #define statements and typedefs, but that it was fixed some
time ago. I know that ecpg won't process #defines, but I think it
should ignore them.

Anyway, thanks for the reply. It's good to know that someone is using
ecpg <g>. We're porting some ancient Informix_se programs, and so far
we like postgreSQL very much. Our support people love the fact that
they can use pgAdmin to maintain the customers' Unix databases.
Quote:
Is there maybe a file 'acct_plan_web' around?

Other than that, it should work.
I'll append some sample code that works on my PostgreSQL 8.1.4 system.

Yours,
Laurenz Albe


-------------------- begin ecpg.pgc ---------------------
#include <stdio.h
#include <stdlib.h

EXEC SQL BEGIN DECLARE SECTION;
EXEC SQL INCLUDE ecpg;
EXEC SQL END DECLARE SECTION;

int main(int argc, char** argv) {
EXEC SQL BEGIN DECLARE SECTION;
my_t my = (my_t)malloc(sizeof(struct mystruct));

EXEC SQL END DECLARE SECTION;

EXEC SQL CONNECT TO DEFAULT;

EXEC SQL SELECT 1, 2 INTO :my;

EXEC SQL DISCONNECT;

printf("my = { %ld, %ld }\n", my->a, my->b);

return 0;
}
------------------------ end ----------------------------

-------------------- begin ecpg.h -----------------------
typedef struct mystruct {
long a;
long b;
} *my_t;
------------------------ end ----------------------------

$ ecpg ecpg
$ gcc -Wall -g -o ecpgtest ecpg.c -I/magwien/postgres/include \
-L/magwien/postgres/lib -lecpg -Wl,-rpath,/magwien/postgres/lib
$ ./ecpgtest
my = { 1, 2 }
--
Al Balmer
Sun City, AZ


Reply With Quote
  #4  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: Problem with ecpg include file - 10-09-2006 , 02:38 AM



Al Balmer <albalmer (AT) att (DOT) net> wrote:
Quote:
I'm porting a number of programs from Informix embedded SQL to
postgreSQL, and ran into the following problem. The optim_accounts.ec
program included the following:

EXEC SQL BEGIN DECLARE SECTION;
EXEC SQL INCLUDE acct_plan_web.h;
EXEC SQL END DECLARE SECTION;

[...]

If I delete the header guard from acct_plan_web.h, it preprocesses
cleanly.

If I eliminate the declare section and use a more conventional

EXEC SQL INCLUDE acct_plan_web;

then I get:

begin output
$ make optim_accounts.c
ecpg optim_accounts.ec
optim_accounts.ec:482: ERROR: invalid datatype 'struct
acct_plan_web_st'
end output

Any ideas? Does ecpg not like typedefs? Am I going blind and missing
something obvious?

I played around, and found that when you

EXEC SQL INCLUDE mumble;

ecpg will look for 'mumble' before looking for 'mumble.h'.

That's a good thing to know, though it doesn't seem to be the problem
here. I notice that the include file in your sample doesn't have a
header guard, and my problem seems to go away if I remove the header
guard.
Ah, good, I thought there was another problem besides the #ifdefs.
You could report it as a bug on pgsql-bugs that the header guards make
ecpg choke. Maybe somebody will fix it some day.

Yours,
Laurenz Albe


Reply With Quote
  #5  
Old   
Al Balmer
 
Posts: n/a

Default Re: Problem with ecpg include file - 10-10-2006 , 06:15 PM



On 09 Oct 2006 07:38:15 GMT, Laurenz Albe <invite (AT) spam (DOT) to.invalid>
wrote:

Quote:
I played around, and found that when you

EXEC SQL INCLUDE mumble;

ecpg will look for 'mumble' before looking for 'mumble.h'.

That's a good thing to know, though it doesn't seem to be the problem
here. I notice that the include file in your sample doesn't have a
header guard, and my problem seems to go away if I remove the header
guard.

Ah, good, I thought there was another problem besides the #ifdefs.
You could report it as a bug on pgsql-bugs that the header guards make
ecpg choke. Maybe somebody will fix it some day.
Dang! I figured out what it was. If the struct definition is used in a
declare section, the definition itself has to be in a declare section,
else the preprocessor doesn't recognize it. The problem only seemed to
go away because the preprocessor stopped complaining (a bug in itself,
probably.)

The modified include file is:
<begin code>
#ifndef H_ACCT_PLAN_WEB
#define H_ACCT_PLAN_WEB

EXEC SQL BEGIN DECLARE SECTION; <--- this line added
typedef struct acct_plan_web_st {
long acct;
long plan;
long min_deposit;
long max_deposit;
} *user_t;
EXEC SQL END DECLARE SECTION; <--- this line added
#endif
<end code>

--
Al Balmer
Sun City, AZ


Reply With Quote
  #6  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: Problem with ecpg include file - 10-11-2006 , 02:25 AM



Al Balmer <albalmer (AT) att (DOT) net> wrote:
Quote:
Dang! I figured out what it was. If the struct definition is used in a
declare section, the definition itself has to be in a declare section,
else the preprocessor doesn't recognize it. The problem only seemed to
go away because the preprocessor stopped complaining (a bug in itself,
probably.)
So you think that the example I posted contains a bug that does not get
reported by ecpg? Or do I misunderstand you?
If you surround the EXEC SQL INCLUDE with a DECLARE SECTION, the struct
definition _is_ inside the DECLARE SECTION.

Quote:
The modified include file is:
begin code
#ifndef H_ACCT_PLAN_WEB
#define H_ACCT_PLAN_WEB

EXEC SQL BEGIN DECLARE SECTION; <--- this line added
typedef struct acct_plan_web_st {
long acct;
long plan;
long min_deposit;
long max_deposit;
} *user_t;
EXEC SQL END DECLARE SECTION; <--- this line added
#endif
end code
Do I get it right that you removed the EXEC SQL BEGIN/END DECLARE SECTION
surrounding the EXEC SQL INCLUDE in the .c file?

That would work. Take care that the header file is never #included in
regular C code.

Yours,
Laurenz Albe


Reply With Quote
  #7  
Old   
Al Balmer
 
Posts: n/a

Default Re: Problem with ecpg include file - 10-11-2006 , 10:55 AM



On 11 Oct 2006 07:25:11 GMT, Laurenz Albe <invite (AT) spam (DOT) to.invalid>
wrote:

Quote:
Al Balmer <albalmer (AT) att (DOT) net> wrote:
Dang! I figured out what it was. If the struct definition is used in a
declare section, the definition itself has to be in a declare section,
else the preprocessor doesn't recognize it. The problem only seemed to
go away because the preprocessor stopped complaining (a bug in itself,
probably.)

So you think that the example I posted contains a bug that does not get
reported by ecpg? Or do I misunderstand you?
If you surround the EXEC SQL INCLUDE with a DECLARE SECTION, the struct
definition _is_ inside the DECLARE SECTION.

I'm probably confusing myself as well as you <g>. This is a different
program, and the EXEC SQL INCLUDE was not in a declare section, but
the include file was the same. Loking back, the original program did
have the EXEC SQL INCLUDE in a declare section, so the mystery
remains. Now I have to go back to the original program and verify that
the modified header works with that (or remove the include from the
declare section there, too.)

I realize I'm ignorant on this subject. I've been writing C since the
mid-70's, but this is the first time I've had to deal with embedded
SQL. Quite likely the last - it's a tiny part of a large port, and
once everything builds and runs, I'll probably never have to deal with
this part again.

Quote:
The modified include file is:
begin code
#ifndef H_ACCT_PLAN_WEB
#define H_ACCT_PLAN_WEB

EXEC SQL BEGIN DECLARE SECTION; <--- this line added
typedef struct acct_plan_web_st {
long acct;
long plan;
long min_deposit;
long max_deposit;
} *user_t;
EXEC SQL END DECLARE SECTION; <--- this line added
#endif
end code

Do I get it right that you removed the EXEC SQL BEGIN/END DECLARE SECTION
surrounding the EXEC SQL INCLUDE in the .c file?

That would work. Take care that the header file is never #included in
regular C code.

Yours,
Laurenz Albe
--
Al Balmer
Sun City, AZ


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.