Eclipse MinGW: how to have multi main in one project - c++

When I run a Java Program on Eclipse, each time I run a file, Compiler will check does it has main, if it has, I can run. And if not, I cannot.
But when I run a C++ Program (CDT) on Eclipse using MinGW, at Compiler doesn't work like that. After I compile, don't matter I run from which file, Compiler will search whole project to find ONE MAIN So, if I has many main file, I will receive error.
Please tell me in Eclipse, how to treat C++ Compiler to have multi main file and work like on Java: it just find main on which file I run from.
#: I have multi main file just for debugging purpose. !!!
Thanks :)

unlike java in a C/C++ you can only have one main:
The main() function is special; normally every C and C++ program must
define it exactly once.
( source )
But: As a workaround you can use preprocessor to decide which of your main's you want to use.
Example:
#ifdef FIRST_MAIN
int main(int argc, char** argv)
{
printf("first");
return (EXIT_SUCCESS);
}
#elif SECOND_MAIN
int main(int argc, char** argv)
{
printf("second");
return (EXIT_SUCCESS);
}
#else
int main(int argc, char** argv)
{
printf("default");
return (EXIT_SUCCESS);
}
#endif
Now you can decide wich one you want to use by setting proper flag at compiletime. If none is set you'll use the default one (#else part).
Alternative: Exclude all your files containing a main-function from compilation and include only the one you need. However, this will only work if those files dont contain more than the main.

Related

LLVM | codegen for program entry function with command line options

For my programming language, the entry of a program is like C/C++ main function:
int main(int argc, char **argv) {
return 0
}
Suppose:
The IR code for main is generated into llvm::Function, using llvm::IRBuilder
I have llvm::LLVMContext and llvm::Module created
Question:
If I want to compile this code into binary prog, how do use llvm to generate the part that passes the commandline arguments to argc and argv and then call main, which are provided by users when running prog?
Thanks to arnt's comment:
If that function actually is called main() and actually has those arguments, then it should just work. If it isn't, then you'll need to generate a bridge function that actually is called main() and calls the function you want to be called, in the way you want it to be called. – arnt
It works!

Invalid amount of arguments

Sorry for such a noobie question, I have just never encountered this before, I look online but can only find descriptions of what argc, and argv are.
Anyway, I started a new win32 console project in visual studio 2012. I clicked empty project, and began by adding program.cpp with which the contents are the following:
#include <iostream>
#include <Windows.h>
int main(int argc, char** argv)
{
printf( "%d", argc );
getchar();
return 0;
}
I am planning to use arguments to make a program, which is why I need argc to be working properly. The code about outputs a huge number like '21374903'. In properties -> linker -> advanced I set the entry point to 'main'.
Is there anything I'm doing wrong? Can I add a setting to make it work properly?
In properties -> linker -> advanced I set the entry point to 'main'.
Don't do that. The entry point needs to be the C Runtime (CRT) entry point, which performs C initialization, obtains the arguments from the operating system, and calls your main function with them.
If you don't explicitly tell the linker what the entry point is named, it will automatically pick the right one. The CRT entry point that calls main is named mainCRTStartup (there are other CRT entry points with other names that call the other kinds of main functions supported by Visual C++).

Process argc and argv outside of main()

If I want to keep the bulk of my code for processing command line arguments out of main (for organization and more readable code), what would be the best way to do it?
void main(int argc, char* argv[]){
//lots of code here I would like to move elsewhere
}
Either pass them as parameters, or store them in global variables. As long as you don't return from main and try to process them in an atexit handler or the destructor of an object at global scope, they still exist and will be fine to access from any scope.
For example:
// Passing them as args:
void process_command_line(int argc, char **argv)
{
// Use argc and argv
...
}
int main(int argc, char **argv)
{
process_command_line(argc, argv);
...
}
Alternatively:
// Global variables
int g_argc;
char **g_argv;
void process_command_line()
{
// Use g_argc and g_argv
...
}
int main(int argc, char **argv)
{
g_argc = argc;
g_argv = argv;
process_command_line();
...
}
Passing them as parameters is a better design, since it's encapsulated and let's you modify/substitute parameters if you want or easily convert your program into a library. Global variables are easier, since if you have many different functions which access the args for whatever reason, you can just store them once and don't need to keep passing them around between all of the different functions.
One should keep to standards wherever practical. Thus, don't write
void main
which has never been valid C or C++, but instead write
int main
With that, your code can compile with e.g. g++ (with usual compiler options).
Given the void main I suspect a Windows environment. And anyway, in order to support use of your program in a Windows environment, you should not use the main arguments in Windows. They work in *nix because they were designed in and for that environment; they don't in general work in Windows, because by default (by very strong convention) they're encoded as Windows ANSI, which means they cannot encode filenames with characters outside the user's current locale.
So for Windows you better use the GetCommandLine API function and its sister parsing function. For portability this should better be encapsulated in some command line arguments module. Then you need to deal with the interesting problem of using wchar_t in Windows and char in *nix…
Anyway, I'm not sure of corresponding *nix API, or even if there is one, but google it. In the worst case, for *nix you can always initialize a command line arguments module from main. The ugliness for *nix stems directly from the need to support portability with C++'s most non-portable, OS-specific construct, namely standard main.
Simply pass argc and argv as arguments of the function in which you want to process them.
void parse_arg(int argc, char *argv[]);
Linux provides program_invocation_name and program_invocation_name_short.
Check out the "getoptlong" family of functions and libraries. These offer a structured way of defining the arguments your program expects and can then parse them readily for you. Can also help with the generation of documentation / help responses.
It's an old library in the UNIX world, and there is a .Net implementation in C# too. (+ Perl, Ruby, & probably more. Nice to have a single paradigm usable across all of these! Learn once, use everywhere!)

Microsoft C++ compiler is too annoying! Is there an alternative for compiling for Windows 32/64bit?

I am so frustrated with the Microsoft C++ compiler (not Visual). I am really annoyed at how it uses
int _tmain(
and
_TCHAR* argv[]
as well as many other things. I just want to use straight C++ like you would in GCC. Is there an alternative?
Thanks to all the responses. I was really just looking for a way of not using Microsoft Style code. This is what helped me -
Remove #include "stdafx.h"
Rename main method to -
int main (int argc, char* argv[])
{
return 0;
}
And it works!
You don't have to use _tmain() and TCHAR if you do not want to. Just create an empty project, add an empty source file to it and start writing
int main(int argc, char* argv[])
{
}
On VC++ 2010 Express you can just compile and execute it straight away. I don't remember if previous versions require that you set the project type.
You can try MinGW-32/64 for windows.
The _tmain/_TCHAR stuff is optional (you can read more about this on MSDN, Generic-Text Mappings in Tchar.h). You can delete the tchar.h file and #include from your project and directly use wmain/wchar_t (or main/char for ANSI) if you so choose.
You can get gcc for Windows. Look at the cygwin site. I know MS lets you use _tmain, TCHAR etc, but its the first I've heard that it actually requires you do to so, are you sure you're not missing some option somehere?
You can try gcw and MinGW but I have had varying results with both so YMMV...

Call Tiny C Compiler from a C++ code

I'm trying to compile a C code in a file from a program in C++. When I run my program it call the Tiny C Compiler and generate a dll from the compilation of c code.
I tried to do it by a lot of ways but I couldn't. Did anyone already do something like this?
Thanks
What platform are you on?
On most platforms, you can use the C standard library's system() function to launch a separate process from your C++ program.
#include <stdlib.h>
int main (int argc, char *argv[])
{
system ("tcc -o myproc a.c");
return 0;
}
This will block until the spawned process exits.
On Windows, if you're not concerned about portability, you can use CreateProcess().