话不多说,看代码:
a.pgc 文件:
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
-
#include <curses.h>
-
-
int dbconnect( void );
-
-
int main ( void )
-
{
-
WINDOW *sc;
-
-
initscr();
-
-
sc= newwin(24,80,0,0);
-
box(sc,0,0);
-
mvwprintw(sc,8,8,"Connect DataBase = %d",dbconnect ());
-
touchwin(sc);wrefresh(sc);
-
-
wgetch(sc);
-
-
endwin();
-
return 0;
-
}
-
-
int dbconnect ( void )
-
{
-
EXEC SQL BEGIN DECLARE SECTION;
-
char *target="sampledb@localhost:5432";
-
char *user="postgres";
-
char *passwd="postgres";
-
int port = 5432;
-
EXEC SQL END DECLARE SECTION;
-
-
exec sql connect to :target user :user using :passwd;
-
-
-
if (sqlca.sqlcode == 0 )
-
printf("connect sucesess ... \n");
-
else {
-
printf("connect faild ... ... \n");
-
printf( " sqlca.sqlcode = %d \n",sqlca.sqlcode );
-
}
-
-
return sqlca.sqlcode;
-
}
Makefile 文件
-
TARGET = demo_a
-
-
CC = cc
-
CFLAGS = -O2 -c -K PIC -DDEBUG
-
# -Wall
-
-
ECPG = ecpg -c
-
-
AR = ar -rcvl
-
RANLIB = ranlib
-
-
INCLDIR = -I. -I$(POSTGRESQL_DIR)/include -I $(HOME)/source_code/include -I/usr/include/libxml2
-
LIBDIR = -L. -L$(HOME)/source_code/lib -L$(POSTGRESQL_DIR)/lib
-
LIBS = -lecpg -lcurses
-
-
OBJ =
-
-
all:$(TARGET)
-
-
clean:
-
rm -f $(TARGET) *.o core.* .*.swp *.pgc~ *.c~
-
-
demo_a:a.o
-
$(CC) -o $@ $^ $(LIBDIR) $(LIBS)
-
# mv $@ $(HOME)/bin
-
-
-
# ################################################################################
-
-
.SUFFIXES:
-
.SUFFIXES:.c .o .pgc
-
-
.pgc.c:
-
$(ECPG) $(INCLUDE) $<
-
-
.c.o:
-
$(CC) $(CFLAGS) $(INCLDIR) $<
编译出错如下:
ecpg -c a.pgc
cc -O2 -c -K PIC -DDEBUG -I. -I/opt/PostgreSQL/12.9/include -I /home/work/source_code/include -I/usr/include/libxml2 a.c
UX:acomp: ERROR: "/usr/include/curses.h", line 60: invalid type combination
UX:acomp: WARNING: "/usr/include/curses.h", line 60: useless declaration
UX:acomp: WARNING: "/usr/include/curses.h", line 60: typedef declares no type name
gmake: *** [a.o] Error 1
rm a.c
出错的原因是 bool 定义冲突,找了一圈,除了将 cursess.h 文件中的 bool 定义删除能解决外,其它的办法没有找到。
一个可以解决的合理办法是将上面的程序拆分,拆分后的结果如下:
a.c 文件
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
-
#include <curses.h>
-
-
int dbconnect( void );
-
-
int main ( void )
-
{
-
WINDOW *sc;
-
-
initscr();
-
-
sc= newwin(24,80,0,0);
-
box(sc,0,0);
-
mvwprintw(sc,8,8,"Connect DataBase = %d",dbconnect ());
-
touchwin(sc);wrefresh(sc);
-
-
wgetch(sc);
-
-
endwin();
-
return 0;
-
}
b.pgc 文件
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
-
int dbconnect ( void )
-
{
-
EXEC SQL BEGIN DECLARE SECTION;
-
char *target="sampledb@localhost:5432";
-
char *user="postgres";
-
char *passwd="postgres";
-
int port = 5432;
-
EXEC SQL END DECLARE SECTION;
-
-
exec sql connect to :target user :user using :passwd;
-
-
-
if (sqlca.sqlcode == 0 )
-
printf("connect sucesess ... \n");
-
else {
-
printf("connect faild ... ... \n");
-
printf( " sqlca.sqlcode = %d \n",sqlca.sqlcode );
-
}
-
-
return sqlca.sqlcode;
-
}
Makefile 文件
-
TARGET = demo_a
-
-
CC = cc
-
CFLAGS = -O2 -c -K PIC -DDEBUG
-
# -Wall
-
-
ECPG = ecpg -c
-
-
AR = ar -rcvl
-
RANLIB = ranlib
-
-
INCLDIR = -I. -I$(POSTGRESQL_DIR)/include -I $(HOME)/source_code/include -I/usr/include/libxml2
-
LIBDIR = -L. -L$(HOME)/source_code/lib -L$(POSTGRESQL_DIR)/lib
-
LIBS = -lecpg -lcurses
-
-
OBJ =
-
-
all:$(TARGET)
-
-
clean:
-
rm -f $(TARGET) *.o core.* .*.swp *.pgc~ *.c~
-
-
demo_a:a.o b.o
-
$(CC) -o $@ $^ $(LIBDIR) $(LIBS)
-
# mv $@ $(HOME)/bin
-
-
-
# ################################################################################
-
-
.SUFFIXES:
-
.SUFFIXES:.c .o .pgc
-
-
.pgc.c:
-
$(ECPG) $(INCLUDE) $<
-
-
.c.o:
-
$(CC) $(CFLAGS) $(INCLDIR) $<
编译结果如下:
cc -O2 -c -K PIC -DDEBUG -I. -I/opt/PostgreSQL/12.9/include -I /home/work/source_code/include -I/usr/include/libxml2 a.c
ecpg -c b.pgc
cc -O2 -c -K PIC -DDEBUG -I. -I/opt/PostgreSQL/12.9/include -I /home/work/source_code/include -I/usr/include/libxml2 b.c
cc -o demo_a a.o b.o -L. -L/home/work/source_code/lib -L/opt/PostgreSQL/12.9/lib -lecpg -lcurses
rm b.c
总结:
=========================================================
ECPG 编译时,首先将 .pgc 的文件编译成 .c 的文件,然后再调用 cc 编译成可执行文件,当 .pgc 文件中有界面操作的头文件时,其生成的 .c 文件中会产生 bool 定义的冲突。
一个解决的办法是:将 .pgc 文件与 .c 的文件进行分开,在 .pgc 的文件中,不要引用 curses.h 的头文件,即: .pgc 程序中,仅实现针对数据库的操作,不做与界面相关的事情,即可很好的避开这个问题。
阅读(1161) | 评论(0) | 转发(0) |