当前位置: 技术问答>linux和unix
linux下的C程序getopt问题
来源: 互联网 发布时间:2016-03-13
本文导语: 我在linux下编了一个C程序,代码如下: #include #include int main(int argc,char **argv) { int ch; opterr = 0; while((ch = getopt(argc,argv,"a:bcde"))!= -1) switch(ch) { case 'a': printf("option a:%sn",optarg); break; case 'b': printf("optio...
我在linux下编了一个C程序,代码如下:
#include
#include
int main(int argc,char **argv)
{
int ch;
opterr = 0;
while((ch = getopt(argc,argv,"a:bcde"))!= -1)
switch(ch)
{
case 'a':
printf("option a:%sn",optarg);
break;
case 'b':
printf("option b :bn");
break;
break;
default:
printf("other option :%cn",ch);
}
printf("optopt +%cn",optopt);
}
编译的时候出现如下错误信息:
testopt.cpp: In function `int main(int, char**)':
testopt.cpp:7: `opterr' undeclared (first use this function)
testopt.cpp:7: (Each undeclared identifier is reported only once for each
function it appears in.)
testopt.cpp:8: `getopt' undeclared (first use this function)
testopt.cpp:12: `optarg' undeclared (first use this function)
testopt.cpp:22: `optopt' undeclared (first use this function)
make: *** [testopt.o] Error 1
怎么回事,unistd.h中不是包含了getopt的定义了吗。
#include
#include
int main(int argc,char **argv)
{
int ch;
opterr = 0;
while((ch = getopt(argc,argv,"a:bcde"))!= -1)
switch(ch)
{
case 'a':
printf("option a:%sn",optarg);
break;
case 'b':
printf("option b :bn");
break;
break;
default:
printf("other option :%cn",ch);
}
printf("optopt +%cn",optopt);
}
编译的时候出现如下错误信息:
testopt.cpp: In function `int main(int, char**)':
testopt.cpp:7: `opterr' undeclared (first use this function)
testopt.cpp:7: (Each undeclared identifier is reported only once for each
function it appears in.)
testopt.cpp:8: `getopt' undeclared (first use this function)
testopt.cpp:12: `optarg' undeclared (first use this function)
testopt.cpp:22: `optopt' undeclared (first use this function)
make: *** [testopt.o] Error 1
怎么回事,unistd.h中不是包含了getopt的定义了吗。
|
首先opterr未定义。
|
试试
#include
#include
|
感觉还是你的头文件所在的目录不是系统默认所在目录。
或者你的头文件就有问题。
连域变量opterr都未找到。
或者你的头文件就有问题。
连域变量opterr都未找到。
|
#include
int getopt(int argc, char * const argv[],
const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
#define _GNU_SOURCE
#include
int getopt_long(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
int getopt_long_only(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
longopts is a pointer to the first element of an array of struct option declared in
as
struct option {
const char *name;
int has_arg;
int *flag;
int val;
};
The meanings of the different fields are:
name is the name of the long option.
has_arg
is: no_argument (or 0) if the option does not take an argument,
required_argument (or 1) if the option requires an argument, or
optional_argument (or 2) if the option takes an optional argument.
flag specifies how results are returned for a long option. If flag is NULL, then
getopt_long() returns val. (For example, the calling program may set val to
the equivalent short option character.) Otherwise, getopt_long() returns 0,
and flag points to a variable which is set to val if the option is found,
but left unchanged if the option is not found.
val is the value to return, or to load into the variable pointed to by flag.
int getopt(int argc, char * const argv[],
const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
#define _GNU_SOURCE
#include
int getopt_long(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
int getopt_long_only(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
longopts is a pointer to the first element of an array of struct option declared in
as
struct option {
const char *name;
int has_arg;
int *flag;
int val;
};
The meanings of the different fields are:
name is the name of the long option.
has_arg
is: no_argument (or 0) if the option does not take an argument,
required_argument (or 1) if the option requires an argument, or
optional_argument (or 2) if the option takes an optional argument.
flag specifies how results are returned for a long option. If flag is NULL, then
getopt_long() returns val. (For example, the calling program may set val to
the equivalent short option character.) Otherwise, getopt_long() returns 0,
and flag points to a variable which is set to val if the option is found,
but left unchanged if the option is not found.
val is the value to return, or to load into the variable pointed to by flag.
|
Solaris手册是这样的:
[code=BatchFile]
Standard C Library Functions getopt(3C)
NAME
getopt - command option parsing
SYNOPSIS
SVID3, XPG3
#include
int getopt(int argc, char * const argv[], const char *opt-
string);
extern char *optarg;
extern int optind, opterr, optopt;
POSIX.2, XPG4, SUS, SUSv2, SUSv3
#include
int getopt(int argc, char * const argv[], const char *opt-
string);
extern char *optarg;
extern int optind, opterr, optopt;
[/code]
[code=BatchFile]
Standard C Library Functions getopt(3C)
NAME
getopt - command option parsing
SYNOPSIS
SVID3, XPG3
#include
int getopt(int argc, char * const argv[], const char *opt-
string);
extern char *optarg;
extern int optind, opterr, optopt;
POSIX.2, XPG4, SUS, SUSv2, SUSv3
#include
int getopt(int argc, char * const argv[], const char *opt-
string);
extern char *optarg;
extern int optind, opterr, optopt;
[/code]
|
#include
#include
int main(int argc,char **argv)
{
int ch;
int opterr = 0;
while((ch = getopt(argc,argv,"a:bcde"))!= -1)
switch(ch)
{
case 'a':
printf("option a:%sn",optarg);
break;
case 'b':
printf("option b :bn");
break;
default:
printf("other option :%cn",ch);
}
printf("optopt +%cn",optopt);
}
#include
int main(int argc,char **argv)
{
int ch;
int opterr = 0;
while((ch = getopt(argc,argv,"a:bcde"))!= -1)
switch(ch)
{
case 'a':
printf("option a:%sn",optarg);
break;
case 'b':
printf("option b :bn");
break;
default:
printf("other option :%cn",ch);
}
printf("optopt +%cn",optopt);
}
|
我画蛇添足了,opterr 是在getopt.h中定义的一个外部变量,不用再次定义了。
这个程序没问题,我试过了。getopt.h 在unistd.h中包含了。
这个程序没问题,我试过了。getopt.h 在unistd.h中包含了。
|
LZ,ubuntu6.06下可以运行,可能是你的系统中头文件的问题,比如找一下 这个文件是不是在
/usr/include下,如果是在/usr/include/unis文件夹下要写成等等
/usr/include下,如果是在/usr/include/unis文件夹下要写成等等