tags:

views:

169

answers:

7

Hi,

I want to process each source code file after it has been preprocessed:

myprocess `gcc -E file1.c`  
myprocess `gcc -E file2.c`  
...  
myprocess `gcc -E fileN.c`

This gets tedious so how do I make this a single command?
That is, something along the line:

myprocess SOMETHINGMAGIC(gcc -E file*.c)

Thanks in advance!

+5  A: 

You mean like

   for i in file*.c ; do
        myprocess `gcc -E $i`
    done
Paul Tomblin
I think you mean "for" not "foreach" for bash.
j_random_hacker
yep, it should be: for i in file*.c; do
skinp
Right. Fixed now, thanks guys. No excuses - I normally hate C-shell.
Paul Tomblin
ok, this will work. i was thinking about a more powerful form of $(), thus didn't think of an explicit loop. Thanks!
John Lane
+2  A: 

If this is part of an ongoing processes (as opposed to a one time thing), use make, it is good at automating work pipelines.

In particular use suffix rules with traditional make or gmake style implicit rules.

Here is an outline for a suffix rule implementation:

.c.cpre:
         $(CC) -E $(CPPFLAGS) -o $@ $<
.cpre.cmy:
         $(MY_PROCESS) $<
         # Or whatever syntax you support..
         #
         # You could 
         # $(RM) $<
         # here, but I don't recommend it
.cmy.o:
         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<
         # $(RM) $<
dmckee
An interesting solution, and not one I would have thought of. Probably not worth it unless he's going to have to do this many times during the life of his project.
Paul Tomblin
@Paul: True, enough. But everytime, I think "this is a one off so why automate it?" I have to do it again next week. Grrr...
dmckee
I didn't think of this at all! Thanks, this will do!
John Lane
@dmckee - Been there, done that, got the t-shirt.
Paul Tomblin
+1  A: 

Perhaps this is what you're looking for?:

for i in file*.c ; do 
    echo "Header for myprocess: $i" 
    gcc -E $i 2>&1 
done | myprocess

2>&1 assumes you want to grab stderr, too
echo ... gives myprocess a starting point for each compilation

Brent.Longborough
A: 

How about:

myprocess {$(gcc -E file1.c),$(gcc -E file2.c),$(gcc -E file3.c)}

Or if you pipe it:

{$(gcc -E file1.c),$(gcc -E file2.c),$(gcc -E file3.c)} | myprocess

It's been a while since I've used bash, so, please, point out my mistakes!

Lucas Jones
+1  A: 

No magic nedded, just

my_process $(gcc -E *.c)

Note that I used the $(command) form because backticks are deprecated.

As an aside: are you sure you want to do that? You are putting the whole output of gcc -E as command line parameters of my_process. Not sure this is what you want. Maybe you want to use a simple pipe

gcc -E file.c | my_process

so that the output of gcc becomes the input of my_process.

In the latter case something like

for c_file in *.c ; do
    gcc -E $c_file | myprocess > ${c_file}.processed
done

would do the trick.

rjack
A: 

I think Paul's simple loop and dmckee's Makefile solution suit my needs best! Thanks for all your suggestions!

John Lane
John, if you're fully satisfied, it would be polite to accept one of them...
Brent.Longborough
Thanks Brent - I will! I'm still learning how to use the features on this site...
John Lane
A: 

ls file*.c | xargs --replace myprocess $(gcc -E {})

frankc