scottybobby
scottybobby

Reputation: 315

Can't name executable with specified optimization

I have to turn off optimizations while compiling c code I wrote while using the gcc compiler on a linux. I have found that I can compile the code but I can only get the code to compile without the executable name specified (default to a.out).

So this works:

gcc -O0 Problem04b.c

But my problem is that I have to submit this assignment and I can't submit an executable called a.out because my instructor needs to know which problem it is. I realize I can probably just run

cp a.out Problem04b

then

rm a.out

but I want to know if there is a way I can just compile the code directly into the executable Problem04b. I've tried to run the command like this:

gcc -O0 Problem04b Problem04b.c

but I'm having no luck.

Thanks for your help.

Upvotes: 0

Views: 85

Answers (3)

Actually, you also want to get warnings (that won't change the produced executable, but it is very helpful to you), so compile with

 gcc -O0 -Wall -Wextra Problem04b.c  -o Problem04b

The -Wall option asks for nearly all warnings, the -Wextra option asks for even more of them.

To run your thing (the ./ is useful because of possible PATH issues):

 ./Problem04b

Notice that -O0 is optional (since it is the default), you could remove it.

 gcc -Wall -Wextra Problem04b.c  -o Problem04b

If you want real optimization, e.g. for benchmarking, use e.g. -O1 or -O2 or -O3

You probably want to compile with debug information, then

 gcc -g -Wall -Wextra Problem04b.c  -o Problem04b

and of course you need to learn how to use the GDB debugger. So read some tutorial about that, then type

 gdb ./Problem04b

You'll get a (gdb) prompt. Try help at that time.

You probably want to read the chapter about invoking GCC of the GCC documentation.

Upvotes: 1

Sourav Ghosh
Sourav Ghosh

Reputation: 134356

To specify the output file, you need to use the -o <filename> option with gcc.

Note : Please mind the lower case here

In your case, it should be

gcc -O0 -o Problem04b Problem04b.c

For reference: From gcc manual

-o file

Place output in file file. This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code.

Upvotes: 4

eduffy
eduffy

Reputation: 40232

It's the -o flag:

 gcc -O0 -o Problem04b Problem04b.c

Upvotes: 5

Related Questions