two main in visual c++ - c++

how can I run two main Visual Studio (Visual C ++)..
I would like to have a main that represents the server and a main that is
the client and run them running on two different consoles.
how can I do?

It is possible to create two separate projects within a single Visual Studio solution. Each one can be an independent console application with its own main entrypoint. However, the simplest way to do that if you are wanting to debug both projects at the same time is to open two separate instances of Visual Studio, one with the client solution and one with the server.

Create two functions:
int server_main( int argc, char* argv[] );
int client_main( int argc, char* argv[] );
in the actual
int main( int argc, char* argv[] )
check for a command line argument ( --server or --client ) and then depending on which one is present, delegate to server_main or client_main.
When it comes to debugging, do what they've already suggested which is run two different instances of VS.
Everybody else is right in pointing out that there can be only one "main", but I think this answers what you actually wanted to ask.

You can't have two main functions. Either have separate builds with ifdef guards or a command line argument.

You have to create two separate programs. Each program will have its separate main() function.

Create a library with the shared code (assuming that's what you're after here) and create two separate binaries, one for the server and one for the client.

Related

Get command line arguments without arguments in main()

One strange thing took my sleep away. .
I have P7 library. It is library for writing logs.
Library contains few examples.
Example for C++ looks like:
int main(int i_iArgC, char* i_pArgV[])
{
// Some code that don't use i_iArgC or i_pArgV
}
But the trick that program handle command line arguments somehow.
I play a little bit to make sure that this main called.
What I do:
Build in debug and set breakpoint on main (to make sure that exactly
this main is called)
Changemain(int i_iArgC, char* i_pArgV[]) to main() (To make sure that no one use them)
I have no idea how it possible.
Here is minimal steps you can do to look on it by yourself:
Download P7 code from this page (link at top left)
Unzip archive
Run build.sh (It runs few makefiles in some order)
Execute Cpp_Example from Binaries folder
Execute again Cpp_Example /P7.Help to see that app react to command line arguments.
Most systems allow for getting the command line parameters without relying on main(). On Windows for example, you can use GetCommandLineW().
The library has non-portable code to do just that in Shared/Platforms/*/PProcess.h. A quick look at Windows_x86/PProcess.h shows that it uses GetCommandLineW() and the same file in Linux_x86/ reads /proc/self/cmdline.

Eclipse CDT: passing multiple program arguments with same file extension

I want to input a bunch of image files from the same folder and apply them the same operation inside a for-loop. I defined main as int main(int argc, char** argv) have this for-loop:
for(int i=1; i < argc; ++i)
{
// do something here
}
In Eclipse CDT (Neon), under Run configurations > Arguments, I'm entering the paths of images that I want to process. It works when I explicitly give a list of images like img1.jpg img2.jpg ... however it doesn't work when I give try to run it on all the image files with a certain extension such as dataset/*.jpg.
Is there a workaround for this? Thanks.
Is there a workaround for this?
The two most obvious ones that come to mind are:
Run the program from a terminal instead of from inside Eclipse.
Modify the program to take just the directory name as the argument, and have it iterate over the files in the directory.

Using Command Line Arguments To Input Parameters

So I have a Windows Studio VS File named RectArea that contains the code to a function to find the area of a rectangle. In another Windows Studio VS file named main1, I have my main function like this:
int main(int argc, char *argv[])
{
return 0;
}
How do I use command line arguments to be able to print the area of the rectangle in the main file as soon as i compile it?
I read so much stuff about it online. I just still don't understand what I put for the executable and the stuff after it.
My function for the rectangle's area is in the RectArea file. Do I type in C://RectArea?
It's under a lot more folders though.
Ah, you're confused on how to pass the arguments to the program, not how to interpret them. Those arguments are not command line arguments passed to the Visual Studio build process; they're arguments that can be changed each time you run the program.
An executable written with an entry point like that can't be run properly via double-click like all the others you've written; you have to open cmd.exe and run the program from there, typing the arguments after the program name separated by a space.

UnitTest++ and main

I want to give TDD a try and I've chosen the UnitTest++ framework, but the documentations is almost non-existent (to my knowledge).
My concern is this: in all the tutorials I've seen, they put UnitTest::RunAllTests() in the main() function. I'm guessing they do it only to simplify the explanation, but I wouldn't want that with my software. Where should I put UnitTest::RunAllTests() so that I can have it executed every time I build the software but not when I run it?
UnitTest::RunAllTests() should be put into the main function of a separate program, which you compile and run as part of your build process.
One thing we've done in the past is to add a command line argument which makes the main executable run all the tests and then exit. It's fairly easy to arrange some #ifdefs such that this code gets compiled out on release builds. Something like this (it's not very C++ but if you weren't parsing command line arguments already this is the simplest way to do it):
int main (int argc, char *argv[])
{
#ifdef DEBUG
if (argc > 1 && !strcmp(argv[2], "-t"))
{
return UnitTest::RunAllTests();
}
#endif
[rest of program]
}

How to deal with spaces within paths when using the system()?

I am still new to C++ and am working out a way to open a program within my C++ program.
The problem is that whenever I have spaces in my paths, it sees it as different parameters.
int _tmain(int argc, _TCHAR* argv[])
{
system("C:\\Users\\blah\\Desktop\\a\\ b.txt");
return 0;
}
The output I receive is:
'C:\Users\blah\Desktop\a\' is not recognized as an internal or external command, operable program or batch file.
You can double quote your string literal. Edit: Also just noticed that your backslashes were not escaped so updated below :P
system("\"C:\\Users\\blah\\Desktop\\a\\ b.txt\"");
Also let it be known for the record that you really shouldn't use system. Try fork, spawn, or perhaps even the unofficial boost.process class which has functionality similar to .NET process class depending on your needs. Also think about why you need launch a process from a process ... perhaps you could make a library?
On Unix, you could use fork() + exec().
On Windows, check out spawn.
These execute the program directly, avoiding the command shell interpreter, thus avoiding any special treatment of special characters like spaces.