bash script to compile multiple .c files with some options


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash script to compile multiple .c files with some options
# 1  
Old 02-16-2011
Data bash script to compile multiple .c files with some options

I'm trying to write a bash script and call it "compile" such that running it allows me to compile multiple files with the options "-help," "-backup," and "-clean". I've got the code for the options written, i just can't figure out how to read the input string and then translate that into option running.
NOTE options are supposed to be used in the order they are written and can be written in any order.
# 2  
Old 02-16-2011
Why not just write a Makefile?
# 3  
Old 02-16-2011
Code:
#!/bin/bash

opt=$1
case $opt in
        -help)
                echo "compiler with -help..."
                ;;
        -backup)
                echo "compiler with -backup..."
                ;;
        -clean)
                echo "compiler with -clean..."
                ;;
        *)
                echo "Usage: $0 {-help|-backup|-clean}"
                exit
                ;;
esac

# 4  
Old 02-16-2011
As pludi said, makefiles are great for this kind of job.
What you have is: to to create something, this otherthing must be done before.

A common place on source compilation is:
I need to build the executable, but first I need to build each individual object. Than I link all objects to produce an executable.

The beauty on Makefiles is that it "knowns" your source file has changed and build only that single file you need.

Code:
$ cat Makefile

PROJECT = MyProject
EXECUTABLE = MyProgram
SOURCE = file1.c file2.c file3.c
OBJECTS = file1.o file2.o file3.o
#This is an easier way to define OBJECTS:
#OBJECTS = $(OBJECTS:%.c=%.o)

help:
   echo Here is a help message

# Prior to "backup", make will run "clean".
backup: clean
   cd .. && tar czvf /backup/$(PROJECT).tar.gz $(PROJECT)

clean:
   rm -f $(OBJECTS) $(EXECUTABLE)

CFLAGS = -Wall -ggdb3 -DSOME_DEFINE=1
LDFLAGS = -L/usr/local/lib -lsomelibname

# All the magic is here

# This is your main target, which depends upon target "program",
# you executable file.

all: $(EXECUTABLE)

# To build "$(EXECUTABLE)", you first need to build all separeted object file,
# i.e., each of your .c file must produce a corresponding .o file
# The special variable $@ means "the target name". On this case, it is "$(EXECUTABLE)'s value"
# The special variable $^ means "all depend targets". On this case, it is $(OBJECTS)'s value

$(EXECUTABLE): $(OBJECTS)
   gcc $(LDFLAGS) $(CFLAGS) -o $@ $^

# This is where any .c file becomes .o file

%.o: %.c
   gcc $(CFLAGS) -c $^ -o $@

You can now run:
Code:
$ make all

if you update file2.c, make sees it's timestamp is newer than corresponding file2.o and recompiles it, then relinks it into your executable.

To cleanup all objects:
Code:
$ make clean

And so on...
Code:
$ make help
$ make backup

Nice, uh?

PS: That makefile was not tested, probably have some problems.
# 5  
Old 02-16-2011
Perfect, thanks! I'm new to my command terminal, thanks for the Make tip
# 6  
Old 02-22-2011
I'm 65 years old and I just started programming in C using Ubuntu for fun.
Very curious as to whether this could be done without using makefiles?
Is it possible.
Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Running options in bash script

Hello UNIX & Linux Forums community! Long time Linux daily user hobbyist, new to shell scripting.... I'm working on a script that does all the "work" in one script, and makes calls to a second script to display info to the user via mostly expanding variables in heredocs. I'm contemplating... (6 Replies)
Discussion started by: Cody Learner
6 Replies

2. Shell Programming and Scripting

Loop through multiple files in bash script

Hi Everybody, I'm a newbie to shell scripting, and I'd appreciate some help. I have a bunch of .txt files that have some unwanted content. I want to remove lines 1-3 and 1028-1098. #!/bin/bash for '*.txt' in <path to folder> do sed '1,3 d' "$f"; sed '1028,1098 d' "$f"; done I... (2 Replies)
Discussion started by: BabyNuke
2 Replies

3. Shell Programming and Scripting

Passing multiple files to awk for processing in bash script

Hi, I'm using awk command in bash script. I'm able to pass multiple files to awk for processing.The code i can use is as below(sample code) #!/bin/bash awk -F "," 'BEGIN { ... ... ... }' file1 file2 file3 In the above code i'm passing the file names manually and it is fine till my... (7 Replies)
Discussion started by: shree11
7 Replies

4. Shell Programming and Scripting

Bash script with python slicing on multiple data files

I have 2 files generated in linux that has common output and were produced across multiple hosts with the same setup/configs. These files do some simple reporting on resource allocation and user sessions. So, essentially, say, 10 hosts, with the same (2) system reporting in the files, so a... (0 Replies)
Discussion started by: jdubbz
0 Replies

5. Shell Programming and Scripting

Bash script to copy timestamps of multiple files

Hi, I have a bunch of media files in a directory that have been converted (from MTS to MOV format), so my directory contains something like this: clip1.mts clip1.mov clip2.mts clip2.mov The problem is that the .mov files that have been created have the timestamps of the conversion task,... (2 Replies)
Discussion started by: Krakus
2 Replies

6. Shell Programming and Scripting

Sed inside bash script - accessing multiple files

I have a file (email) containing email addresses. I have a second file (terms) that contains simple regular expressions and words/characters. Here are some examples: \.trainee \.group \.web I want to go through email and delete lines containing the expressions/words from terms and write... (1 Reply)
Discussion started by: manouche
1 Replies

7. Shell Programming and Scripting

How to write bash script to explode multiple zip files

I have a directory full of zip files. How would I write a bash script to enumerate all the zip files, remove the ".zip" from the file name, create a directory by that name and unzip each zip file into its corresponding directory? Thanks! Siegfried (3 Replies)
Discussion started by: siegfried
3 Replies

8. Shell Programming and Scripting

moving multiple folders/files in subversion using bash script

Hi, I'm new here an dlearning a lot from this forum. i didnt find any solution for this in the forum. I have already checked in folders in subversion named HTT01,... HTT21.. and have files in each folder like below: HTT01/HTT01_00000.hex HTT01/HTT01_00000_fb_result.hex... (2 Replies)
Discussion started by: ravishan21
2 Replies

9. Shell Programming and Scripting

bash - batch script for extracting one file from multiple tar files

so i have hundreds of files named history.20071112.tar (history.YYYYMMDD.tar) and im looking to extract one file out of each archive called status_YYYYMMDDHH:MM.lis here is what i have so far: for FILE in `cat dirlist` do tar xvf $FILE ./status_* done dirlist is a text... (4 Replies)
Discussion started by: kuliksco
4 Replies

10. AIX

Compile Multiple Files

Hi All, I need a script to compile multiple *.pli source files. I can take care of the compile part, it's getting the list of files into an array or parsable string that I'm having the difficult with. ls or find come to mind, but I can only redirect those to a file. I need to use the file... (2 Replies)
Discussion started by: LouPelagalli
2 Replies
Login or Register to Ask a Question