Compiling C in KDE

John L Fjellstad john-ubuntu at fjellstad.org
Mon Jul 10 21:44:24 UTC 2006


"Dotan Cohen" <dotancohen at gmail.com> writes:

> Is there no C IDE in KDE that allows one to quickly write up a "Hello,
> World!" program, and then compile and run it?

unix is an IDE :)

At worst, you can just do this from the command line:
gcc file1.c file2.c file3.c

gcc will create the executable a.out in the current directory.

It's probably too late now, but I would recommend you learn make and
compiling from the command line (unix was built to give you all the
power of an IDE and more).

Here are some quick notes for next time.
gcc and g++ are the C and C++ compilers respectivelly.  They pretty much
take the same arguments.
So, at its most basic, you can call it like this (where ${CC} is the
compiler, ${EXECUTABLE} is the name of the executable you want to
create, and ${OBJECTFILES} are all the object files you want link
together to create the executable)

${CC} -o {EXECUTABLE} ${OBJECTFILES}

To create objectfiles, type

${CC} -c ${SOURCEFILE}

There are two variables that are used that you might want to know about:
CFLAGS and CXXFLAGS.  CFLAGS are flags that are used by gcc, and
CXXFLAGS are used by g++.

Once you understand that, you can create the Makefile.  Makefiles are
basically just rules you create so that the make program nows what to do
next.

There are two basic rules, variable assignments and targets. 
So, variable assignments, say you want a specific version of gcc, you
can assign it like this

CC = gcc-3.3

Targets are stuff you want done. So,

TARGET: DEPENDENCY
        RULES TO CREATE TARGET

(You can have multiple dependencies)
For instance,

myexec: myexec.o
        ${CC} {CFLAGS} -o myexec myexec.o

This means, myexec (TARGET) depends on myexec, it's created by calling
${CC} (whatever that variable is assigned to, by default gcc, if you
don't assign it), using CFLAGS.  Note that before the ${CC} there is a
<tab> (very important!).

You can also have targets that call shell programs. (${CC} is basically a
shell program).

For instance, most programs have a clean target:

clean:
        rm -f *.o *~ ${EXECUTABLE}

Putting it all together.  Create a Makefile that looks like this:

##########
CFLAGS = -Wall

myexec: myexec.o main.o
        ${CC} ${CFLAGS} -o myexec myexec.o main.o

myexec.o: myexec.c
        ${CC} ${CFLAGS} -c myexec.c

main.o: main.c
        ${CC} ${CFLAGS} -c main.c

clean: 
       rm -f *.o *~ myexec
#########

-Wall is a switch for gcc that gives you warnings if you have done
something suspect.
To compile, just type
make
and everything will compile for you. It will also only compile stuff
that get changed between compiles. So you change something in myexec.c,
but not in main.c, then only myexec.c gets compiled before linking. 
To run any particular rule, give
the name of the rule, ie
make clean

Once you have mastered the basic, you can do some pretty advanced stuff
(for instance, combine the object creation instead of having separate
rules for each object). Also, once you have mastered Makefiles, you
aren't stuck with a particular IDE for a particular language.

-- 
John L. Fjellstad
web: http://www.fjellstad.org/          Quis custodiet ipsos custodes





More information about the kubuntu-users mailing list