getopt、getopt | 您所在的位置:网站首页 › getopt使用 › getopt、getopt |
默认的getopt、getopt_long是不可重入的,也就是说,假设你使用它开发交互式应用程序,循环获取用户命令,它只能正确解析第一次命令行输入。
你可以先看这个问题
http://topic.csdn.net/u/20080531/22/f058cc59-558e-4f21-9924-2889ca37746f.html
还有一个以前的帖子
http://topic.csdn.net/t/20050804/15/4188861.html
这个帖子给的解决方案发现并不能解决问题。
于是我决定把libc的源文件自己下载下来,编译链接到自己的程序里,另我惊喜的是,glibc-2.7,已经实现了它的可重入版本,于是我想直接使用这个可重入版本的函数_getopt_long_r。
ftp://sources.redhat.com/pub/glibc/releases/glibc-2.7.tar.gz
它的posix目录下有getopt.h getopt_int.h getopt.c getopt1.c
拷到你自己的项目文件夹下使用
使用时添加上
#include "getopt.h"
#include "getopt_int.h"
但是当我使用_getopt_long_r时,编译器老报,这个函数没有定义的错误(undefined),也就是说,我的库里面没有包含这个函数,其实我的/usr/include下根本没有getopt_int.h这个头文件。
于是我决定测试一下这个getopt.c和getopt1.c
gcc -c getopt.c
objdump -t getopt.c
结果发现
getopt.o: file format elf32-i386
SYMBOL TABLE:
00000000 l df *ABS* 00000000 getopt.c
00000000 l d .text 00000000 .text
00000000 l d .data 00000000 .data
00000000 l d .bss 00000000 .bss
00000000 l d .note.GNU-stack 00000000 .note.GNU-stack
00000000 l d .comment 00000000 .comment
说明编译出来的文件一个符号都没有,这样链接还是链接到c库,而c库里没有包含的函数,比如_getopt_long_r肯定是找不到了。
而假设你自己写个文件
//test.c
#include void fun(){ } int main(int argc, char *argv[]){ printf("test\n");} gcc -c test.c objdump -t test.o test.o: file format elf32-i386 SYMBOL TABLE:00000000 l df *ABS* 00000000 test.c00000000 l d .text 00000000 .text00000000 l d .data 00000000 .data00000000 l d .bss 00000000 .bss00000000 l d .rodata 00000000 .rodata00000000 l d .note.GNU-stack 00000000 .note.GNU-stack00000000 l d .comment 00000000 .comment00000000 g F .text 00000005 fun00000005 g F .te |
CopyRight 2018-2019 实验室设备网 版权所有 |