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.
Related
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!
I am trying to make a program on Windows 10 using Visual Studio 2015 that would sim-link certain files to certain locations. I am trying to make a text file with the location of the files, and the sim-link destination to use.
This is an example of the file data that would be in the properties.txt file:
FileLocation: "Z:\Folder\file.txt"
FileMkdirLocation: "Z:\Folder2\file.txt"
I want to use something like system(mkdir "sim-link_file_location" "file_location") by changing the data that is in properties.txt. I want to be able to add more than 1 file, without recompiling the program and writing each command for each file, one by one.
The problem is that I don't know how to make the commands use the data in the file.
EDIT: I managed to find out a way, but I get errors when compiling the program. I use this code:
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
using namespace std;
//initialization of Properties File used
ifstream PropertiesFile ("PropertiesFile.txt");
int main()
{
//initialization of variables used
int input_option;
char FileLocation[256], Command[]="mklink ";
// string FileLocation, Command;
PropertiesFile >> FileLocation;
/* switch (input_option)
{
case "add all mods":
}
*/
cout << "FileLocation: " << FileLocation;
cout << endl;
strcat(Command, FileLocation);
Command[strlen(FileLocation)] = '\0';
cout << Command;
cout << endl;
//system(command);
system("pause");
return 0;
}
I know that i haven't used all variables yet.
It tells me that "strcat" is deprecated and to use "strcat_s" instead, and when i replace it with that, I get
"Debug Assertion Failed - Expression: (L"Buffer is too small" && 0)"
I had to make the "Command" char bigger than "FileLocation" because then strcat_s would not be able to copy the content. After that the program worked fine, and there were no other Assert Errors.
The command to create a soft link in linux is: ln -s <source> <destination>
You can use this in a system(""); call, BUT before you continue in your code, you will have to make sure that the kernel finished executing this command.
After that you can read the link as if it was the original file.
When i run the .exe release file it shows some kind of awkward error in my IDE.
And when I try to run from outside the IDE as an appilcation it terminates as soon as it opens without showing any output.
I am using Eclipse and MinGw
This is the error message my ide shows.
And i guess there is nothing wrong with my code.
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World" << endl;
cout << "hello Again" << endl;
return 0;
}
This is not an error, you are viewing the contents of the exe file which is not human-readable, but binary data. The output from your application is in the 'Console' window at the bottom of the screen.
I'm using Ubuntu. g++ version 4.7.2.
Can anyone help me with the gnu-extension single list? I compiled the stuff here and got a core dump when executed.
I debuged it and saw the core dump happend in the first line, where it throwed an error that I cannot solve. May anyone please help me with that??
the error code:
std::allocator<char>::allocator (this=0x7fffffffe4d0)
at /build/buildd/gcc-4.7-4.7.2/build/x86_64-linux-gnu/libstdc++-v3/include/bits/allocator.h:104
104 /build/buildd/gcc-4.7-4.7.2/build/x86_64-linux-gnu/libstdc++-v3/include/bits/allocator.h: No such file or directory.
This is my test code:
#include <ext/slist>
#include <cstring>
#include <iostream>
int main(int argc, char * argv[])
{
__gnu_cxx::slist<char> li(&argv[1][0], &(argv[1][strlen(argv[1])]));
if(argc != 3)
return 1;
std::cout << "SList: ";
for(__gnu_cxx::slist<char>::iterator i = li.begin();
i != li.end();
++i)
std::cout << *i;
std::cout << std::endl;
li.remove(argv[2][0]);
for(__gnu_cxx::slist<char>::iterator i = li.begin();
i != li.end();
++i)
std::cout << *i;
std::cout << std::endl;
return 0;
}//main
My guess is that you're not giving any command-line arguments when you run it. It expects two: the character sequence to put in the list, and the character sequence to remove.
UPDATE: as mentioned in the comments, to pass the arguments to your program when using gdb, you need to use the --args option to indicate that arguments following the program name should be passed to the program, not to gdb itself:
gdb --args a.out xxyyxx x
^^^^^^
It initialises the list from the first argument argv[1] before checking that that argument exists; if it doesn't, then you'll get undefined behaviour. If you move the check above the declaration of li, then the program should exit with return code 1 instead in that case.
Then the debugger complains that it can't find the source file, and so can't show you on which source line it went wrong.
By the way, the C++ standard library now includes a singly-linked list, std::forward_list, defined in <forward_list>, which you could use instead of GNU's extension.
i have following code
#include <iostream>
using namespace std;
int main(int argc,char arg[]){
int a=arg[1];
int b=arg[2];
int c=a+b;
cout<<c<<endl;
return 0;
}
i am using windows 7 microsoft visual c++ 2010
how run it from command line?
Open a command prompt from the Start Menu. Use the CD command to change directories to where your exe is. type the name of your exe followed by the arguments.
foo.exe 1 2
or just
foo 1 2
Expect the output (once you've fixed your numerous code errors):
3
Once you compile this you get an executable. Navigate to the directory containing the executable and run it.
Go to google and look for a windows console tutorial. You need to start it from the console. Alternatively you can assign command line in the project properties. I'd recommend learning to do both.
BTW, this code almost certainly does not do what you think it does.
The compiled output of your program will be in the Debug or Release folder inside the solution folder (at least with default project settings). Just change to that directory and run the .exe file.
Open the Visual Studio Command Prompt (you can find it in the Start Menu)
cd to your source file directory
type:
cl.exe <your file name>.cpp
It will create a file .exe
Once your code is setup properly it would be something like this.
MyApp 2 3
Or similar
Navigate to the directory where the executable (.exe) is located. Then type the executable's name followed by two integer parameters.
C:\TestProg\> TestProg 5 6
The problems in your original example are corrected here:
#include <iostream>
#include <sstream>
int main(int argc, char *arg[])
{
std::stringstream sa;
std::stringstream sb;
int a;
int b;
int c;
if (argc >= 3)
{
// Convert string parameter into an integer.
sa.str(arg[1]);
sa >> a;
if (!sa)
{
return 1; // error
}
// Convert string parameter into an integer.
sb.str(arg[2]);
sb >> b;
if (!sb)
{
return 1; // error
}
}
else
{
return 1; // error
}
c = a + b;
std::cout << c << std::endl;
return 0;
}