gcc 编译多个文件 符号重复错误?

查看:147
本文介绍了gcc 编译多个文件 符号重复错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

源代码有五个文件,三个c文件,两个h文件 如下:
getword.h

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAXWORD 100
#define BUFIZE 100

char buf[BUFSIZ];
int bufp = 0;

int getch(void);
void ungetch(int);

int getword(char *, int);

getword.c

#include "getword.h"

int getch(void)
{
    extern char buf[BUFSIZ];
    extern int bufp;
    if(bufp > 0){
        return buf[--bufp];
    }
    return getchar();
}

void ungetch(int c)
{
    extern char buf[BUFSIZ];
    extern int bufp;
    if(bufp >= BUFSIZ){
        printf("ungetch: too many characters!\n");
    } else{
        buf[bufp++] = c;
    }
}

int getword(char *word, int lim)
{
    int c;
    char *w = word;

    while(isspace(c = getch()))
        ;
    if (c != EOF)
        *w++ = c;
    if (!isalpha(c)) {
        *w = '\0';
        return c;
    }
    for (; --lim > 0; w++){
        if (!isalnum(*w = getch())) {
            ungetch(*w);
            break;
        }
    }
    *w = '\0';
    return word[0];
}

treenode.h

#include <stdio.h>
#include <stdlib.h>
#include "getword.h"

struct tnode {
    char *word;
    int count;
    struct tnode *left;
    struct tnode *right;
};

struct tnode *talloc(void);
char *strdupme(char *);

struct tnode *addtree(struct tnode *, char *);
void treeprint(struct tnode *);

treenode.c

#include "treenode.h"

struct tnode *talloc(void)
{
    return (struct tnode *) malloc(sizeof(struct tnode));
}

char *strdupme(char *s)
{
    char *p;

    p = (char *) malloc(strlen(s) + 1);
    if (p != NULL)
        strcpy(p, s);
    return p;
}

struct tnode *addtree(struct tnode *p, char * w)
{
    int cond;

    if(p == NULL) {
        p = talloc();
        p->word = strdupme(w);
        p->count = 1;
        p->left = p->right = NULL;
    } else if((cond = strcmp(w, p->word)) == 0){
        p->count++;
    } else if(cond < 0){
        p->left = addtree(p->left, w);
    } else{
        p->right = addtree(p->right, w);
    }
    return p;
}

void treeprint(struct tnode *p)
{
    if(p != NULL){
        treeprint(p->left);
        printf("%4d %s\n", p->count, p->word);
        treeprint(p->right);
    }
}

main.c

#include "treenode.h"

int main()
{
    struct tnode *root;
    char word[MAXWORD];

    root = NULL;
    while (getword(word, MAXWORD) != EOF){
        if(isalpha(word[0])){
            root = addtree(root, word);
        }
    }
    treeprint(root);
    return 0;
}

gcc编译错误:

gcc getword.c treenode.c main.c -o main.out
duplicate symbol _bufp in:
    /var/folders/q3/kxp92gk548z_y9pc1n3qsztw0000gn/T/getword-5a97ad.o
    /var/folders/q3/kxp92gk548z_y9pc1n3qsztw0000gn/T/treenode-c5ce26.o
duplicate symbol _bufp in:
    /var/folders/q3/kxp92gk548z_y9pc1n3qsztw0000gn/T/getword-5a97ad.o
    /var/folders/q3/kxp92gk548z_y9pc1n3qsztw0000gn/T/main-7e1139.o
ld: 2 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

请问是哪里符号重复了?

解决方案

getword.h里面定义了char buf[BUFSIZ];int bufp = 0;,然后你又这么多文件包含该头文件,导致分别编译,链接时出现重复定义的问题,可以把getword.h里面的定义修改成extern的声明,然后在c文件中定义。这样就可以编译通过了!

这篇关于gcc 编译多个文件 符号重复错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆