Compiling C code in Linux terminal - c

I am using Linux mint 16. I had a code that I change it a bit.
I use two following commands in terminal in order to run the code. The problem is that it does not give me any error but the changes are not applied, which means it runs the previous version of code.
gcc -std=c99 -c Code.c -o Code.o
./Code

gcc -std=c99 -c Code.c -o Code.o will put the compiled object file in Code.o, not ./Code as you expect it to be..
Also, -c tells do not run the linker. So effectively you end up with an object file which cannot be run.
gcc -std=c99 Code.c -o Code will produce what you need.
For a complete list of gcc flags either use man gcc or see http://linux.die.net/man/1/gcc

Try
gcc -std=c99 -c Code.c -o Code
./Code

Related

How to run a C program which uses string.h in Ubuntu

My C program uses string.h.. Initially I was not able to compile it. But then I used
$ gcc filename.c -E
Then it complied but I am not able to run it with both
./a.out
./filename
The -E option to gcc invokes only the preprocessor. If you want to compile you need to do this:
gcc -g -Wall -Wextra -o filename filename.c
The -o option specifies the name of the executable to create, the -W options enable the common compiler warnings, and -g includes debugging symbols so you can use tools such as gdb to step through the code line by line.

how to run a C program with a test file?

I compile the program with gcc
gcc -Wall -ansi -pedantic -o program.c
what is the command to run it with my test file?
./program <test1.txt> ???
Your gcc is wrong and at present errors on building.
$ gcc -Wall -ansi -pedantic -o program.c
Will show this error:
gcc: fatal error: no input files
compilation terminated.
Try:
gcc -Wall -ansi -pedantic -o program program.c
This will compile your program into an exec called program, which you can then run by;
./program
Then maybe try running you're command passing in the test1.txt
./program test1.txt
Please see comments in your OP as people have already offered good advise.

Use of -g and -o options in gcc command in c programming

Suppose there are 2 c program named abc.c and xyz.c . Now we want to work with the 2 executables at a time. So we change the name of the ./a.out using
gcc -g abc.c -o abc
gcc -g xyz.c -o xyz
Even gcc -o abc abc.c works.
What does the -g and -o in the above commands specify or describe?
What is the significance of -g and -o in the command for renaming ./a.out file.
Thanks in advance.
-g means to leave debugging information in the output file, it's unrelated to renaming.
-o means to put the result in the specified file instead of the default filename (abc.o for object files, a.out for linked executable files).
From https://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html:
-g
Produce debugging information in the operating system's native format (stabs, COFF, XCOFF, or DWARF). GDB can work with this debugging information.
-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.
-g starts becoming useful once you use debuggers such as gdb and lldb. When you attach to a running program and advancing one line at a time printing/altering the state as it changes.
if we specify -g option while compiling, debugging symbols will be available in the output file which will be useful when you try to debug using GDB.
If we won't specify -o option, the output will be placed in default a.out file. So if we run
gcc a.c - output will be in a.out
gcc b.c - output is a.out which is replacing old a.out file
If you want the output not to be a.out file, you can give -o option while compiling
gcc abc.c -o a
-o and -g options are not related.

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.

gcc makefile won't compile

My final executable (this is in unix though) will be proj07.
proj07: /user/cse320/Projects/project07.driver.o proj07.support.o
gcc -Wall /user/cse320/Projects/project07.driver.o proj07.support.o
proj07.support.o: proj07.support.c
gcc -c proj07.support.c
This creates proj07.support.o but no proj07 exists after compilation. I don't get an error so my mistake must be simple but I can't seem to figure it out.
Here's the output:
gcc -c proj07.support.c
gcc -Wall /user/cse320/Projects/project07.driver.o proj07.support.o
Also I am to use a static driver to test my file which is why the path is like that
You probably do have an a.out executable. Add -o $# to your first gcc occurrence and you should be fine.

Resources