Hello everyone.

I have an error in my makefile. When i execute it on the terminal in linux, it gives me the following errors

mkab@mkab-ThinkPad-SL400:~/Desktop/TP 5/src$ make test
gcc -c prog2.c
gcc *.o -o test
prog2.o: In function `tester_supprimer':
prog2.c:(.text+0x1c): undefined reference to `ajouter'
prog2.c:(.text+0x32): undefined reference to `ajouter'
prog2.c:(.text+0x48): undefined reference to `ajouter'
prog2.c:(.text+0x5e): undefined reference to `ajouter'
prog2.c:(.text+0x6c): undefined reference to `afficher'
prog2.c:(.text+0x77): undefined reference to `supprimer'
prog2.c:(.text+0x85): undefined reference to `afficher'
collect2: ld returned 1 exit status
make: *** [test] Error 1

This is my makefile

liste.o: liste.h liste.c
	gcc -c liste.c

outils_liste.o: liste.h outils_liste.h outils_liste.c
	gcc -c outils_liste.c

prog0.o: liste.h constantes.h prog0.h prog0.c
	gcc -c prog0.c

prog1.o: liste.h prog1.h constantes.h prog1.c
	gcc -c prog1.c

prog2.o: liste.h outils_liste.h constantes.h prog2.c
	gcc -c prog2.c

test: prog2.o
	gcc *.o -o test

clean:
	rm -v *.o

It's like it cannot detect the functions in the defined in the ".h" files even though i listed them in the dependencies of prog2.o.

Thanks in advance

Recommended Answers

All 11 Replies

But the compiler is telling you that you are getting errors in prog2.c? At a quick guess you haven't defined some variables. Unless there is something Im missing.

the variables and functions are defined in the .h files

This is prog2.c

#include <stdlib.h>
#include <stdio.h>

#include "liste.h"
#include "outils_liste.h"
#include "constantes.h"


void tester_supprimer () {
	liste *l ;
	l = NULL ;
	l = ajouter (l, UN) ;
	l = ajouter (l, DEUX) ;
	l = ajouter (l, TROIS) ;
	l = ajouter (l, QUATRE) ;
	afficher (l) ;
	l = supprimer (l) ;
	afficher (l) ;
}

int main () {
	tester_supprimer () ;
	return 0 ;
}

I included "liste.h" and "outils_liste.h" "constantes.h" and in each of the .h files I defined the functions "ajouter", supprimer" and "afficher".

I would say the entries in your Makefile are in the wrong order.

This line

test: prog2.o
	gcc *.o -o test

requires that

liste.c
outils_liste.c
constantes.c

are built object files. They aren't built when you call them here....unless your make utility reads the entries in the opposite order.

I don't really understand... prog2.o doesn't depend on the object files of

liste.c
outils_liste.c
constantes.c

but just on the .h files.

I change it to

test: prog2.o
	gcc prog2.o -o test

But I still get the same error.

Then why is it in the list of includes

#include "liste.h"
#include "outils_liste.h"
#include "constantes.h"

Take your make file and reverse every entry except the ones in clean....also

prog2.o: liste.o outils_liste.o constantes.o prog2.c
gcc -c prog2.c

header files are for the preprocessor and source/object files are for the makefile.

So does that mean I can't used header files as dependencies of objects files in makefile?
Actually I don't have a constantes.c because I don't really need one

constantes.h

#ifndef _CONSTANTES__H_
#define _CONSTANTES__H_

#define UN "one"
#define DEUX "two"
#define TROIS "three"
#define QUATRE "four"

#endif /* _CONSTANTES__H_ */

So i tried this

prog2.o: prog2.c constantes.h outils_liste.o liste.o 
	gcc -c prog2.c

test: prog2.o
	gcc prog2.o -o test

and I still have the same errors.
I included those header files in prog2 because it contains defined functions and structures
These are the header files if it may help

liste.h

#ifndef _LISTE__H_
#define _LISTE__H_

/* Structure liste */
struct liste {
  char *nom ;
  struct liste * suivant ;
} ;

typedef struct liste liste ;

/* Affiche le contenu de la liste à l'écran */
void afficher (liste *l) ;

/* Ajoute un élément en tête de la liste */
liste *ajouter (liste *l, char *nom) ;

/* Recherche la position du nom dans la liste */
int rechercher (liste *l, char *nom) ;

#endif /* _LISTE__H_ */

outils_liste.h

#ifndef _LISTE__OUTILS__H_
#define _LISTE__OUTILS__H_

#include "liste.h"

/* Supprime un élément de la liste */
liste *supprimer (liste *l) ;

#endif /* _LISTE__OUTILS__H_ */

The only reason to put header files in makefile is to check to see if they exist. Its way of ensuring that you have all the files required and if not producing an error indicating which files are required. The #include "filename.h" is preprocessed therefore writing the header directly into the source code.

The only reason to put header files in makefile is to check to see if they exist.

No, the reason to include header files in a make file is so that if the header file is updated, but the source file that includes it has not been updated, that source file is recompiled anyhow.

commented: Thanks for pinting that out. +5

Ok I get it. Actually I'll need the object files to be built then using them as dependencies which brings me to the main problem of my makefile.

#makefile

liste.o: liste.c liste.h 
	gcc -c liste.c

outils_liste.o: outils_liste.c outils_liste.h liste.h
	gcc -c outils_liste.c

prog0.o: prog0.c prog0.h constantes.h liste.h
	gcc -c prog0.c

prog1.o: prog1.c constantes.h prog1.h liste.h
	gcc -c prog1.c

prog2.o: liste.h outils_liste.h constantes.h
	gcc -c prog2.c

test: prog2.o liste.o outils_liste.o prog1.o prog0.o
	gcc *.o -o test

clean:
	rm -v *.o

I have these errors

mkab@mkab-ThinkPad-SL400:~/Desktop/TP 5/src$ make test
gcc -c prog2.c
gcc -c liste.c
gcc -c outils_liste.c
gcc -c prog1.c
gcc -c prog0.c
gcc *.o -o test
prog1.o: In function `main':
prog1.c:(.text+0x112): multiple definition of `main'
prog0.o:prog0.c:(.text+0x72): first defined here
prog2.o: In function `main':
prog2.c:(.text+0x8b): multiple definition of `main'
prog0.o:prog0.c:(.text+0x72): first defined here
collect2: ld returned 1 exit status
make: *** [test] Error 1

Multiple definition of main. There are main functions in all the .c files. How can i make this work?

No, the reason to include header files in a make file is so that if the header file is updated, but the source file that includes it has not been updated, that source file is recompiled anyhow.

Good point.

You can only have one main function in an executable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.