66

My files are

// main.c  

#include <ClasseAusiliaria.c>

int main(void) {
    int result = add(5,6);
    printf("%d\n", result);
}  

and

// add.c  

int add(int a, int b) {
    return a + b;
}
ProtoTyPus
  • 1,272
  • 3
  • 19
  • 40
  • 1
    You should export the signature `int addizione(int a, int b)` in an header file (e.g. `ClasseAusiliaria.h`). – Massimiliano Jan 21 '14 at 14:23
  • 1
    http://stackoverflow.com/questions/5904530/understanding-header-and-source-files-in-c – kol Jan 21 '14 at 14:24
  • Follow up this thread, this may give you some idea [Including one C source file in another?][1] [1]: http://stackoverflow.com/questions/232693/including-one-c-source-file-in-another – Varo Jan 21 '14 at 14:34

8 Answers8

47

Use double quotes #include "ClasseAusiliaria.c" [Don't use angle brackets (< >) ]

And I prefer to save the file with .h extension In the same directory/folder.

TLDR: Replace #include <ClasseAusiliaria.c> with #include "ClasseAusiliaria.c"

Gaurav Jain
  • 1,549
  • 1
  • 22
  • 30
  • 19
    Why you prefer .h instead to .c? There is some difference? – ProtoTyPus Jan 21 '14 at 14:31
  • 2
    Why it's better to use .h files: http://stackoverflow.com/a/232710/6028746 – Nir Duan Dec 30 '16 at 09:09
  • A small question. I've a header file declaring a struct + method and also the corresponding .c file to define them. If that method takes the struct as a parameter , how would I avoid including the .c file in another .c file where the main method is defined? – stdout Mar 09 '19 at 13:23
15

Change your Main.c like so

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

int main(void)
{
  int risultato;
  risultato = addizione(5,6);
  printf("%d\n",risultato);
}

Create ClasseAusiliaria.h like so

extern int addizione(int a, int b);

I then compiled and ran your code, I got an output of

11
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
6

You must declare int add(int a, int b); (note to the semicolon) in a header file and include the file into both files. Including it into Main.c will tell compiler how the function should be called. Including into the second file will allow you to check that declaration is valid (compiler would complain if declaration and implementation were not matched).

Then you must compile both *.c files into one project. Details are compiler-dependent.

Alexandr
  • 81
  • 1
  • 8
5

make a file classAusiliaria.h and in there provide your method signatures.

Now instead of including the .c file include this .h file.

Kraken
  • 23,393
  • 37
  • 102
  • 162
5

There are many great contributions here, but let me add mine non the less.

First thing i noticed is, you did not make any promises in the main file that you were going to create a function known as add(). This count have been done like this in the main file:

    int add(int a, int b); 

before your main function, that way your main function would recognize the add function and try to look for its executable code. So essentially your files should be

Main.c

    int add(int a, int b);

    int main(void) {
        int result = add(5,6);
        printf("%d\n", result);
    }  

and // add.c

    int add(int a, int b) {
        return a + b;
    }
Precious George
  • 105
  • 4
  • 8
3

You can include the .c files, no problem with it logically, but according to the standard to hide the implementation of the function but to provide the binaries, headers and source files techniques are used, where the headers are used to define the function signatures where as the source files have the implementation. When you sell your project to outside you just ship the headers and binaries(libs and dlls) so that you hide the main logic behind your function implementation.

Here the problem is you have to use "" instead of <> as you are including a file which is located inside the same directory to the file where the inclusion happens. It is common to both .c and .h files

Varo
  • 831
  • 1
  • 7
  • 16
2

you shouldn't include c-files in other c-files. Instead create a header file where the function is declared that you want to call. Like so: file ClasseAusiliaria.h:

int addizione(int a, int b); // this tells the compiler that there is a function defined and the linker will sort the right adress to call out.

In your Main.c file you can then include the newly created header file:

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

int main(void)
{
    int risultato;
    risultato = addizione(5,6);
    printf("%d\n",risultato);
}
UsYer
  • 173
  • 1
  • 8
2
 write main.c like this - 
 caution : while linking both main.0 and ClasseAusiliaria.o should be 
 available to linker.

 #include <stdlib.h>
 #include <stdio.h>
 extern int addizione(int a, int b)

 int main(void)
 {
     int risultato;
     risultato = addizione(5,6);
     printf("%d\n",risultato);
 }