What are compiler flags for?Why won't my code compile? - c

I wrote a code in C in Ubuntu which checks for balanced brackets in the input given.I compiled it using gcc compiler and I am getting the correct output.This is actually a part of an online course and they are asking me to use the compiler flag
gcc -pipe -O2 -std=c11 filename -lm
I don't think I understand what I am supposed to do so I tried compiling using this flag and my code is not compiling. My question is if my code compiles when I do
gcc filename.c
why isn't it compiling when I do
gcc -pipe -O2 -std=c11 filename -lm
The error message I am getting is :
cc1plus: warning: command line option ‘-std=c11’ is valid for C/ObjC but not for C++

The reason is the file ending. A capital C is interpreted as a C++ file. The solution is to just rename the file like this:
mv filename.C filename.c
My question is if my code compiles when I do gcc filename.C why isn't it compiling when I do gcc -pipe -O2 -std=c11 <filename> -lm
See above. But there are some situations where it would not solve everything. While C11 gives some extensions to previous versions, it's not 100% backwards compatible.

-std=c11 is a correct option
however you need at least gcc 4.7 or higher to have this option

By "<filename>" they mean to substitute in the name of the file you want to compile. Including the literal string "<filename>" will not work.

Related

how to switch from assembly code to c code in gdb

I am trying to debug my c program using
gcc -g -lm -lpthread -std=c99 -w terminalproject.c
and then
gdb a.out
but when I type
layout next, it shows me assembly code not c code.
How to switch to C code in layout?
EDIT: I am using Red Hat Linux 6, I tried to run it in Ubuntu, It is showing C code.
p.s my code has pthreads in it.
This command:
gcc -g -lm -lpthread -std=c99 -w terminalproject.c
is wrong. Use this instead:
gcc -g -std=c99 -pthread terminalproject.c -lm
You should never use -w (suppress all warnings) flag, unless you desire painful debugging sessions.
layout src says No source Available.
This likely means that you are using updated GCC, but ancient GDB. Try building recent GDB release from source -- it's usually not hard.

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.
It's the -o flag:
gcc -O0 -o Problem04b Problem04b.c
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.
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.

Using command line arguments when compile with gcc in codeblocks

So my question is simple.
I want to compile a C project file in gcc compiler 4.4.8.
In windows cmd window I can type :
gcc -std=c99 -O2 -DCONTEST -s -static -lm name.c -o name.exe
In Code::Blocks how can I change the compiler default arguments with the above?
I wanted to build a c file and I geting errors like
error: 'for' loop initial declarations are only allowed in C99 mode|
Ok to put your preferred arguments to gnu compiler go to :
Settings -> Debugger -> Compiler settings -> Other Options
and paste there your arguments!
Example:
-std=c99 -O2 -DCONTEST -s -static -lm

How to compile in -std=c99 in Geany

I am coding up some .c files using Geany, and I am interested in compiling using the c99 standard.
I see that I can set build commands and I assume this is where I need to be, but I can't seem to get the syntax correct. Here are commands that I've tried in the Compile tab, but none seem to work;
gcc -std=c99 -Wall -c "%f"
gcc -Wall -c -std=c99 "%f"
gcc -Wall -std=c99 -c "%f"
Any help is appreciated!

Alchemy C Code compilation

I am using alchemy to compile the C code.
This is the way I am compiling
gcc oggvorbis.c -O3 -Wall -swc
oggvorbis.swc -lvorbis
I am getting an error
llvm-gcc: oggvorbis.swc: No such file
or directory.
But the command works fine when i don't use any shared library.
Your command line invocation should probably read (untested)
gcc oggvorbis.c -O3 -Wall -swc -o oggvorbis.swc -lvorbis

Resources