Call a C++ project main() in Python in Visual Studio? - c++

I have a C++ project and a Python project under one solution in Visual Studio. I am reluctant to modify the C++ project, because it is complicated and now complete. I don't want to touch on it any more. So to integrate them, I choose to call the C++ project in Python, instead of the other way round.
I wish to pass the parameters from Python to
int main(int argc, char** argv)
of the C++ project.
How may I do it?

The arguments of main() are the command-line arguments of the program. So if you do for example this in Python:
subprocess.Popen(['myCppprogram.exe', 'foo', 'bar'], ...)
then the following will hold in main():
int main(int argc, char** argv)
{
assert(argc == 3);
assert(argv[1] == std::string("foo");
assert(argv[2] == std::string("bar");
}

According to what i have understood from your question, you want to call a .exe file from python and pass arguments to the C++ file.
import subprocess
program = 'path to your exe file'
argument = "0"
subprocess.call([program, argument])
This will execute the .exe from python and the arguments passed can be read in C++ main as members of the array argv.

Related

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.

Get parameter from exe in C++

As we know the Query String in web. It's key/value go with the website URL ex: abc.com?myName=stack
For example in PHP, if we want get value of the myName, just do this $_GET['myName']
So, in C++, how can I get it?
in C# I pass an parametter to an *.exe file ( this exe file is C++ code ).
In C++ code, how to get this parametter value .
Build a console application with just the following code:
#include <iostream>
int main(int argc, char** argv)
{
for(int i = 1; i != argc; ++i )
{
std::cout << argv[i] << std::endl;
}
}
Assuming the name of the .exe is mytest.exe, execute it with some arguments, such as:
mytest.exe Hello there.
You should get the following output:
Hello
there.
Hope the simple example makes it clear as to how to process command line arguments in C++.
Have no idea about your situation, but surely you will have realize what parameters do you really need?
If you just need arguments from the command line, simple use the char** argv variable. In complicated cases you can use GNU getopt or even Boost::Program_options (the last is cross-platform);
If you are trying to access environmental variables, use standard getenv functions.

Kdevelop execute project with parameters

I am new to Kdevelop and I am trying for 2 hours to run a project based on input parameters in C++ style.
The code is here:
int main(int argc, char** argv)
{
std::string s = args[1]
std::cout<<s<<std::endl;
}
I am trying to add the parameter, but it is crashing and saying
Process Error - Kdevelop
A shell meta character was included in the atguments for file launch ...
Can anyone tell me what is it about? Andhow can I fix it, or where shall I add the execution parameters?
I have placed them in the Launch -> Configuration Launches -> Behaviour -> Arguments see below
Please help
The arguments must be between quotes:
"/your folder and path/Your file"
or
"enter your parameter here"
instead of
just the parameter

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

two main in visual 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.