0

I have a small program for Linklist in C..

** linklist.h**

//
//  linklist.h
//  LinkList
//
//  Created by Mayukh Sarkar on 17/04/15.
//  Copyright (c) 2015 Mayukh Sarkar. All rights reserved.
//

#ifndef __LinkList__linklist__
#define __LinkList__linklist__


struct LinkList{
    int Data;
    struct LinkList* NextData;
};
struct LinkList *Firstdata = 0;
// void push_data_at(struct LinkList* ptr, int index, int data);
struct LinkList* push(struct LinkList* currentData, int data);
// void push_front(struct LinkList* ptr, int data);
void print_list(struct LinkList* ptr);

#endif /* defined(__LinkList__linklist__) */

linklist.c

//
//  linklist.c
//  LinkList
//
//  Created by Mayukh Sarkar on 17/04/15.
//  Copyright (c) 2015 Mayukh Sarkar. All rights reserved.
//

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

struct LinkList* push(struct LinkList* currentData, int data){

    if (Firstdata == 0) {
        currentData = (struct LinkList*)malloc( sizeof(struct LinkList));
        currentData->Data = data;
        currentData->NextData = 0;
        Firstdata = currentData;
    }else{
        struct LinkList* temp = (struct LinkList*)malloc( sizeof(struct LinkList));
        temp->Data = data; // save the data
        currentData->NextData = temp; // Change the pointer from NULL to next value
        currentData = temp;
        currentData->NextData = 0;

    }
    return currentData;
}


void print_list(struct LinkList* ptr){
    struct LinkList* loop = Firstdata;
    while (loop) {
        printf("%d  ", loop->Data);
        loop = loop->NextData;
    }
}

main.c

//
//  main.c
//  LinkList
//
//  Created by Mayukh Sarkar on 17/04/15.
//  Copyright (c) 2015 Mayukh Sarkar. All rights reserved.
//
#include "linklist.h"

int main(int argc, const char * argv[]) {

    struct LinkList* currentNode = 0;
    currentNode =push(currentNode, 10);
    currentNode =push(currentNode, 20);
    currentNode =push(currentNode, 30);

    print_list(Firstdata);


    return 0;
}

I made this in Xcode & compiled but it gives me an error..Please solve the error for me..I cannot even make it work in terminal.

I am using Yosemite 10.10.3 & the latest version of Xcode 6

The error looks like this

Ld /Users/mayukhsarkar/Library/Developer/Xcode/DerivedData/LinkList-anpcumiwovgnfjdfgeztikdyqvqi/Build/Products/Debug/LinkList normal i386
    cd /Users/mayukhsarkar/Documents/C/LinkList
    export MACOSX_DEPLOYMENT_TARGET=10.4
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch i386 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -L/Users/mayukhsarkar/Library/Developer/Xcode/DerivedData/LinkList-anpcumiwovgnfjdfgeztikdyqvqi/Build/Products/Debug -F/Users/mayukhsarkar/Library/Developer/Xcode/DerivedData/LinkList-anpcumiwovgnfjdfgeztikdyqvqi/Build/Products/Debug -filelist /Users/mayukhsarkar/Library/Developer/Xcode/DerivedData/LinkList-anpcumiwovgnfjdfgeztikdyqvqi/Build/Intermediates/LinkList.build/Debug/LinkList.build/Objects-normal/i386/LinkList.LinkFileList -mmacosx-version-min=10.4 -Xlinker -dependency_info -Xlinker /Users/mayukhsarkar/Library/Developer/Xcode/DerivedData/LinkList-anpcumiwovgnfjdfgeztikdyqvqi/Build/Intermediates/LinkList.build/Debug/LinkList.build/Objects-normal/i386/LinkList_dependency_info.dat -o /Users/mayukhsarkar/Library/Developer/Xcode/DerivedData/LinkList-anpcumiwovgnfjdfgeztikdyqvqi/Build/Products/Debug/LinkList

duplicate symbol _Firstdata in:
    /Users/mayukhsarkar/Library/Developer/Xcode/DerivedData/LinkList-anpcumiwovgnfjdfgeztikdyqvqi/Build/Intermediates/LinkList.build/Debug/LinkList.build/Objects-normal/i386/main.o
    /Users/mayukhsarkar/Library/Developer/Xcode/DerivedData/LinkList-anpcumiwovgnfjdfgeztikdyqvqi/Build/Intermediates/LinkList.build/Debug/LinkList.build/Objects-normal/i386/linklist.o
ld: 1 duplicate symbol for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
2
Contributors
2
Replies
16
Views
2 Years
Discussion Span
Last Post by rubberman
0

Thanks guys but I have solved this problem..Here is my solution

  1. deleted line number 17 from linklist.h file
  2. add the same line in linklist.c file
  3. add the line in the linklist.h extern struct LinkList* Firstdata

It was just a simple liniking error...Now I can take a sip of my coffee..:)

0

Yes. The biggest issue was not declaring FirstData variable as extern. As a result, a unique global variable would be created in each translation unit that included linklist.h, which caused the duplicate symbol error.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.