Thread: ECPG and C++ compilation
Has anybody tried to compile the source of test2.pgc (included in the sources postgresql-7.3.1/src/interfaces/ecpg/test dir) with a C++ compiler?? I've tried this on Solaris 8 with the SUN compiler (CC: Sun WorkShop 6 update 1 C++ 5.2 Patch 109508-08 2002/03/07) as well as with g++ (3.1) What I get is the following error: test2.pgc: In function `int main()': test2.pgc:71: `sizeof' applied to incomplete type `varchar_name' test2.pgc:101: `sizeof' applied to incomplete type `varchar_name' Can anybody suggest a method to overcome this problem?
Demetres Pantermalis writes: > What I get is the following error: > test2.pgc: In function `int main()': > test2.pgc:71: `sizeof' applied to incomplete type `varchar_name' > test2.pgc:101: `sizeof' applied to incomplete type `varchar_name' > > Can anybody suggest a method to overcome this problem? Use a C compiler to compile C code and a C++ compiler to compile C++ code, and then link them together if you are so inclined. -- Peter Eisentraut peter_e@gmx.net
Thanks for your reply. However, I don't think that the problem has to do with C or C++ code (as source) and as a proof, all of the examples in the test directory compile without a problem with the C++ compilers, except test2.pgc and test3.pgc which include VARCHAR definitions (and test4.pgc which has a different problem - at line 15 text[10] should be changed to text[11]). The problem is more specific with the generated code from 'ecpg' regarding this VARCHAR type. A more detailed investigation on the issue: The generated code contains the following lines: #line 20 "test2.pgc" struct personal_struct { #line 20 "test2.pgc" struct varchar_name { int len; char arr[ 8 ]; } name ; #line 21 "test2.pgc" birthinfo birth ;} personal , * p ; and after some other lines, the 'varchar_name' struct is used to 'fetch' from the cursor in the following way: while (1) { strcpy(msg, "fetch"); { ECPGdo(__LINE__,NULL, "fetch cur", ECPGt_EOIT, ECPGt_varchar,&(p->name),8L,1L,sizeof(struct varchar_name), ECPGt_int,&(i->ind_name),1L,1L,sizeof(int), ECPGt_long,&(p->birth.born),1L,1L,sizeof(long), ECPGt_long,&(i->ind_birth.born),1L,1L,sizeof(long), ECPGt_short,&(p->birth.age),1L,1L,sizeof(short), ECPGt_short,&(i->ind_birth.age),1L,1L,sizeof(short), ECPGt_char,&(married),0L,1L,1*sizeof(char), ECPGt_long,&(ind_married),1L,1L,sizeof(long), ECPGt_int,&(children.integer),1L,1L,sizeof(int), ECPGt_short,&(ind_children.smallint),1L,1L,sizeof(short), ECPGt_EORT); Everything is OK for C, but for C++ the struct 'varchar_name' is considered an incomplete type and therefore the sizeof(struct varchar_name) produces the error. Replacing sizeof(struct varchar_name) with a constant value (ie 4(for len)+8(for arr) =12) then the compilation with the C++ compiler is successful... I've run some tests with Oracle's ESQL preprocessor just to see what is produced. Oracle does something similar but seems to calculate the sizeof the structure and uses this value instead of sizeof(struct ...). Another solution is to 'typedef' the varchar_name structure, before the definition of the personal_struct (ie typedef struct varchar_name { int len; char arr[ 8 ]; } varchar_name;) and then use 'varchar_name name;' inside the personal_struct (...but this means post-process the pre-processor's output!!! ). Any comments appreciated... Regards, Demetres -----Original Message----- From: Peter Eisentraut [mailto:peter_e@gmx.net] Sent: Thursday, February 20, 2003 7:16 PM To: Demetres Pantermalis Cc: pgsql-interfaces@postgresql.org Subject: Re: [INTERFACES] ECPG and C++ compilation Demetres Pantermalis writes: > What I get is the following error: > test2.pgc: In function `int main()': > test2.pgc:71: `sizeof' applied to incomplete type `varchar_name' > test2.pgc:101: `sizeof' applied to incomplete type `varchar_name' > > Can anybody suggest a method to overcome this problem? Use a C compiler to compile C code and a C++ compiler to compile C++ code, and then link them together if you are so inclined. -- Peter Eisentraut peter_e@gmx.net
On Fri, 2003-02-21 at 02:37, Demetres Pantermalis wrote: > Thanks for your reply. > > However, I don't think that the problem has to do with C or C++ code (as > source) and as a proof, all of the examples in the test directory compile > without a problem with the C++ compilers, except test2.pgc and test3.pgc > which include VARCHAR definitions (and test4.pgc which has a different > problem - at line 15 text[10] should be changed to text[11]). > The problem is more specific with the generated code from 'ecpg' regarding > this VARCHAR type. > > A more detailed investigation on the issue: > The generated code contains the following lines: > #line 20 "test2.pgc" > struct personal_struct { > #line 20 "test2.pgc" > struct varchar_name { int len; char arr[ 8 ]; } name ; > > #line 21 "test2.pgc" > birthinfo birth ; > } personal , * p ; > > and after some other lines, the 'varchar_name' struct is used to 'fetch' > from the cursor in the following way: > while (1) { > strcpy(msg, "fetch"); > { ECPGdo(__LINE__, NULL, "fetch cur", ECPGt_EOIT, > ECPGt_varchar,&(p->name),8L,1L,sizeof(struct varchar_name), > ECPGt_int,&(i->ind_name),1L,1L,sizeof(int), > ECPGt_long,&(p->birth.born),1L,1L,sizeof(long), > ECPGt_long,&(i->ind_birth.born),1L,1L,sizeof(long), > ECPGt_short,&(p->birth.age),1L,1L,sizeof(short), > ECPGt_short,&(i->ind_birth.age),1L,1L,sizeof(short), > ECPGt_char,&(married),0L,1L,1*sizeof(char), > ECPGt_long,&(ind_married),1L,1L,sizeof(long), > ECPGt_int,&(children.integer),1L,1L,sizeof(int), > ECPGt_short,&(ind_children.smallint),1L,1L,sizeof(short), > ECPGt_EORT); > > Everything is OK for C, but for C++ the struct 'varchar_name' is considered > an incomplete type and therefore the sizeof(struct varchar_name) produces > the error. Replacing sizeof(struct varchar_name) with a constant value (ie > 4(for len)+8(for arr) =12) then the compilation with the C++ compiler is > successful... > I've run some tests with Oracle's ESQL preprocessor just to see what is > produced. Oracle does something similar but seems to calculate the sizeof > the structure and uses this value instead of sizeof(struct ...). > > Another solution is to 'typedef' the varchar_name structure, before the > definition of the personal_struct (ie typedef struct varchar_name { int len; > char arr[ 8 ]; } varchar_name;) and then use 'varchar_name name;' inside > the personal_struct (...but this means post-process the pre-processor's > output!!! ). > I don't think that will work. I can never get ecpg to recognize typedefed structs, even when declaring the typedefed struct within a DECLARE SECTION. Probably doing the sizeof calculation within ecpg would be the proper way to go, and using that value in the ECPGdo call instead of sizeof(whatever). With respect to C++, anyhow. I'm sure the developers would accept a patch, but realize that ecpg generates native C code, and follows C conventions. C++ may parse the code differently in some cases. Your best bet is going to be using the C compiler on these files to generate you object code, and your C++ compiler on your native C++ code, as Peter suggested. > > -----Original Message----- > From: Peter Eisentraut [mailto:peter_e@gmx.net] > Sent: Thursday, February 20, 2003 7:16 PM > To: Demetres Pantermalis > Cc: pgsql-interfaces@postgresql.org > Subject: Re: [INTERFACES] ECPG and C++ compilation > > > Demetres Pantermalis writes: > > > What I get is the following error: > > test2.pgc: In function `int main()': > > test2.pgc:71: `sizeof' applied to incomplete type `varchar_name' > > test2.pgc:101: `sizeof' applied to incomplete type `varchar_name' > > > > Can anybody suggest a method to overcome this problem? > > Use a C compiler to compile C code and a C++ compiler to compile C++ code, > and then link them together if you are so inclined. > -- Matthew Vanecek perl -e 'print $i=pack(c5,(41*2),sqrt(7056),(unpack(c,H)-2),oct(115),10);' ******************************************************************************** For 93 million miles, there is nothing between the sun and my shadow except me. I'm always getting in the way of something...
On Sat, Feb 22, 2003 at 09:14:43AM -0600, Matthew Vanecek wrote: > I don't think that will work. I can never get ecpg to recognize > typedefed structs, even when declaring the typedefed struct within a > DECLARE SECTION. Now that I do not understand. What exactly doesn't work? do you have a small example? > Probably doing the sizeof calculation within ecpg would be the proper > way to go, and using that value in the ECPGdo call instead of > sizeof(whatever). With respect to C++, anyhow. I'm sure the developers But that would mean you cannot precompile on one machine for usage on another. Right? > would accept a patch, but realize that ecpg generates native C code, and > follows C conventions. C++ may parse the code differently in some > cases. Your best bet is going to be using the C compiler on these files > to generate you object code, and your C++ compiler on your native C++ > code, as Peter suggested. I agree as well. Michael -- Michael Meskes Email: Michael@Fam-Meskes.De ICQ: 179140304 Go SF 49ers! Go Rhein Fire! Use Debian GNU/Linux! Use PostgreSQL!
On Mon, 2003-02-24 at 09:24, Michael Meskes wrote: > On Sat, Feb 22, 2003 at 09:14:43AM -0600, Matthew Vanecek wrote: > > I don't think that will work. I can never get ecpg to recognize > > typedefed structs, even when declaring the typedefed struct within a > > DECLARE SECTION. > > Now that I do not understand. What exactly doesn't work? do you have a > small example? > #include <stdio.h> EXEC SQL BEGIN DECLARE SECTION; struct s_somestruct { int id; char *field; }; typedef struct s_somestruct SomeStruct; EXEC SQL END DECLARE SECTION; int main(int argc, char **argv) { printf("this is just to show parse errors from ecpg\n"); return 0; } yields: me2v@reliant conn $ ecpg example.pgc example.pgc:7: ERROR: parse error at or near ";" If instead I use: #include <stdio.h> struct s_somestruct { int id; char *field; }; EXEC SQL BEGIN DECLARE SECTION; typedef struct s_somestruct SomeStruct; EXEC SQL END DECLARE SECTION; int main(int argc, char **argv) { printf("this is just to show parse errors from ecpg\n"); return 0; } I get: me2v@reliant conn $ ecpg example.pgc example.pgc:9: ERROR: parse error at or near "SomeStruct" I get this error also if I use: EXEC SQL BEGIN DECLARE SECTION; struct s_somestruct { int id; char *field; } a_struct; typedef struct s_somestruct SomeStruct; EXEC SQL END DECLARE SECTION; Doing "typedef struct { /* stuff */ } AStruct;" appears to work with ecpg from 7.3.2, but IIRC, it would not work in 7.2.3. > > Probably doing the sizeof calculation within ecpg would be the proper > > way to go, and using that value in the ECPGdo call instead of > > sizeof(whatever). With respect to C++, anyhow. I'm sure the developers > > But that would mean you cannot precompile on one machine for usage on > another. Right? > Is sizeof() a compile-time execution/calculation? If so, then it would definitely not be portable, unless you implemented/used a generic types library (such as glib, for example). Since it works in C, though, and ECPG generates C, I'm by no means making a suggestion to follow this path. ;) -- Matthew Vanecek perl -e 'print $i=pack(c5,(41*2),sqrt(7056),(unpack(c,H)-2),oct(115),10);' ******************************************************************************** For 93 million miles, there is nothing between the sun and my shadow except me. I'm always getting in the way of something...
On Tue, Feb 25, 2003 at 02:44:45PM -0600, Matthew Vanecek wrote: > EXEC SQL BEGIN DECLARE SECTION; > struct s_somestruct { > int id; > char *field; > }; > > typedef struct s_somestruct SomeStruct; > EXEC SQL END DECLARE SECTION; Ah, now I understand. Yes, this is a long standing bug. Or make that a missing feature. The struct definition has to be listed inside the typedef. > Doing "typedef struct { /* stuff */ } AStruct;" appears to work with > ecpg from 7.3.2, but IIRC, it would not work in 7.2.3. I'm note sure what worked in 7.2.3. :-) > Is sizeof() a compile-time execution/calculation? If so, then it would AFAIK yes. > definitely not be portable, unless you implemented/used a generic types > library (such as glib, for example). Since it works in C, though, and > ECPG generates C, I'm by no means making a suggestion to follow this > path. ;) Michael -- Michael Meskes Email: Michael@Fam-Meskes.De ICQ: 179140304 Go SF 49ers! Go Rhein Fire! Use Debian GNU/Linux! Use PostgreSQL!