开发者

How to write a shell script to compile multiple embedded sql in C files (.sqc)?

I have written 3 .sqc files i.e. embedded sql in host language C. I need to make a (Unix) shell script to simply compile all 3 sqc files in a row. How can I do that? Right now, I can individually run each .sqc file using a Makefile that basically converts the .sqc file to a c file and then compiles it. Can I make 3 individual Makefiles and run all of them through a shell script? If so, then how? Can I make one Makefile that can compile all 3 .sqc independently and compile them thereafter through a shell script? If so, then how? Any other options?

Here is the Makefile that can only compile a single .sqc file:

NAME=sample

DB2PATH = /sqllib
CC=gcc
CFLAGS=-I$(DB2PATH)/include
LIBS=-L$(DB2PA开发者_运维百科TH)/lib -R$(DB2PATH)/lib -ldb2

all: $(NAME)

$(NAME): $(NAME).sqc util.o
    db2 connect to sampleDB
    db2 prep $(NAME).sqc bindfile
    db2 bind $(NAME).bnd
    db2 connect reset
    $(CC) $(CFLAGS) -c $(NAME).c 
    $(CC) $(CFLAGS) -o $(NAME) $(NAME).o util.o $(LIBS)

clean:
    rm -f $(NAME) $(NAME).c $(NAME).o $(NAME).bnd

util.o : util.c
    $(CC) -c util.c $(CFLAGS)

A possible (Unix) shell script and Makefile example would suffice to help.

Thank you.


This Makefile should do all three in one step, just type "make". Note that you'll have to change the second line to reflect the names of your real .sqc files.

Also note that I'm not familiar with sqc and I haven't tested this, I'm just working from your Makefile.

# THIS IS THE ONLY LINE YOU'LL HAVE TO CHANGE:
NAMES = file1 file2 file3

DB2PATH = /sqllib
CC=gcc
CFLAGS=-I$(DB2PATH)/include
LIBS=-L$(DB2PATH)/lib -R$(DB2PATH)/lib -ldb2

all: $(NAMES)

# This will convert .sqc into .c
%.c: %.sqc
    db2 connect to sampleDB
    db2 prep $< bindfile
    db2 bind $*.bnd
    db2 connect reset

# This will compile .c into .o, whether it's fileN.c or util.c
%.o: %.c
    $(CC) $(CFLAGS) -c $< -o $@

# This will link fileN.o and util.o into fileN
$(NAMES): % : %.o util.o
    $(CC) $(CFLAGS) -o $@ $^ $(LIBS)

# This is just to assure Make that that isn't really a file called "clean"
.PHONY: clean

clean:
    rm -f $(NAMES) $(NAMES:=.c) $(NAMES:=.o) $(NAMES:=.bnd)


DB2PATH = /sqllib 
CC=gcc 
CFLAGS=-I$(DB2PATH)/include 
LIBS=-L$(DB2PATH)/lib -R$(DB2PATH)/lib -ldb2 

all: $(NAME) 

$(NAME): $(NAME).sqc util.o 
    db2 connect to sampleDB 
    db2 prep $(NAME).sqc bindfile 
    db2 bind $(NAME).bnd 
    db2 connect reset 
    $(CC) $(CFLAGS) -c $(NAME).c  
    $(CC) $(CFLAGS) -o $(NAME) $(NAME).o util.o $(LIBS) 

clean: 
    rm -f $(NAME) $(NAME).c $(NAME).o $(NAME).bnd 

util.o : util.c 
    $(CC) -c util.c $(CFLAGS) 

suppose you have three files: file1.sqc file2.sqc file3.sqc, and your makefile is saved as mksqc.mk

Script:

make -f mksqc.mk NAME=file1
make -f mksqc.mk NAME=file2
make -f mksqc.mk NAME=file3
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜