title: How to solve "undefined reference to function" error?
body: <p>I have to build a library for a school project and use that library in a small aplication. Now, I have made the XPM_lib.h and XPM_lib.c files and also my test.c file. But when I try to compile my test.c file I get an "undefined reference to initXPM_image" error.</p>
  
  <p>My files are (XPM_lib.h):</p>
  
  <pre><code>#ifndef LIBRARYXPM_H_INCLUDED
  #define LIBRARYXPM_H_INCLUDED
  
  
  
  #include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;malloc.h&gt;
  #define HEADER "/*XPM*/"
  #define STRING_BEGIN "static char *egc[] = {\n\n /*width ,  height , ncolors ,        charsperpixel */ \n "
  #define STRING_END "\n }"
  #define STRING_SEPARATOR ","
  #define STRING_COLORS "/* colors #RRGGBB */"
  #define STRING_PIXELS "/* pixels */"
  
  
  struct Color{
  
          unsigned char r,g,b;
          char *color_char;
          char key[2];
  };   
  
  
  struct XPM_image {
  
          unsigned int width;
          unsigned int height;
          unsigned int no_colors; // number of colors used
  
          unsigned char char_per_pixel; // number of characters to describe a pixel
  
          struct Color *color_table; // table containing the colors
          unsigned int **image_data; // contains indices from color_table
  
  } ;
  
  
  // functions first problem , lab 1
  
  extern void initXPM_image (struct XPM_image *image, unsigned int width, unsigned int      height, unsigned char char_per_pixel, unsigned int no_colors );
  
  extern void freeXPM_image (struct XPM_image *image);
  
  extern void setXPMpixel (struct XPM_image *image, unsigned int poz_x, unsigned int poz_y, unsigned int index );
  
  extern void setXPMcolor (struct XPM_image *image, unsigned int index, unsigned char r, unsigned char g, unsigned char b, char *pattern);
  
  extern void writeToFile (struct XPM_image *image , char *pathname);
  
  
  
  #endif // LIBRARYXPM_H_INCLUDED
  </code></pre>
  
  <p>and (XPM_lib.c) :</p>
  
  <pre><code>    #include "XPM_lib.h"
  
  // initializeaza spatiul pentru imagine si tabela de culori si seteaza proprietatiile de baza
  void initXPM_image (struct XPM_image *image, unsigned int width, unsigned int height, unsigned char char_per_pixel, unsigned int no_colors )
  {
      image-&gt;width = width;
      image-&gt;height = height;
      image-&gt;char_per_pixel = char_per_pixel;
      image-&gt;no_colors = no_colors;
  
      if ( ( image-&gt;color_table =(struct Color*) malloc ( image-&gt;no_colors * sizeof(struct Color)) == NULL ))
          {
              printf("Eroare in initXPM_image la alocarea color_table");
              exit (0);
  
          }
  
  
  
      if ( ( image-&gt;image_data = (unsigned int **) malloc (height * sizeof(unsigned int *))) == NULL )
      {
  
          printf("Eroare in initXPM_image la alocarea image_data (height)");
          exit (0);
  
      }
  
      int i;
      for (  i = 0 ; i &lt; height ; i++ )
      {
          if ( (image-&gt;image_data[i] = (unsigned int *) malloc (width * sizeof (unsigned int ))) == NULL )
          {
              printf( "Eroare in initXPM_image la alocarea image_data (width)");
              exit (0);
          }
  
      }
  
  
  
  }
  
  
  void setXPMcolor (struct XPM_image *image, unsigned int index, unsigned char r, unsigned char g, unsigned char b, char *pattern)
  {
  
      image-&gt;color_table[index].r = r;
      image-&gt;color_table[index].g = g;
      image-&gt;color_table[index].b = b;
  
      image-&gt;color_table[index].color_char = pattern;
  
  }
  
  void setXPMpixel (struct XPM_image *image, unsigned int poz_x, unsigned int poz_y, unsigned int index )
  {
  
      image-&gt;image_data[poz_x][poz_y] = index; // pun index-ul culorii din tabela de culori din imagine
  
  }
  
  void writeToFile (struct XPM_image *image , char *pathname)
  {
  
      FILE *f;
  
      if (( f = fopen(pathname,"wt")) == NULL)
      {
          printf("Eroare la deschiderea fisierului!");
          exit(0);
  
      }
  
      fprintf( f ,"%s\n%s",  HEADER , STRING_BEGIN);
  
      fprintf( f , "\" %d %d %d %d\"", image-&gt;width, image-&gt;height , image-&gt;no_colors , image-&gt;char_per_pixel);
  
      // colors
      fprintf( f, "%s \n" , STRING_COLORS);
      int i;
      for ( i = 0 ; i &lt; image-&gt;no_colors ; i++ )
          {
              printf("\"%s c #%.02X%.02X%.02X\",\n" , image-&gt;color_table[i].color_char , image-&gt;color_table[i].r , image-&gt;color_table[i].g , image-&gt;color_table[i].b);
          }
  
  
  }
  </code></pre>
  
  <p>The test.c file is :</p>
  
  <pre><code>#include "lib/XPM_lib.h"
  
  
  #define WIDTH 50
  #define HEIGHT 50
  #define COLORS 50
  
  
  int main ()
  {
  
      char first_char = 'a';
      struct XPM_image *image;
  
      if ((image = (struct XPM_image*) malloc (sizeof(struct XPM_image))) == NULL )
      {
          printf("Eroare la alocarea XPM_image\n");
          exit(0);
  
      }
  
      initXPM_image (image, WIDTH, HEIGHT,1, COLORS);
  
      int i,j;
      for (  i = 0 ; i &lt; COLORS; i++ )
      {
          setXPMcolor(image, i, 255*i/(COLORS-1),0,0,&amp;first_char);
          first_char++;
      }
  
      for ( i = 0 ; i &lt; WIDTH ; i++ )
          for ( j = 0 ; j &lt; HEIGHT ; j++ )
              setXPMpixel(image,i,j,i);
  
      writeToFile(image,"imagine.xpm");
      return 0;
  
  }
  </code></pre>
  
  <p>Now, I can't use and IDE to set up a project for this so I have to compile it manually using gcc. To compile, I did: </p>
  
  <pre><code>gcc -c .\lib\XPM_lib.c
  gcc -c test.c
  gcc -o program .\lib\XPM_lib.o test.o 
  </code></pre>
  
  <p>( I forgot to mention !) My directory structure :</p>
  
  <pre><code>.\lib\XPM_lib.h 
  .\lib\XPM_lib.c
  .\test.c
  </code></pre>