Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple source files in a C/C++ Application project ( NetBeans )

I created a new C/C++ project via File > New Project > C/C++ > C/C++ Application.

However, under the Source Files folder, only 1 source file with main is allowed.

Is there any way to include more than 1 main source file in the Source Files folder ?

Or do I have to create a new project for each source file ?

In contrast, for each Java project, there can be many .java files in it. I am trying to find the same functionality for C/C++ applications.

Thanks.

like image 900
TheKraven Avatar asked May 02 '26 08:05

TheKraven


2 Answers

For the same project you could have many source files *.c and *.h and others

But for the same project you could have only one main() function in all of the source files

Example:

Code architecture:

.
└── source_folder
    ├── file1.c
    ├── file2.c
    └── main.c

file1.c

#include <stdio.h>

void printfile1()
{
    printf("this is the file1.c\n");
}

file2.c

#include <stdio.h>

void printfile2()
{
    printf("this is the file2.c\n");
}

main.c

#include <stdio.h>

void printfile1(); //prototype definition
void printfile2(); //prototype definition

int main()
{
    printfile1();
    printfile2();
}
like image 167
MOHAMED Avatar answered May 03 '26 21:05

MOHAMED


you can have many source files of course (.c or .cpp .cu or else extensions/ simply add new source file) but only one main() function since this is the entry point to your program

like image 32
4pie0 Avatar answered May 03 '26 21:05

4pie0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!