话不多说,看代码:
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 程序中,仅实现针对数据库的操作,不做与界面相关的事情,即可很好的避开这个问题。
阅读(840) | 评论(0) | 转发(0) |