简体   繁体   English

使用标头在turbo c项目文件中的链接器错误

[英]linker error in turbo c project file using header

I have this code that was remade to be a project file. 我将此代码重新编译为项目文件。 it runs fine before it was turned into a project file. 它在变成项目文件之前运行正常。 the main error is linker error stating that some of the struct variables are being duplicated, which from my experience before would be fixed if i add some extern to it, but to my surprise it didn't to the trick and nothing really changed. 主要的错误是链接器错误,说明某些结构变量是重复的,根据我以前的经验,如果我添加一些extern,它将被修复,但令我惊讶的是它没有诀窍,没有真正改变。

NOTE: 注意:

I am using turbo C as a compiler school required and what we use in school so yeah 我正在使用turbo C作为编译器学校所需要的以及我们在学校使用的东西所以是的

the variables that are duplicated are: 重复的变量是:

root, temp, t2, and t1 root,temp,t2和t1

错误

Header: 标题:

#ifndef BSTDLL_H
#define BSTDLL_H
#include<stdio.h>
#include<stdlib.h>

struct btnode
{
    int value;
    struct btnode *l;
    struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;

void delete1();
void insert();
void delete();
void create();
void search(struct btnode *t);
void search1(struct btnode *t,int data);
int smallest(struct btnode *t);
int largest(struct btnode *t);
void print(struct btnode *t, int x, int i, int y);

#endif

print: 打印:

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


void print(struct btnode *t, int x, int i, int y){
    i = i / 2 + 2;
    if (root == NULL){
        printf("No elements in a tree to display");
        return;
    }
    if(y > 6){
        return;
    }else{
        if (t->l != NULL){
            print(t->l, (x - i), i, (y + 1));
        }
            gotoxy(x, y * 2);
            printf("%d ", t->value);
        if (t->r != NULL){
            print(t->r, (x + i), i, (y + 1));
        }
    }


}

insert: 插入:

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

/* To insert a node in the tree */
void insert()
{
    create();
    if (root == NULL) 
        root = temp;
    else    
        search(root);    
}

/* To create a node */
void create(){
    int data;

    printf("Enter value: ");
    scanf("%d", &data);
    temp = (struct btnode *)malloc(1*sizeof(struct btnode));
    temp->value = data;
    temp->l = temp->r = NULL;
}

/* Function to search the appropriate position to insert the new node */
void search(struct btnode *t){
    if ((temp->value > t->value) && (t->r != NULL))      /* value more than root node value insert at right */
        search(t->r);
    else if ((temp->value > t->value) && (t->r == NULL))
        t->r = temp;
    else if ((temp->value < t->value) && (t->l != NULL))    /* value less than root node value insert at left */
        search(t->l);
    else if ((temp->value < t->value) && (t->l == NULL))
        t->l = temp;
}

delete: 删除:

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


/* To check for the deleted node */
void delete()
{
    int data;

    if (root == NULL)
    {
        printf("No elements in a tree to delete");
        return;
    }
    printf("Enter the data to be deleted : ");
    scanf("%d", &data);
    t1 = root;
    t2 = root;
    search1(root, data);
}

main 主要

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

struct btnode *root = NULL, *temp = NULL, *t2, *t1;

void main(){
    int ch;
    clrscr();

    while(1){
        printf("\n\n\n\n\n\n\n\n\n\nOPERATIONS ---");
        printf("\n1 - Insert\n");
        printf("2 - Delete\n");
        printf("3 - Exit\n");
        printf("\nChoice : ");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:    
            insert();
            clrscr();
            print(root, 40, 30, 2);
            break;
        case 2:    
            delete();
            clrscr();
            print(root, 40, 30, 2);
            break;
        case 3:    
            exit(0);
        default :     
            printf("Wrong choice");
            break;    
        }
    }
}

I did not add anything in my main.c file 我没有在main.c文件中添加任何内容

ps I have omitted the other function as they maybe irrelevant in my dilemma here. ps我省略了其他功能,因为它们在我的困境中可能无关紧要。

Header : 标题:

change 更改

struct btnode
{
    int value;
    struct btnode *l;
    struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;

to

struct btnode
{
    int value;
    struct btnode *l;
    struct btnode *r;
};
extern struct btnode *root, *temp, *t2, *t1;

main.c main.c中

add

struct btnode *root = NULL, *temp = NULL, *t2, *t1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM