0

I am running a C++ program with argc and argv, when i run from the command line it passes in 12 args, the name of the exe and 11 other arguments. i pass the same arguemnt list in visual studio for example: ./euchre.exe pack.in shuffle 10 Alice Simple Bob Simple Cathy Simple Drew Simple

However when passing the same thing in the argument list in Visual Studio debugger, it also includes the file path as an argv at [0], making the total count 13. My program does not assume 13 arguments, so is there a way i can prevent VS from using the PATH as an argument?

EDIT: argv is the executable name with the file PATH Attached to the executable name : path/to/file/euchre.exe need to make sure that the PATH is not attached to the exe instead: ./euchre.exe, not sure if there is a way to do that

DanielJomaa
  • 497
  • 6
  • 21
  • 1
    There should be no difference. argv[0] should be the executable name in both cases (with or without a path). – drescherjm Feb 28 '19 at 17:05
  • 3
    Perhaps you errantly added `./euchre.exe ...` as your command line arguments in the debugger settings. The executable name does not belong to the command line arguments in that setting. In that case argv[1] would be `./euchre.exe` – drescherjm Feb 28 '19 at 17:08
  • what are `argv[0]` and `argv[1]` ? (@drescherjm is probably right) – bruno Feb 28 '19 at 17:10
  • argv[0] is users/name/path/to/file/euchre.exe argv[1] is ./euchre.exe – DanielJomaa Feb 28 '19 at 17:13
  • Note that there's no *guarantee* that a`agv[0]` is the program name (with or without path). It can in fact be any arbitrary string. If you need to find the path to or name of your executable, there are more reliable ways (OS dependent), like reading the /proc/self/exe symlink on Linux for example (and other methods for other Operating Systems). Don't *rely* on `argv[0]` (it *can* in fact be not even there). – Jesper Juhl Feb 28 '19 at 17:13
  • 1
    ***argv[0] is users/name/path/to/file argv[1] is ./euchre.exe*** Then fix you command line arguments in Visual Studio by removing the `./euchre.exe` part. The executable name is a separate setting. – drescherjm Feb 28 '19 at 17:14

2 Answers2

2

According to the C++ standard, (if arc > 0) argv[0] is either an empty string or it represents the name used to invoke the program. The exact representation is unspecified.

According to POSIX standard, the value in argv[0] should point to a filename string that is associated with the process being started by one of the exec functions.

Note that neither standard guarantee that the path is a relative one, like you would want it to be.

Given that command that argv[0] is some representation of the program name, if you simply pass 11 arguments to the program, then there will be argv[0]...argv[11] i.e. 12 total arguments in main. If you pass 12 arguments to the program, then there will be argv[0]...argv[12] i.e. 13 total arguments in main

Microsoft documentation says that argv[0] is by convention the command with which the program is invoked. Assuming this convention, you can achieve a relative path by using a relative path to execute the program. I don't know if it is possible to configure visual studio debugger to execute the program with a relative path to the working directory.

However, it would instead be preferable to not let your program depend on whether the program was run with a relative path (such as ./filename.exe), or using an absolute path. It is a bad design to rely on one or the other. For example, if you need the name of the executable without the directory that contains it, you can use std::filesystem::path::filename on argv[0]. This works whether the path is absolute or relative.

In the unconventional case where argv[0] is not a file name at all, the above won't work. There is no standard way in C++ to get the file name of the running process. You would need to rely on a OS specific way to get it.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • it is already passing in PATH as an argument, in the command line there are 12 args, in VS there are 13, argv[0] being the PATH. To be clear, i DONT want to include the path – DanielJomaa Feb 28 '19 at 17:11
  • sorry but this is not an answer, you take the risk of being DV by the surly, even this is probably not a real problem considering your reputation ^^ (you was just DV, this is not me -;) ) – bruno Feb 28 '19 at 17:14
  • @DanielJomaa regardless of what you want, you cannot avoid `argv[0]` being the command that was used to run the program. – eerorika Feb 28 '19 at 17:14
  • the issue is that the file path is attached to the executable name like: /path/to/file/euchre.exe where i only want the executable – DanielJomaa Feb 28 '19 at 17:16
  • "argv[0] is always the command that was used to execute the program" - No. That's flat out *false*. It's possible to launch a program with *any* arbitrary string in `argv[0]`. It's also possible to launch a program in a way where the *is no* `argv[0]` at all. And *nothing* in the C++ or POSIX standards guarantee you *anything* about it. – Jesper Juhl Feb 28 '19 at 17:16
  • @DanielJomaa you cannot suppose you get just the name of the executable or the pathname – bruno Feb 28 '19 at 17:16
  • 2
    @DanielJomaa No again the bug is a user error. You typed the wrong thing in the debugger command line arguments setting. In that setting should not pass the name of the executable as the first argument. You are setting argv[1] by doing so. – drescherjm Feb 28 '19 at 17:16
  • @DanielJomaa and my answer is that you should change your program so that it doesn't matter whether the program was run from the working directory or not. In this case, the debugger does not. – eerorika Feb 28 '19 at 17:17
  • @drescherjm yes i see that, removed, the problem still persists that is concatenates the file path to the executable name – DanielJomaa Feb 28 '19 at 17:19
  • 1
    After removing the errant `./euchre.exe` from your Visual Studio command line arguments debugger setting you should have the correct number of command line arguments. – drescherjm Feb 28 '19 at 17:20
  • ***the problem still persists that is concatenates the file path to the executable name*** Removing the path should be an easy exercise (either string manipulation or winapi functions or std::filesystem::path). That is if you really need to use `argv[0]` for some purpose. However that is somewhat different than the question you are asking about and it will have no effect on the number of command line arguments. There are other winapi ways to get the name of the executable if you want that. – drescherjm Feb 28 '19 at 17:22
  • 1
    @JesperJuhl I've modified the answer to include what guarantees are given by the standard(s). – eerorika Feb 28 '19 at 17:48
0

Strictly speaking the value of argv[0] is implementation defined, see this answer for a good explanation.

So you need to go to the Microsoft docs to to find out how this is implemented for Visual Studio. To quote the relevant section:

Note

By convention, argv[0] is the command with which the program is invoked. However, it is possible to spawn a process using CreateProcess and if you use both the first and second arguments (lpApplicationName and lpCommandLine), argv[0] may not be the executable name; use GetModuleFileName to retrieve the executable name, and its fully-qualified path.

Basically, the value of argv[0] will depend on how the program is invoked, and you can't really control how the debugger will invoke your program.

zdan
  • 28,667
  • 7
  • 60
  • 71