Defining a variable in a header file results in multiple variables being defined

I did some test coding to generate a template base from a given template string and did the following:

Header file test.h:

#ifndef test_h

#define test_h

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

extern char uid []= "123456789561";

void generate_uid(FILE *,char *, char);

#endif

      

The .c file looks like this: test.c

#include"test.h"

extern int count;

void generate_uid(FILE *fp,char *uid_t, char ch)
{
    int index = rand()%12;
    int i,j;

    strcpy(uid_t,uid);

    if(ch == ' ')
    {
        for(j=3;j<10;j+=6)
        {
            uid_t[j] = ch;
        }
    }
// Replace numbers with special chars or alphabets(small/capital)
    else
    {
        if(index < 6)
        {
            for(i=0;i<index;i++)
            {
                uid_t[i]=ch;
            }
        }   
        else
        {
            for(i=index;i<strlen(uid);i++)
            {
                uid_t[i]=ch;
            }
        }
    }
    count++;
    fprintf(fp,"\"%s\", ",uid_t);
        return;
}

      

main.c:

#include"test.h"

int count = 0;

int main()
{

    FILE *fp_char,*fp_test;

    char invalid_chars;
    char *uid_t = (char *)malloc(sizeof(char)*14);

    fp_char = fopen("invalidchars.txt","r");
    fp_test = fopen("uid_test.txt","w");

    if(fp_test == NULL || fp_char == NULL)
    {
        printf("cannot open file.\n");
        return;
    }

    while((invalid_chars = fgetc(fp_char)) != EOF) 
    {
        if(invalid_chars == '\n')
        {
            continue;
        }

        generate_uid(fp_test,uid_t, invalid_chars);

    }
        //Greater than 12 digit
        strcpy(uid_t,uid);
        strcat(uid_t,"2");
        count++;
        fprintf(fp_test,"\"%s\", ",uid_t);

        //Less than 12 digit
        strcpy(uid_t,uid);
        uid_t[11]='\0';
        count++;
        fprintf(fp_test,"\"%s\", ",uid_t);       



        count++;
        fprintf(fp_test,"\"%s\", ","NULL");
        count++;
        fprintf(fp_test,"\"%s\", ","empty");


    free(uid_t);
    fclose(fp_char);
    fclose(fp_test);
    printf("Number of string count : %d\n",count);
    return 0;
}

      

Makefile:

all : test.o main.o run

run : test.o main.o
    $(CC) -g $^ -o $@

%.o : %.c
    ${CC} -g -c $< -o $@


.PHONY : clean

clean :
    -rm -f *.o run

      

When I compiled it gives the following:

cc -g -c test.c -o test.o
cc -g -c main.c -o main.o
cc -g test.o main.o -o run
main.o:(.data+0x0): multiple definition of `uid'
test.o:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status
make: *** [run] Error 1

      

Where am I going wrong.

+3


source to share


2 answers


You can declare a global variable in the header file, but not assign a value to it, because you can only do this once. So if you include your file test.h

in more than one .c file (this will usually be the case), your compiler will see many initializations of the same thing when it builds your .o files.

Let only in your file test.h

:

extern char uid [];

      



And in one particular .c file (for example uid.c

), initialize it:

char uid []= "123456789561";

      

Then add this new file to your Makefile.

+3


source


You have defined in the header object

extern char uid []= "123456789561";

      

and this definition is now present in every compilation unit where the header is included. Thus, the same object is defined multiple times and the compiler throws an error.

You have to declare it in the header, for example



extern char uid [13];

      

and define it in one of the modules

char uid []= "123456789561";

      

0


source







All Articles