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?

Recommended Answers

All 2 Replies

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.