Jump to content

  • Log In with Google      Sign In   
  • Create Account

Why Does Xcode Give My Application A Command Line Argument?

  • You cannot reply to this topic
7 replies to this topic

#1   Members   -  Reputation: 263

Like
0Likes
Like

Posted 27 July 2016 - 08:42 AM

So, today I played around a little with my main function and decided to print out the argv and argc arguments in the main function.

This resulted in the following being printed out : 

Commandline argument amount : 1.

1. /Users/name/Library/Developer/Xcode/DerivedData/Playground-bdassylsrksorocyjtssfnwfbkpg/Build/Products/Debug/Playground

Input anything to continue.
a
Program ended with exit code: 0

After the '1.' it prints out the command line argument, my question is why does it have that command line argument? And what is it used for?

 

Here's the code I used for this little program : 

#include <iostream>

int main(int argc, const char* argv[]) {
    
    std::cout << "Commandline argument amount : " << argc << "." << std::endl;
    
    for (int i = 0; i < argc; i++)
    {
        std::cout << std::endl;
        std::cout << i + 1 << ". " << argv[i] << std::endl;
    }
    
    std::cout << std::endl;
    std::cout << "Input anything to continue." << std::endl;
    std::string inpt;
    std::cin >> inpt;
    
    return 0;
}


#2   GDNet+   -  Reputation: 8878

Like
0Likes
Like

Posted 27 July 2016 - 08:52 AM

There is always at least 1 argument -- the first being the command used to invoke the program (if available, or empty if not).

A bit surprised that it isn't just the name of the application, but the entire filepath, but that might just be how Macs work?


Hello to all my stalkers.


#3   Moderators   -  Reputation: 10728

Like
1Likes
Like

Posted 27 July 2016 - 09:08 AM

That's how C++ works. Per the standard, if argc is non-zero argv[0] will be a pointer to the string that "represents the name used to invoke the program." The specifics of what that name looks like are not called out by the standard, but the presence of such a name is standard C++.

 

Xcode in particular launches your program via its absolute path.


Edited by Josh Petrie, 27 July 2016 - 01:40 PM.


#4   Members   -  Reputation: 263

Like
0Likes
Like

Posted 27 July 2016 - 09:11 AM

There is always at least 1 argument -- the first being the command used to invoke the program (if available, or empty if not).

A bit surprised that it isn't just the name of the application, but the entire filepath, but that might just be how Macs work?

What would happen if the argument would be empty? :P Nothing at all? The application wouldn't launch?



#5   Moderators   -  Reputation: 39694

Like
3Likes
Like

Posted 27 July 2016 - 09:16 AM

my question is why does it have that command line argument? And what is it used for?

 
It comes from standards that have been around since the 1970s.  Over history, many programs have used this for various reasons.
 
One reason is to change behavior based on the name of the program.  Note that decades ago storage space was extremely expensive. Many utilities were written with shared behavior, and instead of having unique programs with unique executables, a single executable with multiple symbolic links were used.  If the program were started with one name it would behave one way. If the program were started with another name it would be have another way.
 
Another reason is that it can provide additional information like the path to the program. These days there are environment variables that can also be provided to programs, or to use the concept of a program's working directory, but these did not exist originally. By passing the path to the program developers could use that to know where to look for related files, such as other executable programs.
 

What would happen if the argument would be empty? :P Nothing at all? The application wouldn't launch?



There can be zero or more arguments to programs. In C-based languages they are passed as arguments to main, traditionally called argc (argument count) and argv (argument variables).
 
Copying from one version of the standard:
 
— If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.
 
So yes, the count can be zero and argv[0] is undefined.  Otherwise the system is supposed to do all it can to provide the program name (whatever that means to the system) in argument argv[0].

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.


#6   Crossbones+   -  Reputation: 7959

Like
0Likes
Like

Posted 27 July 2016 - 01:32 PM

The POSIX standard requires the invoking environment pass the name of the program as the first argument.  Mac OS and Linux are both POSIX compliant.  If you test the program on Microsoft Windows, which is not POSIX compliant, you will find the first argument is usually empty.

 

This is not a language standard and not related to C++.


Stephen M. Webb
Professional Free Software Developer

#7   Crossbones+   -  Reputation: 8639

Like
0Likes
Like

Posted 27 July 2016 - 02:23 PM

If you test the program on Microsoft Windows, which is not POSIX compliant, you will find the first argument is usually empty.


Actually, on Windows it will be usually filled as expected as well. Especially if you launch it from the command line or Explorer. I think I managed to get an empty argv[0] once when launching a child process myself but that was mostly me not using it as intended. You certainly can end up without a valid argv[0] on Windows but I have never actually seen it in the wild.

#8   Moderators   -  Reputation: 39694

Like
0Likes
Like

Posted 27 July 2016 - 02:47 PM

It can happen on any platform if you invoke a program directly.  Typically it is a value filled out by the shell or the OS. 

The C language, languages derived from it, and the POSIX standard do require the first parameter to a program to be the program's name as described above, but it can be blank and the requirement is enforced as policy, not through technical means.


Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.






PARTNERS