2

I'm trying to write a bash script that will take in an optional argument, and based on the value of that argument, compile code using that argument as a preprocessor directive. This is my file so far:

#!/bin/bash

OPTIMIZE="$1"

if[ $OPTIMIZE = "OPTIMIZE" ] then
    echo "Compiling optimized algorithm..."
    gcc -c -std=c99 -O2 code.c -D $OPTIMIZE
else
    echo "Compiling naive algorithm..."
    gcc -c -std=c99 -O2 code.c 
fi

However, it doesn't seem to like the "-D" option, complaining that there is a macro name missing after -D. I was under the impression -D defines a new macro (as 1) with name of whatever is specified. I wanted "OPTIMIZE" to be the name of that macro. Any hints?

2
  • Why do you have such a script? What for? How would code.o be used? Why not use make ? Commented Mar 16, 2014 at 9:41
  • This was just a excerpt of the code; other things are done with code.o. However, you're right, a Makefile is probably the only correct method here. Commented Mar 16, 2014 at 17:41

1 Answer 1

6

The -D should be glued to the name (ie -DFOO not -D FOO)

     gcc -c -std=c99 -Wall "-D$OPTIMIZE" -O2 code.c

and you forgot to pass -Wall to gcc. It is almost always useful.

BTW, you might consider (even for a single file) using make with two phony targets: the default one (e.g. plain), and an optimized one.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! For some reason the idea of a Makefile never occurred to me...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.