Modular program in C does not seems to work

88 views Asked by At

I am trying to make a program import a function to another however after following the steps I saw in a tutorial I can't make it work here's the main program:

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

#include "projet.h"

In this program I call a function named Mise who is defined in another program:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "projet.h"

int Mise(int ChoixChiffre[37], int ChoixCouleur, int ChoixDouzaine[3], int ChoixParite, int ChoixPlage, int MiseChiffre[37], int MiseCouleur, int MiseDouzaine[3], int MiseParite, int MisePlage, int ArgentJoueur) //Main function

And in the .h file I wrote this:

#ifndef PROJET_H
#define PROJET_H

int Mise(int ChoixChiffre[37], int ChoixCouleur, int ChoixDouzaine[3],
         int ChoixParite, int ChoixPlage, int MiseChiffre[37],
         int MiseCouleur, int MiseDouzaine[3], int MiseParite,
         int MisePlage, int ArgentJoueur);

#endif

When I compile the program I have an error with undefined reference when I call Mise() in a function

I tried removing the call to Mise() and the program works perfectly fine, changed the .h program a little and it changed nothing.

the Makefile looks like this:

tpimage: tpimage.o libisentlib.a
    gcc -Wall tpimage.o -o tpimage libisentlib.a -lm -lglut -lGL -lX11
    ./tpimage

The error is the following:

gcc -Wall tpimage.o -o tpimage libisentlib.a -lm -lglut -lGL -lX11
/usr/bin/ld : tpimage.o : dans la fonction « gestionEvenement » :
tpimage.c:(.text+0x1d8b) : référence indéfinie vers « Mise »
collect2: error: ld returned 1 exit status
make: *** [makefile:2 : tpimage] Erreur 1
1

There are 1 answers

0
chqrlie On

Declaring the function in a header file is not sufficient for the compiler to determine when to find the code for it. You must link the object file that has the function definition along with your own object file.

Given your Makefile, it seems the function Mise should be defined in a module added to the library libisentlib.a but the compiler cannot find it. You should investigate the contents of this archive with:

nm libisentlib.a

Make sure you include the header file in both your source file and the source file with the actual function definition to let the compiler check that the declaration is consistent with the definition and thus avoid undefined behavior.