drpogue
drpogue

Reputation: 133

Using gcc -c to generate .o files

So I had to create a bit of code that would make a list and do various things to it. Specifically print it, sort it, and see if a value were in it. Did that, ran fine. Now I've got to take those functions and split them up into separate files, then use gcc -c (which I'm not super sure I'm using correctly) to get .o files, plus a .o for the test program. Then I have to use gcc to link my .o files together into an executable. The prompt says it will recognize the .o's and realize how to link them.

So here are my questions: Why is the below code returning errors( in ways which will be defined below)? and what exactly am I supposed to be writing into the command line to link these guys?

So the code is as follows: (first the .h files, then the main .c file)

node.h

typedef struct Node{
    int data;
    struct Node *next;
    struct Node *prev;
}node;

print.h

#include<stdio.h>
#include"node.h"
void print(node *pointer){
    if (pointer == NULL){
        return;
    }

    printf("%d ",pointer->data);
    print(pointer->next);
}

init.h

#include<stdio.h>
#include"node.h"
int init(node *pointer,int find){
    pointer = pointer->next;

    while (pointer != NULL){
        if (pointer->data == find)//found find
        {
            printf("The data is in the list.");
            return 1;
        }
        pointer = pointer->next;// Search in the next node.
    }

    //find is not found
    printf("The data is not in the list.");
    return 0;
}

sort.h

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

void swap (node *x, node *y){
    int temp = x->data;
    x->data = y->data;
    y->data = temp;
}

void sort(node*pointer){
    int i;

    while (pointer->next != NULL){
        if (pointer->data>pointer->next->data){
            swap(pointer,pointer->next);
        }

        pointer = pointer->next;
        sort(pointer);
    }
}

list.c

#include<stdio.h>
#include<stdlib.h>
#include"node.h"
#lnclude"print.h"
#include"sort.h"
#include"init.h"
int i;
node *p;
node *n;

void insert(node *pointer, int data){
    //go through list till ya find the last node
    while (pointer->next != NULL){
        pointer = pointer->next;
    }

    //allocate memory for new node and put data in it
    pointer->next = (node *)malloc(sizeof(node));
    (pointer->next)->prev = pointer;
    pointer = pointer->next;
    pointer->data = data;
    pointer->next = NULL;
}

int main(){
    //start is used to point to the first node
    //temp is for the last node
    node *start, *temp;
    int z;
    start = (node *)malloc(sizeof(node));
    temp = new;
    temp->next = NULL;
    temp->prev = NULL;

    for (z = 0; z < 10; z++){
        insert(start,(3*10) - z);
    }

    init(start,12);
    init(start,3);
    init(start,27);
    init(start,7);
    print(start);
    sort(start);
    print(start);

}    

Now the code all ran perfectly fine when it was all together, with the exception of node.h (that was always a separate file). The .h files will compile perfectly, but when I try to compile the .c file it returns errors claiming that I am trying to redefine node in each .h file. Could this be because I am including it in each .h file?

I am also getting errors that I am trying to pass inappropriate arguments to the init function, which does not seem to the case. But I might just be over looking things.

Thank you in advance for any help.

EDIT: changed variable in main from new to start Errors when typing "gcc list.c"

In file included from init.h:2:0,
                 from list.c:4:
node.h:1:16 error: redefinition of'struct Node'
node.h:1:16 note: originally defined here
node.h:5:2  error: conflicting types for 'node'
node.h:5:2  note: previous declaration of 'node' was here

In file included from sort.h:2:0;
                 from list.c:5:
node.h:1:16 error: redefinition of'struct Node'
node.h:1:16 note: originally defined here
node.h:5:2  error: conflicting types for 'node'
node.h:5:2  note: previous declaration of 'node' was here

list.c: In function 'main':
list.c:41:1: warning: passing argument 1 of 'init' from incompatible pointer type[enabled by default]
init.h:4:5: note: expected 'struct node *' but argument is of type 'struct node *'

(and then there are errors for each of the separate init calls in main)

list.c:45:1: warning:passing argument 1 of 'print' from incompatible pointer type [enabled by default]
print.h:3:6: note expected 'struct node *' but argument is of type 'struct node *' 

(and then there is another one for the second print function)

Upvotes: 4

Views: 8182

Answers (1)

William Morris
William Morris

Reputation: 3684

Put inclusion barriers in the .h files. Eg, for sort.h

#ifndef INCLUDE_SORT_H
#define INCLUDE_SORT_H

// contents of file

#endif


EDIT

Actually I just looked at your code more closely. You have defined functions in .h files which is not the thing to do. Really I see no reason to separate this into separate files at all.

So combine them all into a single file and compile with:

gcc -o list -Wall list.c

If you really want separate files, then put the functions into C files and the structure and prototypes into a .h file (which you include into each C file). Then compile and link using something like:

gcc -o list -Wall list.c node.c main.c

Upvotes: 7

Related Questions