sgerbhctim
sgerbhctim

Reputation: 3640

How to set up multiple C files to coexist

How would I set up a structure such that I have methods in

helper.c

main.c

main.h

... how can I include helper.c in my main.c and use the methods built in helper.c?

I am running makefile as:

all:
        gcc -o main main.c
        gcc -o helper helper.c



clean: 
        rm -f main
        rm -f helper

I understand I need a helper.h, but how do I properly set that up.. say I'd like my helper file to look like this:

struct Node{
    struct Node* nxt;
    int x;
};

int isThere(struct Node *head, int value){

    if(head==NULL){
        return 0;
    }
    struct Node *tmp=head;

    while(tmp!=NULL){
        if(tmp->x==value){
            return 1;
        }
        tmp=tmp->nxt;
    }
    return 0;
}

struct Node *nodeInsert(struct Node *head, int value){
    if(head==NULL){
        head=malloc(sizeof(struct Node));
        head->x=value;
        head->nxt=NULL;
        printf("inserted\n");
        return head;
    } else if(head!=NULL && isThere(head,value)==1){
        printf("duplicate\n");
        return head;
    } else{

        struct Node *new;
        struct Node *tmp=head;
        while(tmp->nxt!=NULL){
            tmp=tmp->nxt;
        }   

        new=malloc(sizeof(struct Node));
        new->x=value;
        tmp->nxt=new;
        new->nxt=NULL;
        printf("inserted\n");
        return head;
}}

Upvotes: 2

Views: 399

Answers (4)

Redouane
Redouane

Reputation: 430

I would like to add to @John Weldon answer that you can include your helper.c in your main.c directly and declare functions in it as static like this example:

// main.c    
#include "helper.c"

int main(void)
{
    HelloWorld();
    return 0;
}

// helper.c
#include <stdio.h>

static void HelloWorld(void)
{
    puts("Hello World!!!");
}

And in your Makefile you compile helper.c and main.c together like:

gcc -o main main.c helper.c

Upvotes: 0

Petr Skocik
Petr Skocik

Reputation: 60143

gcc -o helper helper.c would attempt both compilation and linking, but since helper.c doesn't define a main(), it won't link.

What you want to do is simply compile main.c and helper.c separately into object files:

gcc -c main.c #-o main.o (the -o main.o part is implied if missing)
gcc -c helper.c #-o helper.o

and then link the resulting object files into the final executable.

gcc -o main main.o helper.o

As for the header: helper.c defines struct Node and methods nodeInsert and isThere. In order to use these properly, main needs their prototypes, so the standard way to provide them to it would be to define a helper.h header:

#ifndef HELPER_H
#define HELPER_H /*header guard to protect against double inclusion*/
struct Node{
    struct Node* nxt;
    int x;
};
int isThere(struct Node *head, int value);
struct Node *nodeInsert(struct Node *head, int value);
#endif

and include it at the top of main.c:

#include "helper.h"
//...

(You can also include it in helper.c. That should allow the compiler to help you catch possible erroneous inconsistencies.)

Upvotes: 2

adnanalhomssi
adnanalhomssi

Reputation: 129

I think the problem is that you miss understanding compiling and linking in C. There is a lot of source to explain this, here is a good one : http://courses.cms.caltech.edu/cs11/material/c/mike/misc/compiling_c.html

what you should do is compile all of them to object files then link them together. you can do this in single command

gcc -o executable main.c helper.c

or compile each one first then link them together

gcc -c main.c gcc -c helper.c gcc -o executable main.o helper.o

Make sure you write prototypes for all functions of helper.c in helper.h and include helper.h at the beginning of main.c

Upvotes: 4

John Weldon
John Weldon

Reputation: 40789

Change your makefile so that all the .c files that are supposed to be in the binary are referenced:

all:
    gcc -o main main.c helper.c

Also, your code in main.c needs to know the method declarations in helper.c, which is why the struct declarations and the function declarations for the code in helper.c should be in main.h (or in helper.h and included in main.h)

Upvotes: 1

Related Questions