Pass arguments to main function at compiling time - c++

I am a novice programmer learning C++ and the following question can be considered cross platform as I tried this both in Visual Studio 2010/12 and Qt Creator in Linux Mint.
I have set up the main() function of my program to accept command line arguments like this:
int main(int argc, char* argv[])
{
if(argc < 5)
{
printf("Not enough input parameters!\n");
printf("Usage:\n");
printf("'program' lamda1 lamda2 Attraction_Range Order_Param_Range\n");
return 0;
}
else
{
lamda1 = atof(argv[1]);
lamda2 = atof(argv[2]);
attRange = atof(argv[3]);
oRange = atof(argv[4]);
cout << lamda1 << lamda2 << attRange << oRange << endl;
}
}
However, when I hit Ctrl+F5 in Visual Studio or Ctrl+R in Qt Creator, to compile, the code forks to the if statement because I cannot input any parameters. Can I somehow pass initial arguments to my program, so that when it compiles, it can immediately go to the else part of the above example?
I am sorry if this is a duplicate/ wrong question, but I didn't even know how to search for this on the net.

In Visual Studio: How could I run a project with some parameters in Visual Studio?
In Qt Creator: QtCreator and Command Line Arguments

Related

SYCL program working using VS Debugger but not when running the .exe

I am trying to build and run a simple SYCL program from this book. Here it is:
#include <CL/sycl.hpp>
#include <iostream>
using namespace sycl;
const std::string secret {
"Ifmmp-!xpsme\"\012J(n!tpssz-!Ebwf/!"
"J(n!bgsbje!J!dbo(u!ep!uibu/!.!IBM\01"
};
const auto sz = secret.size();
int main(int argc, char* argv[]) {
queue Q;
char* result = malloc_shared<char>(sz, Q);
std::memcpy(result, secret.data(), sz);
Q.parallel_for(sz, [=](auto& i) {
result[i] -= 1;
}).wait();
std::cout << result << "\n";
return 0;
}
I am using Visual Studio 2019 and I am compilating with Intel oneAPI DPC++ 2022. If I run the Visual Studio Debugger, everything is working, I am obtaining as output:
"Hello World! I'm sorry, Dave. I'm afraid i can't do that. - HAL"
But if I am executing the .exe file that I just have built from the command prompt, nothing is happening... The program is executing itself, nothing is given as output, and I receive no error either. I tried to put some printf everywhere to see where to problem could come from. If I put a printf right just after "queue Q;" I wouldn't be able to see it when I run the .exe file.
From what I have read the problem comes from the initialization of my object Q. I replaced "queue Q;" by "queue Q(default_selector{});" but it didn't solve the problem.
EDIT : I have simply reduced the code to the following:
#include <CL/sycl.hpp>
#include <iostream>
using namespace sycl;
int main(int argc, char* argv[]) {
std::cout << "Beginning of the program.\n";
queue Q; // The problem appears to come from this line
std::cout << "End of the program.\n";
system("pause");
return 0;
}
Here is the output when I am launching the program in the Visual Studio Debugger:
> Beginning of the program.
> End of the program.
>
> Sortie de C:\Users\...\test.exe (processus 8108). Code : 0.
> Press any key to continue . . .
Here is the output when I am calling the .exe from the command prompt:
> C:\Users\...\Release>test.exe
> Beginning of the program.
>
> C:\Users\...\Release>
I have noticed that during the short time when the program is running in the command prompt (something like one second), I saw that the program Windows Problem Reporting ran in the Task Manager. Than it vanished as soon as the program apparently finished to compute.
EDIT 2 : Here is what happens if I am looking for the device used. With the following code:
#include <CL/sycl.hpp>
#include <iostream>
using namespace sycl;
int main(int argc, char* argv[]) {
default_selector device_selector;
std::cout << "default_selector has been defined.\n";
auto defaultQueue = queue(device_selector);
std::cout << "default_queue has been defined.\n";
std::cout << "Running on " << defaultQueue.get_device().get_info<info::device::name>() << "\n";
system("pause");
return 0;
}
I get this output from the Visual Studio debugger:
> Beginning of the program...
> default_selector has been defined.
> default queue has been defined.
> Running on Intel(R) Core(TM) i5-3337U CPU # 1.80GHz
> Press any key to continue...
And this when I am executing the .exe from the commande prompt (doing this in administrator or not doesn't change anything):
> C:\Users\...\Release>test.exe
> Beginning of the program...
> default_selector has been defined.
>
> C:\Users\...\Release>
The answer has been brought by the Intel Developer Software Forums.
Although the compiler has been well installed on my machine, the oneAPI environment had not be configured yet. This is why it couldn't work when running the .exe in the Windows command prompt.
I had to run the batch file setvars.bat that was at the adress C:\Program Files (x86)\intel\oneAPI then it worked!

How to put custom input for debugging in Visual Studio Code

In Visual studio code , i dont want to give long input to my program everytime. Is there a way like in hackerearth to copy paste my input and the program will run on them.
Take this as example.What should i write in args of launch.json. I am new to vscode.Here is my program
You have to change the main() as below:
int main(int argc, char* argv[])
{
for(int i = 1; i <argc; ++i)
std::cout<<argv[i];
return 0;
}
This is simple program.
You have to right-click the project, choose Properties, go to the Debugging section -- there is a box for "Command Arguments".
Specify the as "34 45 44".
You can run this again and again by mentioning it once.

Simple output loop fails in Visual Studio Code terminal when compiled with cl

Consider the simple program, in file a.cpp:
#include <iostream>
int main(int argc, char* argv[])
{
for (int i = 0; i < 10000; i++)
{
std::cout << i << std::endl;
}
}
It works fine when I run it in the Visual Studio Code (Powershell) terminal after compiling it on Windows with Cygwin g++, like so:
g++ a.cpp -o a.exe
However, I get strange and inconsistent behavior when I run it after compiling it with cl like so (in a separate program, the Developer Command Prompt for VS 2017):
cl a.cpp
When run in the Visual Studio Code (Powershell) terminal after compiling it this way, it stops outputting numbers well before 9999. Furthermore, it stops outputting at a different point each run. Running the program from a .bat script to check the %errorlevel% indicates that the error code is 0, so it doesn't seem to be crashing during the loop.
This, on the other hand, works totally fine from the VSCode terminal when compiled with cl, outputting 9999 every time:
#include <iostream>
int main(int argc, char* argv[])
{
int j = -1;
for (int i = 0; i < 10000; i++)
{
j++;
}
std::cout << j << std::endl;
}
So it seems like code compiled with cl will just stop outputting data written to std::cout after a little while when run from the Visual Studio Code terminal. But why would that happen?
Update
It works perfectly after compiling with cl when running from Developer Command Prompt for VS 2017, or from Powershell, or from Command Prompt. It only has the strange behavior when (a) compiled with cl instead of g++ and (b) run from a Powershell terminal within VSCode.

Visual Studio in debug mode restarts automatically after an exception is thrown (C++)

I'm working on a big C++ project which I haven't created, but the following short code fully represents a problem which I'm faced with:
#include <string>
#include <iostream>
int main(int argc, char **argv){
using namespace std;
try{
string str;
str = "r";
double d = stod(str);
cout << "'stod' worked! d = " << d << endl;
}
catch (invalid_argument){
cout << "I'm in 'catch'\n";
}
cout << "I'm after 'try-catch'\n";
cin.get();
cin.get();
return 0;
}
If I launch the code without debugger, everything works fine and as expected it gets into the catch-scope and then main() proceeds.
If I use the debugger, Visual Studio under 32-bit configuration restarts immediately or under 64-bit configuration I get a message: "The debugger's worker process (msvsmon.exe) unexpectedly exited. Debugging will be aborted."
I'm using VS2013 Ultimate on Windows 8.1 Professional.
Due to this post:
msvsmon.exe crashed when debugging
I have already installed Update 4. I have no breakpoints. In DEBUG->EXCEPTIONS the reset was done.
I also do not understand, what msvsmon.exe is. The Google-search combines it with remote debugger which confuses me a little bit, because I'm using the local debugger, at least I press each time the 'Local Windows Debugger' button.
Could anybody help me out?

int main() issue with argv parameter [duplicate]

This question already has answers here:
Debugging with command-line parameters in Visual Studio
(13 answers)
Closed 8 years ago.
EDIT: The following code is run through Microsoft Visual Studio 2013
I have the following script:
#include "stdafx.h"
#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cout << "Usage: tut1 path\n";
return 1;
}
std::cout << argv[1] << std::endl;
std::cout << "File Size is: " << file_size(argv[1]) << std::endl;
return 0;
}
But when I run it with ctrl+f5, I get this message (which is predicted by an if-condition in the code itself:
Usage: tut1 path
It seems the number of arguments is lower than 2.
Why this happens? How should I avoid this problem?
EDIT:
When I remove the following line:
std::cout << "File Size is: " << file_size(argv[1]) << std::endl;
I get the "Filing.cpp" printed on my console which means
argv[0] value is Filing.cpp that further shows argv is getting the commands from command arguments of Debuger of project correctly.
But when I add the line again, I see the message "Filing.exe not found or not built by the last incremental link;"
The easiest solution would be to open a prompt in the directory of your compiled output and call your program and pass in the string of the filename.
e.g. FileSize.exe foo.jpg
This saves messing about with project config options.
The if triggers because the application filename is considered the first argument, so argc == 1 which is less than 2, triggering the instructions.
If you are running it like this the number of arguments is only one (the executables name). If you are using Visual Studio (which you propably are) and you want to add arguments, go to properties->Debugging and add the arguments you want on "Command Arguments"
If you want to run a program with arguments please run the exe file by cmd.
Exe file would be in debug directory.
In cmd go to path of exe file then run command like ABC.exe then arguments.