How to compile a C project with more than one main function?
Actually, I find Dev-C++ supports working on multiple main files that are not part of any project, so I can create an run as many files as I need.
Thanks all who corporate here :)
Gook luck for all.
Also, for Linux/win I found Code::Blocks do that trick. thanks.
C: Can there be two main() functions in a program?
Standard C and C++ do not support nested functions, but:
- GCC supports nested functions in C, as a language extension.
- The D language, which is C-related, has nested functions.
and CodeBlocks use GCC compiler only, hence you're are not getting any error.
For the question
about having two
main()
in C program
No you can't, this is how compiler interprets where to start executing the program. It will take one of the main as local.
Also,
you are not getting "hello" printed
because when compiler starts executing your first main()
function, it takes the second main()
as local, and because you haven't called the second main()
, the string doesn't get printed.
How does the compiler choose between multiple main functions in C++?
It doesn't.
If you try to produce an executable with two functions of identical signature and external linkage, main
or something else, your linker should tell you that there are duplicate definitions and fail the build.
I suppose it could go undiagnosed in some cases (such as providing distinct overloads) but that doesn't make it right, and the results could be anything.
A static (or dynamic) library should not contain a main
, ever. There is nothing about unit testing that requires you to do that.
I don't want to [..] split my own project into several small pieces without main() function in them.
Why not? That is the normal, commonplace, proper way to design modular software. This problem you're experiencing is just one reason why.
main()
is the entrypoint to a program, not a trigger point for every individual piece of functionality within it. In your case, the entrypoint is the test runner, and it should be able to access your functionality via the appropriate classes and functions that you created in your library.
Two main functions in a C application
You can't have multiple main
functions for a single executable. There are several possibilities.
If doing a build builds only a single application, then only one of the main
functions will be compiled. (Or none, if there's an option to build a library rather than an executable.) There are probably options that determine which one to build, depending on which variant of the application you want, the target system, or something else.
Or perhaps the application consists of multiple executables, with one main
function for each one.
If running the build doesn't take too long, a trick I've used to determine which of several source files is actually compiled is to temporarily add #error
directives, like:
#error "TEMPORARY: This is /full/path/to/source.cpp"
The resulting error message will tell you which source file was actually compiled. (You can also use #warning
directives if your compiler supports them.)
I can have two main function in two static library of C++ when I link both of them?
The linker handles these two cases very differently (by default). When compiling code into object files and linking them the linker will not allow two strong definitions to exist. Strong here is for example a function or a variable with a set value. It will allow one strong and multiple weak ones (like a variable with a set value in one object and another with the same variable but no set value).
With static libraries the linker goes through them in the order they’re given. It looks up the exports, if any of them are needed by the other linked objects it takes it in and restarts the process in that file (to find the functions the just found function possibly needs). So in this process when it gets to the first library it checks for its exports, sees main
, determines it’s needed and takes it in. Then it goes to the second library, sees main
, doesn’t see it in the list of undefined symbols and just skips it.
More information can be found on Eli Bendresky’s website
Related Topics
Void_T "Can Implement Concepts"
What Does _Declspec(Dllimport) Really Mean
Opencv Cv::Mat and Eigen::Matrix
Reduce Flicker with Gdi+ and C++
Function in C++ Returns by Value or by Reference
Return Value of Operator Overloading in C++
What's the Point of G++ -Wreorder
How to Read Entire Stream into a Std::String
How to Alpha Blend Rgba Unsigned Byte Color Fast
How to Download the Visual C++ Command Line Compiler Without Visual Studio
Operator Overloading Outside Class
How to Get the Process Name in C++