[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] Skip to the content of the web site.

Using gcc

Contents Previous Topic Next Topic

Given a C file, we would like to convert it into an executable file which we can run. This is done through the compiler gcc. This takes the Ccc code and creates an executable file which itself is can be run as a command in the shell.

To use gcc in Unix, you pass it one argument which is the name of the C file.

{ecelinux:1} gcc filename.c

This will generate an executable file called a.out (from assembler output). This is an executable file which is created in the current working directory. There is one small problem, however. By default, shells do not check the current directory when it is looking for executable files. Thus, you must specify the full path, and you may do so by giving a path relative to the working directory:

{ecelinux:2} ./a.out
{ecelinux:3}

Recall that . is the name of the current directory, and therefore, you are stating: run the file a.out which is in the current directory.

This may appear strange if you run executables in Windows, however, the philosophy of Unix is that all binary files are stored in appropriate bin directories as specified by the PATH environment variable:

{ecelinux:3} echo $PATH
/usr/local/bin:/usr/ccs/bin:/usr/bin:/usr/ucb:/usr/openwin/bin:/usr/dt
/bin:/software/.admin/bins/bin:/opt/gcc/bin:/usr/sfw/bin:/bin:/usr/sbi
n:/sbin:/packages/share/bin:/packages/xray/bin:/usr/local/bin:/home/ec
e250/bin:/home/ece250/isg-bin/testing:/home/ece250/isg-bin
{ecelinux:4}

Extending this list, we get:

/usr/local/bin
/usr/ccs/bin
/usr/bin
/usr/ucb
/usr/openwin/bin
/usr/dt/bin
/software/.admin/bins/bin
/opt/gcc/bin
/usr/sfw/bin
/bin
/usr/sbin
/sbin
/packages/share/bin
/packages/xray/bin
/usr/local/bin

Thus, if you do not specify a path, Unix will look in all of these directories (in order) for a file called a.out, and it will run the first one it finds. If it does not find one, it will issue:

{ecelinux:4} a.out
a.out: Command not found.
{ecelinux:5}

Note: in some terminal programs, it may, by default, look in the local directory, however, it doesn't hurt to specify the path with ./a.out.

Terminating Infinite Loops

If you start running a.out and it appears to be in an infinite loop, you can try to kill it by pressing Ctrl-C (a few times, if necessary). If this does not work, do the following: Press Ctrl-Z (which sends the process into the background) and then type kill %1. (This assumes that you are not running any other jobs in the background, which, if you are, you will be aware of what you are doing.) For more advanced information about process management, see this topic.

Other Options to gcc

You can specify the name of the executable (if you don't like the name a.out) by using the -o option:

{ecelinux:1} gcc -o myname filename.c
{ecelinux:2} ./myname
{ecelinux:3}

You can request that the compiler attempt to optimize your program by using the -O option:

{ecelinux:1} gcc -O filename.c
{ecelinux:2} ./a.out
{ecelinux:3}

Further Reading

You can read about gcc and other GNU compilers at Wikipedia.

Contents Previous Topic Next Topic

Copyright ©2005-2012 by Douglas Wilhelm Harder. All rights reserved.

[an error occurred while processing this directive]