-1

I have written a C code for linked list and have various functions such as insert, delete, and traverse declared within the same file. I want to now define these functions within separate .c files, but I am unable to find the right solution. The following code is from LinkedList.h file:

    struct node
{
    int data;
    struct node *link;
};
struct node *head; // Global variable

void insert(int x);
void insert2(int x, int n);
void traverse();
void delete(int n);

LinkedList.c file :

#include <stdio.h>
#include <stdlib.h>
#inlcude "LinkedList.h"
#inlcude "insert.c"
#inlcude "traverse.c"
#inlcude "insert.c"
#include "insert2.c"

int main(void)

from one of the .c files `include

#include <stdlib.h>
#include "LinkedList.h"

void insert(int x)
{
    struct node *curr = (struct node *)malloc(sizeof(struct node));
    if(head == NULL)    // for empty list condition
    {

I have included only the initial part of the code for the .c files.

following error is observed on compiling LinkedList.c

  In file included from delete.c:3:0,
                 from LinkedList.c:14:
LinkedList.h:1:8: error: redefinition of ‘struct node’
 struct node
        ^
In file included from traverse.c:3:0,
                 from LinkedList.c:13:
LinkedList.h:1:8: note: originally defined here
 struct node
        ^
In file included from delete.c:3:0,
                 from LinkedList.c:14:
LinkedList.h:6:14: error: conflicting types for ‘head’
 struct node *head; // Global variable
              ^
In file included from traverse.c:3:0,
                 from LinkedList.c:13:
LinkedList.h:6:14: note: previous declaration of ‘head’ was here
 struct node *head; // Global variable
              ^
In file included from insert2.c:3:0,
                 from LinkedList.c:15:
LinkedList.h:1:8: error: redefinition of ‘struct node’
 struct node
        ^
In file included from delete.c:3:0,
                 from LinkedList.c:14:
LinkedList.h:1:8: note: originally defined here
 struct node
        ^
In file included from insert2.c:3:0,
                 from LinkedList.c:15:
LinkedList.h:6:14: error: conflicting types for ‘head’
 struct node *head; // Global variable
              ^
In file included from traverse.c:3:0,
                 from LinkedList.c:13:
LinkedList.h:6:14: note: previous declaration of ‘head’ was here
 struct node *head; // Global variable
              ^
In file included from insert.c:3:0,
                 from LinkedList.c:16:
LinkedList.h:1:8: error: redefinition of ‘struct node’
 struct node
        ^
In file included from insert2.c:3:0,
                 from LinkedList.c:15:
LinkedList.h:1:8: note: originally defined here
 struct node
        ^
In file included from insert.c:3:0,
                 from LinkedList.c:16:
LinkedList.h:6:14: error: conflicting types for ‘head’
 struct node *head; // Global variable
              ^
In file included from traverse.c:3:0,
                 from LinkedList.c:13:
LinkedList.h:6:14: note: previous declaration of ‘head’ was here
 struct node *head; // Global variable
              ^
In file included from LinkedList.c:17:0:
LinkedList.h:1:8: error: redefinition of ‘struct node’
 struct node
        ^
In file included from insert.c:3:0,
                 from LinkedList.c:16:
LinkedList.h:1:8: note: originally defined here
 struct node
        ^
In file included from LinkedList.c:17:0:
LinkedList.h:6:14: error: conflicting types for ‘head’
 struct node *head; // Global variable
              ^
In file included from traverse.c:3:0,
                 from LinkedList.c:13:
LinkedList.h:6:14: note: previous declaration of ‘head’ was here
 struct node *head; // Global variable

Following error is observed on compiling .c files wherein function is defined

/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
CaptainDaVinci
  • 975
  • 7
  • 23
  • 1
    Your global variable, `head`, is being defined multiple times, each time that header is included. Perhaps you should be using an `extern` declaration? Also, if you're compiling C code, it is not necessary (and not recommended) to cast the result of `malloc`. – Cody Gray - on strike Nov 26 '16 at 14:58
  • But malloc would return a void pointer, isn't it necessary to type cast it as pointer to a node ? – CaptainDaVinci Nov 27 '16 at 07:16
  • No. Void pointers are assignment-compatible with other pointer types in C. If you were compiling as C++, the cast would be required, but not for C. See: http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Cody Gray - on strike Nov 27 '16 at 09:01

1 Answers1

1

I believe you should better understand how C programs are built:

        #include     compile         link
               +-----+      +-----+
           ,-> | y.c | ---> | y.o |-.
  +-----+ /    +-----+      +-----+  \    +-----+
  | x.h |                             o-> | exe |
  +-----+ \    +-----+      +-----+  /    +-----+
           `-> | w.c | ---> | w.o |-'
               +-----+      +-----+

Assuming GCC compiler:

  • header files (.h) are included into the source files (.c) with #include.
  • source files (.c) are compiled into object files (.o) by the compiler with gcc -c
  • object filese (.o) are linked together adding library files (.a) into executables programs with gcc.

Your LinkedList.c files clearly shows that you had something different in mind.

The effect of #include is the same of copying the content of the file that is being included and pasting it into the file that includes it.

If you look again at you LinkedList.c file, you'll understand the reason of the errors. After all the #include happened you have multiple definitions of the struct and the variable.

The error about missing main() is due to the fact that you are trying to bypass the link step and trying to create an executable from a source file that has no main() function in it.

Try to reorganize your build process keeping in mind the different steps that are involved and you'll solve the issue.

Remo.D
  • 16,122
  • 6
  • 43
  • 74