简体   繁体   English

getopt.h:在 Windows 中编译 Linux C 代码

[英]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.我试图在 Windows 下编译一组九个 *.c 文件(和九个相关的 *.h 文件)。

The code was originally designed in Linux to take command line arguments using the standard GNU-Linux/C library "getopt.h".该代码最初在 Linux 中设计为使用标准 GNU-Linux/C 库“getopt.h”获取命令行参数。 And that library does not apply to building the C-code in Windows.该库不适用于在 Windows 中构建 C 代码。

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?对于那些熟悉这个 C 库“getopt.h”的人:如果我的代码依赖于 POSIX 风格的命令行参数,是否可以在 Windows 中构建和运行我的代码? Or will I have to re-write the code to work for Windows, passing input files differently (and ditching the "getopt.h" dependency)?或者我是否必须重新编写代码才能在 Windows 上工作,以不同的方式传递输入文件(并放弃“getopt.h”依赖项)?

getopt() is actually a really simple function. getopt()实际上是一个非常简单的函数。 I made a github gist for it, code from here is below too我为它制作了一个github 要点这里的代码也在下面

#include <string.h>
#include <stdio.h>

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 */
}

You are correct.你是对的。 getopt() is POSIX, not Windows, you would generally have to re-write all command-line argument parsing code. getopt()是 POSIX,而不是 Windows,您通常必须重新编写所有命令行参数解析代码。

Fortunately, there is a project, Xgetopt, that is meant for Windows/MFC classes.幸运的是,有一个项目 Xgetopt 是为 Windows/MFC 类设计的。

http://www.codeproject.com/Articles/1940/XGetopt-A-Unix-compatible-getopt-for-MFC-and-Win32 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.此外,它还带有一个很好的支持 GUI 的演示应用程序,您应该会发现它很有帮助。

Good luck!祝你好运!

There is a possibilty to use code from MinGW runtime (by Todd C. Miller):可以使用 MinGW 运行时的代码(Todd C. Miller):

http://sourceforge.net/apps/trac/mingw-w64/browser/trunk/mingw-w64-crt/misc 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):我用这些文件和 CMake 脚本创建了一个小库(可以生成一个 VS 项目):

https://github.com/alex85k/wingetopt https://github.com/alex85k/wingetopt

I did compile the getopt code under windows.确实在windows下编译了getopt代码。

I did this as I wanted to explicilty use its command line parsing functionality in a windows (command line) app.我这样做是因为我想在 Windows(命令行)应用程序中明确使用其命令行解析功能。

I successfully did this using VC2010 .我使用VC2010成功地做到了这VC2010

As far as I remember I ran into no significant issues doing so.据我所知,我这样做没有遇到任何重大问题。

getopt.c getoptl.c getopt.c getoptl.c

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.如果你只想在没有其他依赖的情况下在 Visual c++ 中使用 getopt,我从最新的 gnu libc 2.12 移植了 getopt.c,具有所有新功能。唯一的区别是你必须使用 TCHAR 而不是 char,但这是非常常见于windows。

simply download the source, make, copy libgetopt.lib and getopt.h getopt_int.h to your project.只需下载源代码,制作,将 libgetopt.lib 和 getopt.h getopt_int.h 复制到您的项目中。

you can also make it using CMakeList.txt in the root dir.你也可以在根目录中使用 CMakeList.txt 来制作它。

download the source from github从github下载源码

You might try looking into glib-2.0 as an alternative.您可以尝试研究 glib-2.0 作为替代方案。 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.老实说,我还没有尝试让它工作(我主要坚持使用 Linux),所以 YMMV。

Getting glib to work in windows: HowTo让 glib 在 Windows 中工作: HowTo

Oh, you might explore using mingw for the build environment, and visual studio for your IDE.哦,您可以探索将 mingw 用于构建环境,并为您的 IDE 使用 Visual Studio。

Glib for Win32: HowTo Win32 的 Glib:操作方法

Anywho, hope this helps.任何人,希望这会有所帮助。

From my reading of the documentation the header file getopt.h is specific to the GNU C library as used with Linux (and Hurd).根据我对文档的阅读,头文件 getopt.h 特定于与 Linux(和 Hurd)一起使用的 GNU C 库。 The getopt function itself has been standardised by POSIX which says it should be declared, along with optind optarg etc. in unistd.h getopt 函数本身已经被 POSIX 标准化,它说它应该在 unistd.h 中与 optind optarg 等一起声明

I can't try this on Visual Studio myself but it would be worth checking if unistd.h exists and declares this function as Visual Studio does provides some other POSIX functions.我自己无法在 Visual Studio 上尝试此操作,但值得检查 unistd.h 是否存在并声明此函数,因为 Visual Studio 确实提供了一些其他 POSIX 函数。

If not, then I'd definitely grab an implementation of getopt rather than re-write the argument parsing to work without it.如果没有,那么我肯定会抓住 getopt 的实现,而不是重新编写参数解析以在没有它的情况下工作。 Getopt was written to make things easier for the programmer and more consistent for user of programs with command line arguments.编写 Getopt 的目的是使程序员的工作更轻松,并使具有命令行参数的程序用户更一致。 Do check the license, though.不过,请检查许可证。

The getopt.h exists in git, I have download it and it works for me: getopt.h 存在于 git 中,我已经下载了它并且对我有用:

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

From what I remember of getopt.h , all it does is provide a handy parser for processing argv from your main function:根据我对getopt.h记忆,它所做的只是提供一个方便的解析器来处理来自主函数的 argv:

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. Windows 控制台程序仍然有一个 main 方法,因此您可以简单地循环遍历 argv 数组并自己解析参数。 eg例如

for ( int i = 1; i < argc; i++ )
{
  if (!strcmp(argv[i], "-f"))
    filename = argv[++i];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM