How command line arguments are passed ?

  • Thread starter Subhransu Sekhar Sahoo
  • Start date
S

Subhransu Sekhar Sahoo

Hi,

I have a very basic qus. An executable can be written using n number of
languages, viz C,,C++,Perl,Java,...And all these languages use different
calling convensions for example C uses cdecl and C++ uses fast call and so
on.
Now, my question how does the console window pass command line arguments to
the executable with out knowing whether it is a C or Java executable.

Thanking In Advance,
Subhransu Sahoo
 
K

Karl Heinz Buchegger

Subhransu said:
Hi,

I have a very basic qus. An executable can be written using n number of
languages, viz C,,C++,Perl,Java,...And all these languages use different
calling convensions for example C uses cdecl and C++ uses fast call and so
on.
Now, my question how does the console window pass command line arguments to
the executable with out knowing whether it is a C or Java executable.

It doesn't.
The 'console window' passes arguments in always the same way. The key
point is, that it doesn't pass those arguments to your main() (or the
equivalent in other languages) directly. Before main() runs other code
gets started, ( which eg. initializes global variables, sets up the
memory management etc. ) and this code knows about the way the
arguments from the console are passed and what main() expects.
 
S

Subhransu Sekhar Sahoo

Sorry, I I don't understand how the code that you are refering to knows
about the calling convention that main() follows.
 
T

Thomas Maier-Komor

Subhransu said:
Hi,

I have a very basic qus. An executable can be written using n number of
languages, viz C,,C++,Perl,Java,...And all these languages use different
calling convensions for example C uses cdecl and C++ uses fast call and so
on.
Now, my question how does the console window pass command line arguments to
the executable with out knowing whether it is a C or Java executable.

Thanking In Advance,
Subhransu Sahoo

executing a program does not mean calling main or whatever directly.
The whole thing is very plattform specific and each language has additionally
a wrapper around the main routine which adapts it to the runtime environment.
If you want to know more about this, you should read the documentation
for the linker (e.g. ld) of your development environment...

Tom
 
U

ulrich

Sorry, I I don't understand how the code that you are refering to knows
about the calling convention that main() follows.

the code knows that because it is created by the very same linker
("executable maker") which also processes the code resulting from your
main and other source code.

imagine this: a linker (or linker/compiler pair) is both platform and
programming language dependent. the latter dependency constitues what
source code can be processed, and the former dependency constitutes on
which platform the produced executables can be run.
 
K

Karl Heinz Buchegger

Subhransu said:
Sorry, I I don't understand how the code that you are refering to knows
about the calling convention that main() follows.

It knows it, because that code is part of your compiler system.
The very same guys that wrote your compiler also wrote that startup
code, so they know what to do with the passed arguments. The very
sam guys also know on which platform their compiler executes, so they
also know in which way the arguments are passed from the commmand line.

Just in case you haven't guessed it already: That startup code is part
of your executable. When the operating system starts your executable, that
startup code gets the control and gets the arguments passed.
The startup code reformats that arguments and in turn
calls main(), where you eventually get your hands at the arguments.
 
B

benben

This does actually not quite relate to the command line argument passing,
but rather about the runtime startup code. The question is does that piece
of startup code run ahead of or after the initialization of global objects
and static objects, or does the startup code actually calls those
constructors?

ben
 
K

Karl Heinz Buchegger

benben said:
This does actually not quite relate to the command line argument passing,
but rather about the runtime startup code. The question is does that piece
of startup code run ahead of or after the initialization of global objects
and static objects, or does the startup code actually calls those
constructors?

I would say: the later.
The startup code collects all global objects and calls constructors as
necessary (some code has to call them). How this is done exactly is highly
implementation specifc.

But make a thought experiment: Assume you write a compiler. How would you
do it? You can do anything you want and are not bound by the C++ rules.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top