2

I'm trying to do a simple "Oriented object" programm in C and I come from Java. I did some research and reading online and I came with this code :
Declaring my base class

#ifndef FIGURE_H
#define FIGURE_H
struct figure{
    int(*m_draw) (struct figure* oref);
    int(*m_move) (struct figure* oref, int x, int y);
    int(*m_resize) (struct figure* oref, int x, int y);
    int(*m_rotate) (struct figure* oref, int angle);
    int(*m_erase) (struct figure* oref);
    int(*m_show) (struct figure* oref);

};
extern void init_figure(struct figure* oref);

extern int draw (struct figure* oref);
//... so on

#endif //FIGURE

So this is the header file of my main structure called Figure I'm not really sure about the last part where I define all method extern I think it's wrong. Then I want to do a figure_x so I a figure_x.c I did the following code to redefine the methods
figure_x.c

#include <stdio.h>
#include "figure.h"

int f_draw(struct  figure* oref){
    printf("Hey I'm an awesome figure (%p), I bet you never have seen something like that b4", oref);
    //idk what to do but when I tried with void I had error and with int it's fine lol help me
    return -1;
}

int f_move(int x, int y, struct  figure* oref){
    return -1;
}

//so on for each method defined in the figure structure
void init_figure (struct figure* oref)
{
    oref->m_draw = f_draw;
    oref->m_move = f_move;
    oref->m_resize = f_resize;
    oref->m_rotate = f_rotate;
    oref->m_erase  = f_erase;
    oref->m_show   = f_show;
}

And finally I create a main.c where I chose to use a static declaration and usage of my objet like this:
main.c

#include <stdio.h>
#include "figure.h"
int main() {
    //create a fig
    struct figure fig;
    //init the fig
    init_figure(&fig);
    fig.m_draw(&fig);
    fig.m_move(&fig, 1, 2);
    fig.m_resize(&fig, 3, 4);
    fig.m_rotate(&fig, 5);
    fig.m_erase(&fig);
    fig.m_show(&fig);
    return 0;
}

Now I get the following error :

C:\Users\xyz\AppData\Local\Temp\ccSxcgqa.o:main.c:(.text+0x15): undefined reference to `init_figure'
collect2.exe: error: ld returned 1 exit status

My question is how can be init_figure undefined even if I use the key word extern

J.erome
  • 688
  • 7
  • 26
  • please provide your build command – yano Nov 04 '19 at 20:36
  • I'm using `Clion` from `Jetbrains` so I just type `gcc main.c` in the terminal of `Clion` am I doing something wrong ? – J.erome Nov 04 '19 at 20:37
  • https://stackoverflow.com/a/12574400/1505939 – M.M Nov 04 '19 at 20:38
  • so if I understand I also need to `#define figure_x.c` in `main.c` – J.erome Nov 04 '19 at 20:42
  • 1
    no, try "gcc main.c figure_x.c". The error you get is the linker saying "I don't know where the function definition for `init_figure` is". That's in figure_x.c, so include that in your build line. I can never remember if the order of the files here matters, I don't think so. Order matters for linking object files. – yano Nov 04 '19 at 20:44
  • 1
    @yano it's working the error isn't here anymore – J.erome Nov 04 '19 at 20:46

0 Answers0