标签:成员 返回值 mil 链接库 hang ble poi == time
本文翻译自:https://docs.microsoft.com/en-us/cpp/cpp/main-function-command-line-args?view=vs-2019
(除动态链接库dll,静态链接库lib工程外)所有的C++程序都必须有一个main函数。如果你编译一个没有main函数的C++exe工程,编译器会提示错误。main函数是你的代码开始执行的地方,但在main函数调用前,所有的没有被显示初始化的static类成员都被设置为零。在微软C++中,调用main函数前全局静态对象也会被初始化。main函数相对于其他函数而言有以下不同之处:
main函数并没有声明,因为它是语言内置的。如果有的话,其形式如下所示:
int main(); int main(int argc, char *argv[], char *envp[]);
如果你的代码文件使用的是Unicode wide character,你可以使用wmain,是main函数的款字符版本,其声明如下:
int wmain( ); int wmain(int argc, wchar_t *argv[], wchar_t *envp[]);
你还可以使用_tmain,该函数定义在tchar.h。_tmain被解释为main,除非你定义了_UNICODE(在这种情况下,_tmain被解释为wmain)。
如果main函数的返回值没有指定,编译器会提供一个值为零的返回值。另外,main wmain可以被声明为返回为void(没有返回值)。如果你将main wmain声明为void,name你就不能通过return语句将exit code返回给程序的父进程或系统。在main wmain是void的时候,你需要使用the exit function来返回exit code。
main或wmain的参数可以方便地在命令行中解析参数,并可以选择性地获取环境变量。argc argv的类型由语言进行定义。argc argv envp是典型的变量名称,但是你也可以任意命名:
int main( int argc, char* argv[], char* envp[]); int wmain( int argc, wchar_t* argv[], wchar_t* envp[]);
这些变量的定义如下:
注意:
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. The envp array, which is a common extension in many UNIX systems, is used in Microsoft C++. It is an array of strings representing the variables set in the user‘s environment. This array is terminated by a NULL entry. It can be declared as an array of pointers to char (char *envp[]
) or as a pointer to pointers to char (char **envp
). If your program uses wmain
instead of main
, use the wchar_t data type instead of char. The environment block passed to main
and wmain
is a "frozen" copy of the current environment. If you subsequently change the environment via a call to putenv
or _wputenv
, the current environment (as returned by getenv
or _wgetenv
and the _environ
or _wenviron
variable) will change, but the block pointed to by envp will not change. See Customizing Command Line Processing for information on suppressing environment processing. This argument is ANSI compatible in C, but not in C++.
// argument_definitions.cpp // compile with: /EHsc #include <iostream> #include <string.h> using namespace std; int main( int argc, char *argv[], char *envp[] ) { int iNumberLines = 0; // Default is no line numbers. // If /n is passed to the .exe, display numbered listing // of environment variables. if ( (argc == 2) && _stricmp( argv[1], "/n" ) == 0 ) iNumberLines = 1; // Walk through list of strings until a NULL is encountered. for( int i = 0; envp[i] != NULL; ++i ) { if( iNumberLines ) cout << i << ": " << envp[i] << "\n"; } }
Microsoft C / C ++启动代码在解释操作系统命令行上给定的参数时使用以下规则:
argv
)Command-Line Input | argv[1] | argv[2] | argv[3] |
---|---|---|---|
"abc" d e |
abc |
d |
e |
a\\b d"e f"g h |
a\\b |
de fg |
h |
a\\\"b c d |
a\"b |
c |
d |
a\\\\"b c" d e |
a\\b c |
d |
e |
例子:
// command_line_arguments.cpp // compile with: /EHsc #include <iostream> using namespace std; int main( int argc, // Number of strings in array argv char *argv[], // Array of command-line argument strings char *envp[] ) // Array of environment variable strings { int count; // Display each command-line argument. cout << "\nCommand-line arguments:\n"; for( count = 0; count < argc; count++ ) cout << " argv[" << count << "] " << argv[count] << "\n"; }
你可以使用通配符-问好(?)和星号(*)-用于在命令行上指定文件名和路径变量。
Command-line arguments are handled by a routine called _setargv
(or _wsetargv
in the wide-character environment), which by default does not expand wildcards into separate strings in the argv
string array. For more information on enabling wildcard expansion, refer to Expanding Wildcard Arguments.
Microsoft Specific
If your program does not take command-line arguments, you can save a small amount of space by suppressing use of the library routine that performs command-line processing. This routine is called _setargv
and is described in Wildcard Expansion. To suppress its use, define a routine that does nothing in the file containing the main
function, and name it _setargv
. The call to _setargv
is then satisfied by your definition of _setargv
, and the library version is not loaded.
Similarly, if you never access the environment table through the envp
argument, you can provide your own empty routine to be used in place of _setenvp
, the environment-processing routine. Just as with the _setargv
function, _setenvp
must be declared as extern "C".
Your program might make calls to the spawn
or exec
family of routines in the C run-time library. If it does, you shouldn‘t suppress the environment-processing routine, since this routine is used to pass an environment from the parent process to the child process.
END Microsoft Specific
标签:成员 返回值 mil 链接库 hang ble poi == time
原文地址:https://www.cnblogs.com/zyk1113/p/13228784.html