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

标签 c visual-studio build command-line-arguments getopt

我正在尝试获取一组九个 *.c 文件(以及九个相关的 *.h 文件)以在 Windows 下编译。

代码最初是在 Linux 中设计的,使用标准 GNU-Linux/C 库“getopt.h”获取命令行参数。而且该库不适用于在 Windows 中构建 C 代码。

我想忽略我的代码现在所做的事情并提出以下问题。对于那些熟悉这个 C 库“getopt.h”的人:如果它依赖于 POSIX 样式的命令行参数,是否可以在 Windows 中构建和运行我的代码?或者我是否必须重新编写代码才能在 Windows 上工作,以不同的方式传递输入文件(并放弃“getopt.h”依赖项)?

最佳答案

getopt() 实际上是一个非常简单的函数。我做了一个github gist对于它,代码 from here也在下面

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

关于c - getopt.h : Compiling Linux C-Code in Windows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10404448/

相关文章:

c - 如何获取二元组最后一个元素以外的元素?

c - 将值无穷大存储到变量

c++ - 整数到 const c++, c

visual-studio - TFS 2010 Gated Check-in Build 给我留下了一个带有待处理更改的 .vspscc 文件

c# - 评论存储在哪里以及如何提取它们?

ios - 在 XCode 中为设备/模拟器编译不同的文件

转换为6位

c# - 转到仅显示公共(public)成员的类定义

Xcode 4 目标build设置 "Skip install"。它是什么?

java - Ant 作业为字符串密码字段添加转义字符