ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

在头文件中定义变量会导致多个变量定义

2019-11-20 12:00:36  阅读:236  来源: 互联网

标签:c-3 linux


我正在做一些测试编码,以根据给定的模式字符串生成模式,并执行如下操作:

头文件是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

.c文件如下: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

当我编译时,它给出以下内容:

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

我要去哪里错了.

解决方法:

您可以在头文件中声明全局变量,但不能为它赋值,因为您只能执行一次.因此,如果将test.h文件包含在多个.c文件中(这很常见),则编译器在汇编.o文件时将看到同一事物的许多初始化.

只在您的test.h文件中:

extern char uid [];

在一个特定的.c文件(例如uid.c)中,将其初始化:

char uid []= "123456789561";

然后将此新文件添加到您的Makefile中.

标签:c-3,linux
来源: https://codeday.me/bug/20191120/2043767.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有