简体   繁体   中英

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.

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();
}

您当然可以拥有许多源文件(.c 或 .cpp .cu 或其他扩展名/只需添加新源文件)但只有一个 main() 函数,因为这是您程序的入口点

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----

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().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM