-1

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
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 4
    How are you compiling and running the program? – Hamza Jadid Jun 22 '23 at 18:26
  • 1
    Sounds like you're not linking this together properly. This is where a good `Makefile` template, or CMake if you prefer, really helps. – tadman Jun 22 '23 at 18:28
  • 1
    What I see in main is not a call to the function, but a redefinition ... It would be more clear if you provide a more complete example. – Damien Jun 22 '23 at 18:33
  • my makefile look like this – furie_noire Jun 22 '23 at 18:42
  • tpimage: tpimage.o libisentlib.a gcc -Wall tpimage.o -o tpimage libisentlib.a -lm -lglut -lGL -lX11 ./tpimage – furie_noire Jun 22 '23 at 18:42
  • 1
    You have not shown "when i call `Mise()` in a function". And you haven't shown *which* `Mise` you remove and from where, to make it work. Please post a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), the shortest *complete* code that shows the problem. – Weather Vane Jun 22 '23 at 18:58
  • OT: The posted snippet shows a function with *eleven* arguments of type `int` or `int *`, please consider writing and passing one or more `struct`s instead. – Bob__ Jun 22 '23 at 19:29

1 Answers1

1

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.

chqrlie
  • 131,814
  • 10
  • 121
  • 189