简体   繁体   English

C ++如何将命令行参数转换为数组?

[英]How C++ turns command line arguments into an array?

I would like to how does C++ actually turn the command line arguments into a char array? 我想C ++如何将命令行参数转换为char数组? What "secret" code does this? 什么“秘密”代码这样做? Where can I view the code that does this (even if it is in assembly, I know some assembly)? 我在哪里可以查看执行此操作的代码(即使它在汇编中,我知道一些汇编)? I am using Linux, if that helps. 我正在使用Linux,如果这有帮助的话。

Thank You 谢谢

In most (all?) Unix-based operating systems, they are already an array. 在大多数(所有?)基于Unix的操作系统中,它们已经是一个数组。 It's how operating system executes a process there — when a process starts, there is already an array of arguments ready for it. 这就是操作系统在那里执行进程的方式 - 当进程启动时,已经有一系列参数可供它使用。

The code that turns command line into an array lives in a shell (like bash) or any other program that starts another program. 将命令行转换为数组的代码存在于shell(如bash)或启动另一个程序的任何其他程序中。 bash has it's sources available, other programs — it's different. bash有它的可用来源,其他程序 - 它是不同的。

In Windows, they are one string (which you can get unmodified using GetCommandLine() API call), which is parsed by C runtime library to turn it into an array, because the language specification requires them to come as an array. 在Windows中,它们是一个字符串(您可以使用GetCommandLine() API调用来修改它),它由C运行时库解析以将其转换为数组,因为语言规范要求它们作为数组出现。

For programs compiled with Visual C++, the code that does this is included into Visual Studio distributions. 对于使用Visual C ++编译的程序,执行此操作的代码包含在Visual Studio分发中。 You may have to turn on a checkbox that says something like “Include C runtime library source” in installer in order to have it installed. 您可能必须在安装程序中打开一个类似“包含C运行时库源”的复选框才能安装它。

This is usually handled by the operating system when it creates the process for the program. 这通常由操作系统在为程序创建进程时处理。 This code could very well be written in C (for example, if the OS is written in C), or it could be in assembly. 这段代码很好地用C语言编写(例如,如果操作系统是用C语言编写的),或者它可以在汇编中。 To find the code for this, you will probably have to look at the operating system code. 要查找此代码,您可能需要查看操作系统代码。

Hope this helps! 希望这可以帮助!

It's an OS job to manage command line arguments and to put it to the stack during process creation. 管理命令行参数并在进程创建期间将其放入堆栈是一项操作系统工作。

For POSIX systems the execution path is: 对于POSIX系统,执行路径是:

  1. in your program you call the execle/execve/... system call, passing the path to the new process executable and command line arguments. 在您的程序中,您调用execle / execve / ...系统调用,将路径传递给新的进程可执行文件和命令行参数。
  2. this data go to the kernel 这些数据转到内核
  3. the kernel updates its internal structures to take the new process identity into account and allocates the address space for the new process (also kernel purges the old address space if it's not needed anymore). 内核更新其内部结构以考虑新的进程标识并为新进程分配地址空间(如果不再需要,内核也会清除旧的地址空间)。 the kernel initializes the process memory with zeros, copying the info from old memory to the new address space at the top of the stack. 内核用零初始化进程内存,将信息从旧内存复制到堆栈顶部的新地址空间。
  4. the kernel puts the new process to the scheduling queue and returns from the exec() system call, transferring execution path to the userspace and eventually to the entry point of the process (this is usually routine from crt0.o object file, which is linked by default to every executable - this routine calls main() ). 内核将新进程放入调度队列并从exec()系统调用返回,将执行路径传递到用户空间并最终传递到进程的入口点(这通常是来自crt0.o目标文件的例程,它是链接的默认情况下,每个可执行文件 - 此例程调用main() )。

For Linux, you can see this code here: http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/fs/exec.c#L383 : 对于Linux,您可以在此处看到此代码: http//www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/fs/exec.c#L383

383 /*
384  * 'copy_strings()' copies argument/environment strings from the old
385  * processes's memory to the new process's stack.  The call to get_user_pages()
386  * ensures the destination page is created and not swapped out.
387  */

In the do_execve() kernel counterpart of userspace execve() syscall, at line 1345, copy_strings() 's are called, and copy_strings() routine actually does the job you're asking about. 在用户空间execve()系统调用的do_execve()内核对应部分,在第1345行,调用copy_strings()copy_strings()例程实际上完成了你所询问的工作。

It's part of the c runtime library. 它是c运行时库的一部分。 If you would like to know what that is, look here: What is the C runtime library? 如果你想知道那是什么,请看这里: 什么是C运行时库?

I just created a basic C++ console app using Microsoft Visual Studio and set a breakpoint on the first line of the program. 我刚刚使用Microsoft Visual Studio创建了一个基本的C ++控制台应用程序,并在程序的第一行设置了一个断点。 When debugged, the program stops on that line and you call look up the call stack to see the function that calls 'main'. 调试时,程序在该行停止,并且您调用查找调用堆栈以查看调用“main”的函数。 The calling function is part of the c runtime, and this does seem to contain some code that manipulates the command line... I haven't given it a close look, but that's probably where you should start. 调用函数是c运行时的一部分,这似乎包含一些操作命令行的代码......我没有仔细看过,但这可能就是你应该从哪里开始的。

Sometimes it is not the actual main() that is executed first. 有时它不是首先执行的实际main() For example, on Visual Studio it is the function mainCRTStartup() that serves as entry point that calls Windows API to retreive and parse the command line (you can see this if you use debugger). 例如,在Visual Studio上,函数mainCRTStartup()充当入口点,调用Windows API来检索和解析命令行(如果使用调试器,则可以看到这一点)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM