Skip to content
Sign upLog in
← Back to Community

How can I link other .C files in the main file?

Profile icon

Instead of making a huge code with a lot of functions in only one file, is it possible to link other .C files saved and attach them to the Main file to do the same function? For example. I'd like to use the Main only as a menu, and put the functions on other files and link it on the Main file

Profile icon
Profile icon

I'm not entirely familiar with C, BUT in C++, you do this with a header file. For example, in your main file, you have
#include "example.h"

Then you create example.h. In here you declare, but don't define, the functions you want to have in your other file, like this:
extern int example();

Finally, you make your other file to be linked. In here you include the header and define your functions:
#include "example.h"
int example() { std::cout << "this can be whatever you want it to be\n"}

Then, to use the functions in your main file, you just call them as normal.
int main () {
return 0;}

Again this is for c++ but it should be fairly similar in C and this should at least give you a place to start looking.