running my code using terminal Ubuntu - c

I've watched tons of videos about how to use sublime text 3 I do what they say but it doesn't work. When i type "subl test.c" in terminal it opens up a a file called test.c in sublime text 3 when i use the command gcc -c test.c everything is fine too, but when I try to run the code using ./test it says bash: ./test: No such file or directory

Bash says that there is no such file or directory because you haven't created a file called 'test'. You should specify an output filename, i.e. you should type gcc test.c -o your_out_filename. Then you may run your program using ./your_out_filename. Without -o flag gcc will create a a.out by default, so your out_filename will be a.out.

You have to use the following command to create a file called test:
gcc test.c -o test
If you don't use the -o option (gcc test.c) your created file will be a.out.
The option -c of gcc only compiles your file and doesn't link it to a program which you can run. The result of the -c option is only an object file called test.o.
Therefore the easiest way is the one I have mentionend above (-o option).

You have to run:
gcc -o output test.c
output is the file you have to do ./output in terminal for it to execute

Related

How to run C program without having to run 'gcc filename.c' and 'a.exe '

When i type the command gcc filename.c a new file 'a.exe' is created, then i have to run a.exe to get my program to run.
Is there a way just to type one command to run my program or can you run a C program without having a new .exe file being created?
I use gcc version gcc (MinGW.org GCC-6.3.0-1) 6.3.0
(Complete beginner with C)
You can't run the .c file directly - you have to compile it into an executable (using gcc or whatever compiler). However, you don't have to compile it every time you want to run it - you only have to compile it after first creating it and after you make any changes. So you can run gcc once, then run a.exe multiple times.
You can name the executable something other than a.exe if you wish using the -o option:
gcc -o prog filename.c
./prog
You could make a .bat file that does everything for you. For example:
build.bat
gcc %1.c -o %1.exe
%1.exe
This will build and run the file for you:
build hello
will compile and run hello.exe for you.

cannot execute binary files on MAC terminal

I've used makefile to generate file.
gcc -c hello.c -o hello
and fixed the permission problem through:
chmod a+x ./hello
However, when I want to execute "hello" file.
./hello
the system told me that "cannot execute binary file"
Can someone help me? I am looking forward your reply badly.
The -c argument to gcc produces an object file which you later on must link in order to produce an executable. You can not execute the object file you produced.
Instead, to compile and link at the same time, suitable when you only have 1 .c file, do
gcc hello.c -o hello
Or if you want to break it down to separate compilation and linking steps, do
gcc -c hello.c -o hello.o
gcc hello.o -o hello
Check whether the GCC compiler is installed in your system correctly or not.
gcc -v
Compile your file:
gcc filename.cpp -o any-name
Running your program:
./any-name
As an alternative to compiling and linking at the same time you can use make:
make hello
Note: without the .c extension.
The terminal output should be:
cc hello.c -o hello

When I compile .c file it always creates .exe file with name 'a'

I use GCC compiler to compile and run C program on CMD.
When I compile C program using command gcc hello.c it creates an executable file file with name a.exe but when I use IDE it uses the name same as .c file as hello.exe.
Is it possible to create .exe file name same as .c file on CMD?
Just tell GCC the output name you want.
gcc hello.c -o hello
You have to specify the name of the executable
gcc -c hello.c // compiling
gcc -o hello hello.o // linking
^------------------------- name of the executable
you can do the compiling and the linking in one command
gcc -o hello hello.c
^------------------------- name of the executable
You need to understand the syntax of output. Following is the description of the command:
$ gcc [options] [source files] [object files] -o output file
In your case you should execute the commands as following:
$ gcc myfile.c -o myfile
$ ./myfile
For more descriptions and more optimized ways refer to the following url:
http://www.rapidtables.com/code/linux/gcc/gcc-o.htm

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.

ELF File generation commands and options

I was reading about ELF files on the net and am stuck in understanding a standard command to generate an ELF file.
Till now I have been running my code using > gcc test.c -o test.o .Thats it!!
One article says:
gcc -c test.c // will generate ELF file test.o
Now -o option is to tell the compiler to create an executable ( which is not ELF)
Another article says:
gcc -c test.c -o test.o // will generate ELF test.o -> here's where I am confused.
-o should always generate Executable.
The option -c tells GCC to generate an object file. This object file is only the compiled code from the source file test.c, not a complete program. To generate a complete program you need to link the object file. Or not use the -c option.
The -o option tells GCC what to name the output file, no matter what kind of output file it is.
So, to generate an executable file from a single source file, the simplest command is
$ gcc test.c
The above command will create an executable named a.out in the current directory. To name the output file something else you use the -o option:
$ gcc test.c -o myprogram
The above commands names the executable program myprogram.
To use the intermediate step with object files you use the -c option, and then use a separate step to link the program, like
$ gcc -c test.c
$ gcc test.o -o myprogram
The above two commands is the same as the single command gcc test.c -o myprogram.

Resources