2013-02-28 69 views
2

我必须建立一个学校项目的图书馆,并在一个小应用程序中使用该图书馆。现在,我已经制作了XPM_lib.h和XPM_lib.c文件以及我的test.c文件。但是当我尝试编译我的test.c文件时,我得到一个“未定义的initXPM_image引用”错误。如何解决“未定义的函数引用”错误?

我的文件(XPM_lib.h):

#ifndef LIBRARYXPM_H_INCLUDED 
#define LIBRARYXPM_H_INCLUDED 



#include<stdio.h> 
#include<stdlib.h> 
#include<malloc.h> 
#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 

和(XPM_lib.c):

#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->width = width; 
    image->height = height; 
    image->char_per_pixel = char_per_pixel; 
    image->no_colors = no_colors; 

    if ((image->color_table =(struct Color*) malloc (image->no_colors * sizeof(struct Color)) == NULL)) 
     { 
      printf("Eroare in initXPM_image la alocarea color_table"); 
      exit (0); 

     } 



    if ((image->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 < height ; i++) 
    { 
     if ((image->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->color_table[index].r = r; 
    image->color_table[index].g = g; 
    image->color_table[index].b = b; 

    image->color_table[index].color_char = pattern; 

} 

void setXPMpixel (struct XPM_image *image, unsigned int poz_x, unsigned int poz_y, unsigned int index) 
{ 

    image->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->width, image->height , image->no_colors , image->char_per_pixel); 

    // colors 
    fprintf(f, "%s \n" , STRING_COLORS); 
    int i; 
    for (i = 0 ; i < image->no_colors ; i++) 
     { 
      printf("\"%s C#%.02X%.02X%.02X\",\n" , image->color_table[i].color_char , image->color_table[i].r , image->color_table[i].g , image->color_table[i].b); 
     } 


} 

的test1.c文件是:

#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 < COLORS; i++) 
    { 
     setXPMcolor(image, i, 255*i/(COLORS-1),0,0,&first_char); 
     first_char++; 
    } 

    for (i = 0 ; i < WIDTH ; i++) 
     for (j = 0 ; j < HEIGHT ; j++) 
      setXPMpixel(image,i,j,i); 

    writeToFile(image,"imagine.xpm"); 
    return 0; 

} 

现在,我无法使用和IDE为此设置项目,因此我必须使用gcc手动编译它。要编译,我所做的:

gcc -c .\lib\XPM_lib.c 
gcc -c test.c 
gcc -o program .\lib\XPM_lib.o test.o 

(我忘了提!)我的目录结构:

.\lib\XPM_lib.h 
.\lib\XPM_lib.c 
.\test.c 
+1

你的实际目录结构是什么?这些头文件和源文件位于何处? – Mike 2013-02-28 16:55:50

回答

1

你需要确保GCC可以“看到”所有需要的头文件,以确保正确编译。为了做到这一点建设时,你应该包括lib目录到您的路径:

gcc -I ./lib -c XPM_lib.c test.c 
gcc -o program XPM_lib.o test.o 

注:

“未定义参考......”是指,即使你包括了头文件,但你不不告诉编译器与库链接。这些不是“未声明”的,它们是“未定义的”。这就是为什么它没有找到符号。

+0

这是我的错,我编辑了这篇文章,因此它现在准确无误。我有一个lib文件夹,在我的主文件夹中有两个XPM_lib.h和XPM_lib.c文件。我觉得奇怪的是,它与XPM_lib中定义的结构没有问题,只与函数有关。 – user1550876 2013-02-28 17:06:45

+0

@ user1550876 - 感谢您提供这些信息,检查更新和备注,是否回答您的问题? – Mike 2013-02-28 17:21:32

+0

非常感谢。它现在有效。 – user1550876 2013-02-28 17:27:23

相关问题