c linux windows.h,getopt.h: Compiling Linux C-Code in Windows

问题

I am trying to get a set of nine *.c files (and nine related *.h files) to compile under Windows.

The code was originally designed in Linux to take command line arguments using the standard GNU-Linux/C library \"getopt.h\". And that library does not apply to building the C-code in Windows.

I want to ignore what my code does right now and ask the following question. For those of you familiar with this C-library \"getopt.h\": will it be possible to build and run my code in Windows if it depends on POSIX-style command-line arguments? Or will I have to re-write the code to work for Windows, passing input files differently (and ditching the \"getopt.h\" dependency)?

回答1:

You are correct. getopt() is POSIX, not Windows, you would generally have to re-write all command-line argument parsing code.

Fortunately, there is a project, Xgetopt, that is meant for Windows/MFC classes.

http://www.codeproject.com/Articles/1940/XGetopt-A-Unix-compatible-getopt-for-MFC-and-Win32

If you can get this working in your project, it should save you a fair bit of coding and prevent you from having to rework all parsing.

Additionally, it comes with a nice GUI-enabled demo app that you should find helpful.

Good luck!

回答2:

getopt() is actually a really simple function. I made a github gist for it, code from here is below too

#include

#include

int opterr = 1, /* if error message should be printed */

optind = 1, /* index into parent argv vector */

optopt, /* character checked for validity */

optreset; /* reset getopt */

char *optarg; /* argument associated with option */

#define BADCH (int)'?'

#define BADARG (int)':'

#define EMSG ""

/*

* getopt --

* Parse argc/argv argument vector.

*/

int

getopt(int nargc, char * const nargv[], const char *ostr)

{

static char *place = EMSG; /* option letter processing */

const char *oli; /* option letter list index */

if (optreset || !*place) { /* update scanning pointer */

optreset = 0;

if (optind >= nargc || *(place = nargv[optind]) != '-') {

place = EMSG;

return (-1);

}

if (place[1] && *++place == '-') { /* found "--" */

++optind;

place = EMSG;

return (-1);

}

} /* option letter okay? */

if ((optopt = (int)*place++) == (int)':' ||

!(oli = strchr(ostr, optopt))) {

/*

* if the user didn't specify '-' as an option,

* assume it means -1.

*/

if (optopt == (int)'-')

return (-1);

if (!*place)

++optind;

if (opterr && *ostr != ':')

(void)printf("illegal option -- %c\n", optopt);

return (BADCH);

}

if (*++oli != ':') { /* don't need argument */

optarg = NULL;

if (!*place)

++optind;

}

else { /* need an argument */

if (*place) /* no white space */

optarg = place;

else if (nargc <= ++optind) { /* no arg */

place = EMSG;

if (*ostr == ':')

return (BADARG);

if (opterr)

(void)printf("option requires an argument -- %c\n", optopt);

return (BADCH);

}

else /* white space */

optarg = nargv[optind];

place = EMSG;

++optind;

}

return (optopt); /* dump back option letter */

}

回答3:

I did compile the getopt code under windows.

I did this as I wanted to explicilty use its command line parsing functionality in a windows (command line) app.

I successfully did this using VC2010.

As far as I remember I ran into no significant issues doing so.

getopt.c getoptl.c

回答4:

There is a possibilty to use code from MinGW runtime (by Todd C. Miller):

http://sourceforge.net/apps/trac/mingw-w64/browser/trunk/mingw-w64-crt/misc

I have created a small library with these files and CMake script (can generate a VS project):

https://github.com/alex85k/wingetopt

回答5:

You might try looking into glib-2.0 as an alternative. It would be a bit large for just needing an option parser. The up side would be having access to all the other wonderful toys in the glib.

Just to be honest, I haven't tried getting this to work (I stick mostly to Linux), so YMMV.

Getting glib to work in windows: HowTo

Oh, you might explore using mingw for the build environment, and visual studio for your IDE.

Glib for Win32: HowTo

Anywho, hope this helps.

回答6:

if you just want getopt to be used in visual c++ without other dependences, I have port the getopt.c from latest gnu libc 2.12, with all new features.The only difference is you have to use TCHAR instead of char,but This is very common in windows.

simply download the source, make, copy libgetopt.lib and getopt.h getopt_int.h to your project.

you can also make it using CMakeList.txt in the root dir.

download the source from github

回答7:

The getopt.h exists in git, I have download it and it works for me:

https://gist.github.com/ashelly/7776712

回答8:

From what I remember of getopt.h, all it does is provide a handy parser for processing argv from your main function:

int main(int argc, char * argv[])

{

}

Windows console programs still have a main method, so you can simply loop through your argv array and parse the parameters yourself. e.g.

for ( int i = 1; i < argc; i++ )

{

if (!strcmp(argv[i], "-f"))

filename = argv[++i];

}

来源:https://stackoverflow.com/questions/10404448/getopt-h-compiling-linux-c-code-in-windows

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值