Problems using $typedef in a header file ?
Posted in 2000
Topics: High Availability & Replication
Hello. I am having problems compiling the following : /* head.ec */ #include <stdio.h> #include "head.h" int main(void){ head H; strcpy(H.name,"hello") } /* head.h */ #include <sqlhdr.h> $typedef struct head { char name[10]; int a; } _n; Can anyone help ? Thanks Richard.
This is because #include gets expanded only after running esql, and then it's too late to use $ keywords. You could either specify -cp, so that the cpp preprocessor is ran before esql (esql -cp foo.ec). Or you could use $include (or EXEC SQL include) instead of #include. $include head.h; instead of #include "head.h" Richard wrote: > > Hello. > > I am having problems compiling the following : > > /* head.ec */ > #include <stdio.h> > #include "head.h" > > int main(void){ > > head H; > > strcpy(H.name,"hello") > > } > > /* head.h */ > #include <sqlhdr.h> > > $typedef struct head { > char name[10]; > int a; > } _n; > > Can anyone help ? > > Thanks > > Richard.
Richard wrote: > I am having problems compiling the following : > > /* head.ec */ > #include <stdio.h> > #include "head.h" > > int main(void){ > > head H; > > strcpy(H.name,"hello") > > } > > /* head.h */ > #include <sqlhdr.h> > > $typedef struct head { > char name[10]; > int a; > } _n; > > Can anyone help ? As David Stes suggested, a large part of the answer is to use: EXEC INCLUDE head; or, equivalently, $include head; (I'm 99% sure the ESQL/C preprocessor adds the ".h" automatically). Certainly, you have to get across this hurdle before you can run into the other troubles. You can get the ESQL/C script to run the C preprocessor (as David suggested) before it runs the ESQL/C preprocessor. This is a fairly new feature (only been around two or three years) and I don't use it myself -- mainly because I have some idea how much code the C preprocessor emits which I really don't want the ESQL/C preprocessor to have to deal with. However, there are other problems. Your head.h file is not currently usable except in an ESQL/C program. Typically, you want to be able to use it in non-ESQL/C code too. To get that to work, you have to: * Remove the $ from the typedef. * Enclose the $include inside a declare section. EXEC SQL BEGIN DECLARE SECTION; EXEC SQL INCLUDE head; EXEC SQL END DECLARE SECTION; Also, you 99.999% probably shouldn't need to include <sqlhdr.h> in head.h. Also the code shown doesn't define a type head (it defines a struct head and an alias _n for it). -- Jonathan Leffler (jleffler@informix.com, jleffler@earthlink.net) Guardian of DBD::Informix v1.00.PC1 -- see http://www.perl.com/CPAN #include <disclaimer.h>