Using gcc -c to generate .o files

So I had to create some code that would make a list and do different things for it. Specifically, print it out, sort it, and see if there was a value in it. It was good. Now I have to take these functions and split them into separate files and then use gcc -c (which I am not sure if I am using correctly) to get the .o files as well as .o for the test program.Then I have to use gcc to link my .o files together with the executable. The tooltip says it will recognize the .o and figure out how to link them.

So here are my questions: Why does the code below return errors (in ways that will be defined below)? and also what exactly should I write on the command line to link these guys?

So the code looks like this: (first .h files, then 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 worked fine when everything was together except for node.h (it was always a separate file). The .h files will compile just fine, but when I try to compile the .c file, it returns errors, claiming that I am trying to override node in every .h file. Could it be because I am including it in every .h file?

I also get errors that I try to pass in mismatched arguments to the init function, which doesn't seem to be the case. But I might just rethink things.

Thanks in advance for any help.

EDIT: Changed variable mostly 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 individual init calls in the 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)

+3


source to share


1 answer


Place inclusion barriers in .h files. For example for sort.h

#ifndef INCLUDE_SORT_H
#define INCLUDE_SORT_H

// contents of file

#endif

      


EDIT


Actually I just took a close look at your code. You have defined functions in .h files that cannot be executed. Actually I see no reason to split this into separate files.

So, combine them all into one file and compile with:

gcc -o list -Wall list.c

      

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

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

      

+6


source







All Articles