Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Makefile problem....

Status
Not open for further replies.

mrkan

Programmer
Oct 30, 2006
39
US
New to Linux/Ubuntu Edgy...

I am trying to write my first makefile for last 3 hours. So far couldn't make it. I copied couple simple makefile examples and non of them worked: here is one:
Tried one on the top and got message that myfile.o doesn't exists.

I have this: myfile.c myfilefunc.c and myfile.h
In the same folder I created makefile, assigned execute privilege for myself, made sure that every command start with tab.

I produced this:
Code:
myfile : myfile.o myfilefunc.o
	gcc -o myfile myfile.o myfilefunc.o

myfile.o : myfile.c
	gcc -c myfile.c

myfilefunc.o : myfilefunc.c
	gcc -c myfilefunc.c

so.... why this doesnt work? I tried to use variables, but non of them would get recognized... what I am doing wrong?
 
So, what does it do? and what do you get as output from the makefile?

The answer is "42"
 
this message: myfile.o does not exist.

Or if I tried to used variables like:

CC = gcc

Makefile would show error on this line. (I had this as a first line)...
 
Code:
myfile : myfile.o myfilefunc.o
    gcc -o myfile myfile.o myfilefunc.o

myfile.o : myfile.c
    gcc -c myfile.c

myfilefunc.o : myfilefunc.c
    gcc -c myfilefunc.c

Are the the command lines tabbed (with real) in the real file?
Does the project compile manually:
Code:
gcc -c myfile.c
gcc -c myfilefunc.c
gcc -o myfile myfile.o myfilefunc.o

Using a couple of thrown together files, and changing myfile rule to all and adding rule for clean just for my sanity... I get two .o files and a compaltation error (because I didn't use extern and I've proved the file is good I don't want to take the two minutes to compile a two file "hello world" program)... The makefile _SHOULD_ work.

For comparison:
Code:
[red]all:[/red] myfile.o myfilefunc.o
        gcc -o myfile myfile.o myfilefunc.o

myfile.o: myfile.c
        gcc -c myfile.c

myfilefunc.o: myfilefunc.c
        gcc -c myfilefunc.c

[red]clean:
        rm *.o[/red]

[plug=shameless]
[/plug]
 
Dont you have to first create .o files then to binary.
Maybe put the first section of code last; after the creation of the .o files

/*******************************

DragonForce
-Is it wrong to be strong
*******************************/
 
dragonforce, that isn't how make works. Order it appears in the file isn't the order it's executed for make files. The .o files are created first, that's why the .o files are the dependency list for the the main rule (all or myfile -- either works).

To use the make file (for mine):
[red]make all[/red]
Or for the parent's
[red]make myfile[/red]

The basic sytax is:
Code:
[green]rule[/green]: [green]dependency[/green] [green]dependency[/green] [green]...[/green]
[red]<TAB>[/red][green]command[/green]
The [green]rule[/green] is what you want to pass on the command line or list as a [green]depenency[/green] (files in a directory may also count as [green]dependencies[/green], and that is part of the magic of make).

If a dependency is not up to date, before the [green]command[/green] is executed it does the [green]dependency[/green].

The [red]<TAB>[/red] must be a real tab, and not spaces.

The [green]command[/green] for a [green]rule[/green] is executed if the rule is not already up to date, and the [green]dependcies[/green] are up to date. If the dependcies aren't meet, or not up to date, they are preformed before the original rule.

So with our make file:
Code:
all: myfile.o myfilefunc.o
        gcc -o myfile myfile.o myfilefunc.o

myfile.o: myfile.c
        gcc -c myfile.c

myfilefunc.o: myfilefunc.c
        gcc -c myfilefunc.c

clean:
        rm *.o *~

Running [green]make all[/green] will see if the files myfile.o and myfilefunc.o are up to date and if the rules are up to date... If we just changed the .c files, or haven't made once yet, then it does the rule [green]myfile.o[/green], which does: [red]gcc -c myfile.c[/red], and then [green]myfilefunc.o[/green], which does [red]gcc -c myfilefunc.c[/red]. Once those are up to date, it does the [green]all[/green]'s command [red]gcc -o myfile myfile.o myfilefunc.o[/red]. So the output should be:
Code:
$make all
gcc -c myfile.c
gcc -c myfilefunc.c
gcc -o myfile myfile.o myfilefunc.o

[plug=shameless]
[/plug]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top