Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly use system() to execute a command in C++?

I am new to C++ programming under Windows. I am trying to execute a command say cuobjdump in C++ code using the system() function:

system("C:\\program files\\nvidia gpu computing...\\cuobjdump.exe --dump-cubin C:\\..\\input.exe");

output:

Usage  : cuobjdump [options] <file>

This followed by the list of the options for cuobjdump.

When I execute this program I always get the cuobjdump help options displayed in the command line. It's as if the system call does not parse the filename. What am I doing wrong? I get the same result while using createprocess. The options --dump-cubin gives an error as if I mistyped it.

like image 300
ash Avatar asked Jul 12 '11 23:07

ash


People also ask

How does system () work in C?

System() Function in C/C++It is used to pass the commands that can be executed in the command processor or the terminal of the operating system, and finally returns the command after it has been completed. <stdlib. h> or <cstdlib> should be included to call this function.

How does system command work?

system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.

How do I run a system command in C++?

system() is used to invoke an operating system command from a C/C++ program. int system(const char *command); Note: stdlib. h or cstdlib needs to be included to call system.


2 Answers

Give a try with (that is, surrounding cuobjdump.exe path with ", properly escaped in C++ as \"):

system("\"C:\\program files\\nvidia gpu computing...\\cuobjdump.exe\" --dump-cubin C:\\..\\input.exe");
like image 138
Grzegorz Szpetkowski Avatar answered Sep 22 '22 13:09

Grzegorz Szpetkowski


system("cuobjdump --dump-cubin path\filename.exe");

That \f is interpreted by the compiler as a string escape sequence, try path\\filename.exe

like image 28
Ben Voigt Avatar answered Sep 26 '22 13:09

Ben Voigt