0

hi

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?

3
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by dkalita
0

Normally, you would prototype all the functions that you want to use in one .c file that are defined in another in a header (.h) file, then use the #include statement to include that header, and then indicate to the linker that you are going to use both .c files.

You could also just #include the .c file in the other, but this is considered bad practice.

0

example:

file1.h

#ifndef FILE1_H
#define FILE1_H
int fun();
#endif

file1.c

#inlclude"file1.h"
int fun()
{
     printf("in fnuction");
}

file2.c

#include"file1.h"
int main()
{
   fun();
   return 0;
}

If you are using some IDE, you can directly build the project because it will do the linking for you.

If you are working in Linux/Unix, do the following:


cc -o testout file1.c file2.c

then

./testout

to run your program.

Edited by dkalita: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.