New to linux

I am starting linux. i am using Ubuntu, and when i compile m c program with
gcc Hello.c it builds to a.out. what is this? an object file, how do i turn it to an executable file
Most compilers create a.out by default. Usually one uses "-o output_name" to name the actual program. If your program compiled and linked ok, you should be able to run a.out by typing "a.out" and then hitting ENTER.

Using makefiles will greatly aid your programming. A basic makefile for Hello is:

1
2
3
4
5
6
7
8
9

Hello: Hello.o
    gcc -c Hello.c

Hello.o: Hello.c
    gcc Hello.c -o Hello


i try to run a.out through the terminal, but it says there is no such file, but i am sure i am in the correct directory. in ubuntu i just have to drag the folder name to the terminal
Try ./a.out.
i try to run a.out through the terminal, but it says there is no such file, but i am sure i am in the correct directory.

In Linux the current directory (named "./") isn't usually added to the PATH (for security reasons).
The PATH contains the directories where executables are searched for, separated by colon ":".

To see the contents of the PATH variable, do:
echo $PATH


Anyway, you will end up doing what kooth advised -- specify that you want to run the program that's in the current directory.
./program_name

Last edited on
A basic makefile for Hello is:
make is the program that reads Makefile and executes instructions in it. GNU make has several implicit rules, so all need not be written. Rules have overall form of:
1
2
target : dependencies
  commands

kooth had a slight typo on his example.

The first rule says that in order to create file named 'Hello', one has to have a file named 'Hello.o', and that the command to run is 'gcc -c Hello.c'. The -c flag to gcc says not to run linker. Therefore, 'gcc -c Hello.c' will produce an object file named 'Hello.o'.

The second rule supposedly tells how to convert Hello.c into Hello.o. It's proper command was on line 3.

'gcc Hello.c -o Hello' makes gcc both compile Hello.c into object file and then link that object file into binary named 'Hello'. That is how you would call it from command line. Thus, try 2:
1
2
3
4
5
6
7
8
Hello: Hello.o
    gcc -o Hello Hello.o

Hello.o: Hello.c
    gcc -c Hello.c

clean:
    rm *.o

The nice thing about make is that when you have multiple source files, it will recompile only the ones that have changed since previous compilation.

Recommended commands:
man make
info make

(Not sure if Ubuntu has the latter.)
Just type gcc -o Hello Hello.c into the terminal

>
(Not sure if Ubuntu has the latter.)

Yes it does
Topic archived. No new replies allowed.