Reputation: 120
I'm new to c++ and I've seen many c++ compilers automatically pass int argc,char* argv as parameters in main function but where are they defined? A little curious to know about.
Upvotes: 4
Views: 744
Reputation: 1
Please read a good C++ programming book, then see this C++ reference and some C++ standard like n3337 or better.
Your compiler (such as GCC) does not add argc
and argv
parameters to main
. With gcc -Wall -Wextra -g
it is checking that main
has these arguments (but you could define int main(void)
or int main(int argc, char**argv, char**environ)
on Linux). What is important is the signature of main
, not the exact name of the arguments (just their type and number).
Your operating system (e.g. Linux and its kernel) is passing these to your main
. On Linux, a program is started by execve(2) and that system call passes argument to main
. Technically, the initial layout of the call stack is specified in the ABI. Some crt0 initialization code is calling main
(and static constructors). That initialization code is written in assembler. Read a good OS textbook.
Sometimes, C++ is used in a freestanding mode. Then, there is no main
and other conventions apply. A typical example is when you code an operating system kernel in C++ (see OSDEV for examples)
Since GCC, the Linux kernel, and GNU libc are free software, you are allowed to download their source code and study it (and later improve it). See also LinuxFromScratch.
Study -for inspiration- the source code of existing C++ open source projects like Qt, FLTK, RefPerSys etc. For RefPerSys contact me by email to [email protected]
Upvotes: 6
Reputation: 124997
I'm new to c++ and I've seen many c++ compilers automatically pass int argc,char* argv as parameters in main function but where are they defined?
They’re part of the C and C++ standards. Exactly how they’re provided to the main()
function will depend on the operating system. In Unix-likesystems, for example, a combination of fork()
and some version of exec()
are used to create a new process and then load and execute your program in the context of that new process, with the arguments to the new program passed along. Other operating systems will start new processes other ways, but the argc
and argv
parameters will still come from the program that launched your program, whether that’s a command-oriented shell, some GUI desktop manager, or something else.
Upvotes: 1
Reputation: 211560
These are typically supplied by the shell, or more specifically, the parent process. That is the parent process can decide which, if any, parameters to pass through. These show up in argc
/argv
. See the exec
family of functions for examples of how this looks from the parent process perspective. There's also kernel versions of same for launching a process as well.
They're specifically defined and ultimately provided by the operating system and it's inter-process mechanics. This is part of the C++ standard that defines the main()
function.
This mechanism is inherited from the C version of same, and that's largely a byproduct of how UNIX itself was designed since C and UNIX were produced in tandem.
Upvotes: 5