getopt函数
头文件
#include "unistd.h"
gcc编译器使用
#include "<getopt.h>"
函数原型
int getopt(int argc, char * const argv[ ], const char * optstring);
前两个参数表示传入main()中的参数;第三个参数表示选项字符串。
全局变量
该函数设置了三个全局变量
optarg
——指向当前选项参数的指针;
optind
——再次调用该函数时下一个argv
指针的索引;
optopt
——最后一个已知的选项,可重复调用此函数,直到返回-1为止
用法
int main(int argc, char* argv[]){
int opt;
int threadNum = 4; //default number of thread