67

I want to write my C functions in 2 separate .c files and use my IDE (Code::Blocks) to compile everything together.

How do I set that up in Code::Blocks?

How do I call functions in one .c file from within the other file?

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
amin
  • 805
  • 2
  • 8
  • 8
  • 3
    I don't see how this could possibly be any vaguer. Have you tried anything yet? Do you know the C language? Do you know what a header is? – Mike Caron Feb 26 '11 at 17:54
  • 6
    On the off chance that you're not aware of this: c is usually compiled, while talking about *"call[ing] files to use them"* has an interpreted language feel to it. The workflow for compiled languages differs slightly from that for interpreted languages (though many IDE will hide the difference from you). – dmckee --- ex-moderator kitten Feb 26 '11 at 17:57
  • People will need to know which IDE you are using to tell you how to do something in it. – J. Taylor Feb 26 '11 at 17:54

1 Answers1

163

In general, you should define the functions in the two separate .c files (say, A.c and B.c), and put their prototypes in the corresponding headers (A.h, B.h, remember the include guards).

Whenever in a .c file you need to use the functions defined in another .c, you will #include the corresponding header; then you'll be able to use the functions normally.

All the .c and .h files must be added to your project; if the IDE asks you if they have to be compiled, you should mark only the .c for compilation.

Quick example:

Functions.h

#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
/* ^^ these are the include guards */

/* Prototypes for the functions */
/* Sums two ints */
int Sum(int a, int b);

#endif

Functions.c

/* In general it's good to include also the header of the current .c,
   to avoid repeating the prototypes */
#include "Functions.h"

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

Main.c

#include <stdio.h>
/* To use the functions defined in Functions.c I need to #include Functions.h */
#include "Functions.h"

int main(void)
{
    int a, b;
    printf("Insert two numbers: ");
    if(scanf("%d %d", &a, &b)!=2)
    {
        fputs("Invalid input", stderr);
        return 1;
    }
    printf("%d + %d = %d", a, b, Sum(a, b));
    return 0;
}
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • if i want to use a .c file i should make it's header file (.h) too! – amin Feb 26 '11 at 18:26
  • i just want to break my complete programm into .c files and then IDE compile them together – amin Feb 26 '11 at 18:31
  • @amin: I don't understand, did you manage to solve your problem with my answer or you need additional help? – Matteo Italia Feb 27 '11 at 13:22
  • thanks a lot -you solved my problem-pardon me can you explain ifndef & endif commands? – amin Feb 28 '11 at 21:09
  • 7
    The `#ifdef`/`#define`/`#endif` are there to avoid problems in case of multiple inclusions of the same header. Have a look at the page about [include guards](http://en.wikipedia.org/wiki/Include_guard) on Wikipedia - it's all explained there. By the way, since my answer solved your problem, you may consider marking it as accepted (thank you!). `:)` – Matteo Italia Feb 28 '11 at 22:41
  • What order do you compile the c files in? – Luke Miles Apr 21 '18 at 16:43
  • 1
    @lcmgcd whatever order you prefer, each one is completely independent from the other - and actually, on modern machines multiple files are compiled in parallel. It's only at the linking stage that they are tied together. – Matteo Italia Apr 21 '18 at 16:46
  • @MatteoItalia this is almost a decade old but this answer helped me a lot, +1 and Thanks ! – Ait-Gacem Nabil Sep 05 '21 at 18:01
  • @Ait-GacemNabil: always glad that something I wrote a decade ago keeps helping people! :-) – Matteo Italia Sep 05 '21 at 21:31
  • Could you elaborate this answer to include how one could incorporate multiple directories (eg., headers, srcs, etc.) in the project? – learner Nov 23 '21 at 21:57
  • 2
    @learner unfortunately that depends on the exact IDE/build system you are using, so I don't think I can easily add it. Ultimately it's a part that is better left to answers specific to each build system. – Matteo Italia Nov 30 '21 at 21:28