How to create an executable in Ubuntu of a c program - c

I would like to create an executable of my two mycode.c and my main.c, how can I create an executable? i did
gcc mycode.c main.c
and it generates a a.out, but when i click it it would not run.. (i am new to this so please bear with me)
Thank you

Try this
gcc mycode.c main.c -o myprogram
Then run ./myprogram
If you double click it you probably won't see anything, you should instead try running it from the command line, where you compiled it from in the first place.

Your a.out might not be executable yet.
do:
$> chmod 755 a.out
or
$> chmod a+x a.out
then try running it:
$> ./a.out

Related

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

running my code using terminal Ubuntu

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

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.

edit bash command gcc to compile and execute in one line

I want to know if this is possible:
if you put gcc filename.c you will compile filename.c in a.out file
and if you put ./a.out you will execute the file.
The composite version it's
gcc filename.c && ./a.out
I want edit this command in bash_profile to do this in one short command line.
gcc filename.c do this gcc filename.c && ./a.out
You can declare a function:
gccrun () {
gcc "$1" && ./a.out
}
Be careful, though: what if someone uses full path to the C file as a parameter?
Makefiles are usually used for this kind of stuff.
a.out: file.c
gcc $<
run: a.out
./a.out
make run would run a.out recompiling it if its source has changed since the last compilation or doesn't exist.

The output of GCC

I have GCC running on my Ubuntu operating system. I wrote a small program in C and tried compiling it. Its output was an a.out file like it would do on Windows. How can I make it put out a Linux executable?
a.out is the executable (assuming you've done full compilation rather than just generation of object files but that's the most likely case). To run it, use (from a shell):
./a.out
If you want to give it a different name, simply rename it, or better:
gcc -o actualname myprog.c
to get an executable called actualname which is then run (of course) with:
./actualname
See the following transcript:
pax> cat testprog.c
#include <stdio.h>
int main (void) { printf("Hi.\n\n"); return 0; }
pax> gcc testprog.c ; ./a.out
Hi.
pax> gcc -o xyzzy testprog.c ; ./xyzzy
Hi.
Suppose your C file is f.c.
gcc f.c gives the a.out executable, and you can run it in a terminal as ./a.out.
gcc f.c -o myprog gives myprog as the executable, and you can run it in a terminal as ./myprog.
It is a Linux executable. a.out files (actual a.out format, not files named a.out by default) cannot be executed on Windows.
In both cases, most likely you get a standard executable usable in the local system, but named a.out. On Linux it's an ELF file.

Resources