Hello.

im having a little problem with file inclusion.

im getting the following errors:

error LNK2005: "long * addresses" (?addresses@@3PAJA) already defined in rere.obj
fatal error LNK1169: one or more multiply defined symbols found

My 3 files are as follows:

mainFile.c

//Program for a singly-linked list

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


int main(void)
{
    
    short first_node = 1; 
    short no_of_nodes = 0;
    short total = 0;

    struct Node *head = NULL;
    struct Node *temp = NULL;
    printf("Enter number of nodes to build\n");
    scanf("%d", &total);


    while(no_of_nodes <= total)
    {

        no_of_nodes += 1; //counter for nodes
        if(first_node)
        {
            head = (Node *)malloc(sizeof(Node)); //Allocate memory to head node
            head->i = (rand()%10); //Assign random numbers
            temp = head;
            printf("Address %d allocated is:- %p\t", no_of_nodes, head);
            printf("Value allocated is:- %d\n", head->i);
            first_node = 0; //set to false
        }
        else
        {
            if(no_of_nodes == (total+1))
            {
                temp->next = NULL;
                printf("List terminated at no. %d\n", no_of_nodes);
            }
            else
            {

                temp->next = (Node *)malloc(sizeof(Node));
                temp->i = (rand()%10);
                printf("Address %d allocated is:- %p\n", no_of_nodes, temp->next);
                printf("Value allocated is:- %d\n", temp->i);
                temp = temp->next;

            }
        }
    }

    reverse_singly_list(head, total, no_of_nodes=0);

    getchar();
    return 0;
}

def.h:

#ifndef DEF_H
#define DEF_H

long addresses[5];

struct Node {
    int i;
    struct Node *next;
};

void reverse_singly_list(struct Node* t1, int total, int n);

#endif

def.c:

#include<stdio.h>
#include "def.h"

void reverse_singly_list(struct Node* t1, int total, int n)
{
    printf("\nlist will be reversed here!\n");
    printf("\nAddress is %p \n",t1);
    printf("\nAddress is %p \n",t1->next);
    t1 = t1->next;
    printf("\nAddress is %p \n",t1->next);
    t1 = t1->next;

    //yet to write the code.	
}

______________________________________________________________________________________

i have give the #ifndef for avoiding multiple inclusion. But the problem still persists.Can anyone please tell me where i'm going wrong?

Thanks.

Recommended Answers

All 2 Replies

Take this out of your header file

long addresses[5];

and put it in your main executable...

When you compile def.c you create a label called addresses and when you compile mainfile.c you create another label addresses. When you bring both files together for the final executable it fails because addresses is defined in both def.o and mainFile.o.

Thanks a lot! That works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.