user3717474
user3717474

Reputation: 57

How to link two files using header file in C

I am trying to link two files. Means, there are files "file1.c" and "file2.c".

file1.c

#include"stdlib.h"
#include"stdio.h"
 void function1(int a)
 {
printf("hello I am file%d.c\n ", a);
  }
 void main()
 {
 function1(1);
  }

file2.c

#include"stdlib.h"
#include"stdio.h"
#include"file.h"
void function2(int b)
{
 printf("hello I am file%d.c\n", b);
 }
int main()
{
 function2(2);
 function1(1);
 }

Then I make a header file file.h as

#ifndef hell
#define hell
 void function1(int a);
#endif

When I compile file2.c as "gcc file2.c file1.c -o file2 " it gives following error

/tmp/cc4tno9R.o: In function `main':
 file1.c:(.text+0x24): multiple definition of `main'
/tmp/ccL4fEki.o:file2.c:(.text+0x24): first defined here
 collect2: ld returned 1 exit status

How to write in header file? Is there any error in header file? Or error in file2.c?

And what about extern? Is it uses for same purpose?

Upvotes: 1

Views: 4520

Answers (4)

nIcE cOw
nIcE cOw

Reputation: 24616

Say that the directory structure is like:

                  Project
                     |
       ------------------------------
      |              |              |
    csource        output         header
      |              |              |
    *.c files    executable      .h files
                   files

Now, put these two .c files inside the source folder.

function.c

int sum(int a, int b)
{
    return (a + b);
}

main.c

#include <stdio.h>
#include <stdlib.h>
#include <mymath.h>

int main(void)
{
    int result = sum(11, 19);
    printf("Result: %d\n", result);

    return EXIT_SUCCESS;
}

Put this header file inside header folder.

mymath.h

#ifndef _MyMath_H__
#define _MyMath_H__
int sum(int, int);
#endif

COMPILATION:

Firstly, we will compile function.c file and create one object file with .o extension, as follows:

C:\Mine\C\test\project>gcc -o output\function.o -c source\function.c

On Cygwin:

Gagandeep Bali@LAPTOP ~/c/Mine/C/test/project
$ gcc -o output/function.o -c source/function.c

Since, function.c doesnot contains a main method, hence, we will simply use the -c option, to only create an object file.

Here, the use of -I option, basically tells the compiler, where to look for include files. Since, we are defining our header folder, hence, you can use #include <mymath.h> instead of #include "mymath.h". Now we will compile themain.c` file as:

C:\Mine\C\test\project>gcc -o output\main -I header\ -Wall source\main.c output\function.o

On Cygwin:

Gagandeep Bali@LAPTOP ~/c/Mine/C/test/project
$ gcc -o output/main -I header/ -Wall source/main.c output/function.o

Now one can run it, like:

C:\Mine\C\test\project>.\output\main
Result: 30

On Cygwin:

Gagandeep Bali@LAPTOP ~/c/Mine/C/test/project
$ ./output/main
Result: 30

You can also, create static and dynamic libraries, of custom functions, that you can use. I just know, how to create a static library.

If you wanted to create a static library, of your own, simply first put all object files inside the library. Create another folder, say library for this purpose. Now add all .o files inside the library, like this:

Gagandeep Bali@LAPTOP ~/c/Mine/C/test/project
$ ar cr library/mymathlibrary.a output/function.o

Now simply compile program like:

Gagandeep Bali@LAPTOP ~/c/Mine/C/test/project
$ gcc -Wall source/main.c library/mymathlibrary.a -o output/main -I header

And run as previously described.

Upvotes: 4

Sashikanta
Sashikanta

Reputation: 51

You don't need to include all library files in the first file. Just save it as a library file with a ".h" extension as a library file and include it in second file, Like shown below.

file1.h

void function1(int a) {
    printf("hello I am file%d.c\n ", a);
}

file2.c

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

void function2(int b) {
    printf("hello I am file%d.c\n", b);
}

int main() {
    function2(2);
    function1(1);

return 0;

}

Upvotes: 3

antonpuz
antonpuz

Reputation: 3316

When you run the program main is beeing called. If you have 2 definitions of main which one should be called?

There should be one file including main and another file including function that you want to use in the first file.

Upvotes: 1

Rizier123
Rizier123

Reputation: 59681

So all should look like this:

file1.c:

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

void function1(int a) {
    printf("hello I am file%d.c\n ", a);
 }

file2.c:

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

void function2(int b) {
    printf("hello I am file%d.c\n", b);
}

int main() {
    function2(2);
    function1(1);

    return 0;

}

Upvotes: 1

Related Questions