3

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.

4 Answers 4

4

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();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hmm so the main file in a project executes all the source files in the same project ? I am still new to C, kinda confused.
@TheKraven your main c file could contain a calls of functions which are defined in other C files. You have to define a prototype of theses function at the top of the main file before using them
Thanks alot, your example cleared up my confusion a fair bit :)
1

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

Comments

0

I had the same confusion and solved it using this method:

  1. Create source files including the functions (prototype and function body) of different codes

  2. Include these functions (prototype and function calling segment) in the main() file

  3. Run the application.

----Works well----

Comments

-1

for same project,you can have a no. of sources files,but only one of those sources files can have a main() function.

same project : - sources files: * 1st source file (without main()) * 2nd source file (without main()) * 3rd source file (without main()) * nth source file (without main()) * and only one source file with main().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.