c++ - Cygwin 1.7.28 中的 mkstemp() 和 fdopen()

标签 c++ c gcc cygwin

在使用 GNU GCC 4.8.2 在 Cygwin(1.7.28-2,64 位)下构建一些基于 C++ 的代码的过程中,我遇到了以下错误:

...
SortDetails.cpp: In function ‘FILE* create_tmpfile(const char*, char**)’:
SortDetails.cpp:127:20: error: ‘mkstemp’ was not declared in this scope
   fd = mkstemp(tmpl);
                    ^
SortDetails.cpp:133:24: error: ‘fdopen’ was not declared in this scope
   fp = fdopen(fd, "wb+");
...

编译失败的具体代码块是:

FILE *
create_tmpfile(char const* path, char** fileName)
{
  FILE* fp;
  int fd;
  char* tmpl;

  if ( path == NULL )
      {
          fileName = NULL;
          return tmpfile();
      }

  tmpl = (char*)malloc(1 + strlen(path) + L_tmpnam);
  strcpy(tmpl, path);
  strcpy(tmpl+strlen(path), "/sb.XXXXXX");
  fd = mkstemp(tmpl);                        /* <----- here... */
  if(fd == -1)
      {
          fprintf(stderr, "unable to create temp file!\n");
          return NULL;
      }
  fp = fdopen(fd, "wb+");                    /* <----- ...and here */
  *fileName = (char*)malloc(strlen(tmpl) + 1);
  strcpy(*fileName, tmpl);
  free(tmpl);
  return fp;
}

(正在转换 malloc 的结果,因为此代码位于一个更大的基于 C++ 的项目中。)

回归

此代码在 Linux 主机上与 GNU GCC 4.8.x 以及 OS X 下的 Clang/++ 5.0 一起编译并成功运行。

环境

我正在使用以下版本的 Cygwin:

$ uname -a
CYGWIN_NT-6.1 CygFoo-PC 1.7.28(0.271/5/3) 2014-02-09 21:06 x86_64 Cygwin

这是我使用的 GCC 版本:

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-cygwin/4.8.2/lto-wrapper.exe
Target: x86_64-pc-cygwin
Configured with: /cygdrive/i/szsz/tmpp/cygwin64/gcc/gcc-4.8.2-2/src/gcc-4.8.2/configure --srcdir=/cygdrive/i/szsz/tmpp/cygwin64/gcc/gcc-4.8.2-2/src/gcc-4.8.2 --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --libexecdir=/usr/libexec --datadir=/usr/share --localstatedir=/var --sysconfdir=/etc --libdir=/usr/lib --datarootdir=/usr/share --docdir=/usr/share/doc/gcc --htmldir=/usr/share/doc/gcc/html -C --build=x86_64-pc-cygwin --host=x86_64-pc-cygwin --target=x86_64-pc-cygwin --without-libiconv-prefix --without-libintl-prefix --enable-shared --enable-shared-libgcc --enable-static --enable-version-specific-runtime-libs --enable-bootstrap --disable-__cxa_atexit --with-dwarf2 --with-tune=generic --enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable-graphite --enable-threads=posix --enable-libatomic --enable-libgomp --disable-libitm --enable-libquadmath --enable-math-support --enable-libssp --enable-libada --enable-libgcj-sublibs --disable-java-awt --disable-symvers --with-ecj-jar=/usr/share/java/ecj.jar --with-gnu-ld --with-gnu-as --with-cloog-include=/usr/include/cloog-isl --without-libiconv-prefix --without-libintl-prefix --with-system-zlib
Thread model: posix
gcc version 4.8.2 (GCC)

问题

  1. Cygwin 的 GCC 4.8.2 是否支持 mkstemp()fdopen()

  2. 如果没有,是否有我可以添加的包或我可以相对轻松编译的库来添加对这些功能的支持?

  3. 如果没有,是否可以使用 mkstemp()fdopen() 的替代方法在 Cygwin 下复制它们的功能?

可能的修复

这是此函数的修改版本:

FILE *
create_tmpfile(char const* path, char** fileName)
{
  FILE* fp;
  char* tmpl;

  if ( path == NULL )
      {
          fileName = NULL;
          return tmpfile();
      }

#if defined(__CYGWIN__) && !defined(_WIN32)
  const char *cygwinPrefix = "/sb.";
  const char *cygwinTmpDir = "/tmp";
  char *cygwinTmplSuffix = (char *)malloc(1 + L_tmpnam);
  tmpnam(cygwinTmplSuffix);
  tmpl = (char *)malloc(1 + strlen(path) + strlen(cygwinPrefix) + strlen(cygwinTmplSuffix + strlen(cygwinTmpDir) + 1));
  strcpy(tmpl, path);
  strcpy(tmpl+strlen(path), cygwinPrefix);
  strcpy(tmpl+strlen(path)+strlen(cygwinPrefix), cygwinTmplSuffix + strlen(cygwinTmpDir) + 1);
  fp = fopen(tmpl, "wbx+"); /* we add the 'x' extension to apply the O_EXCL flag, to avoid a security hole described in the GNU C library docs */
  free(cygwinTmplSuffix);
#else
  tmpl = (char*)malloc(1 + strlen(path) + L_tmpnam);
  strcpy(tmpl, path);
  strcpy(tmpl+strlen(path), "/sb.XXXXXX");
  int fd = mkstemp(tmpl);
  if(fd == -1)
      {
          fprintf(stderr, "unable to create temp file!\n");
          return NULL;
      }
  fp = fdopen(fd, "wb+");
#endif
  *fileName = (char*)malloc(strlen(tmpl) + 1);
  strcpy(*fileName, tmpl);
  free(tmpl);
  return fp;
}

这很丑陋。如果有办法使用 POSIX 函数,我想尽可能地使用它们。感谢您的任何建议。

最佳答案

使用 g++ 编译时4.8.2在Cygwin上,我记录了三种情况下宏的展开:

$ g++ -std=c++11 -E -Dd foo.cpp > foo.log.c++11
$ g++ -ansi -E -Dd foo.cpp > foo.log.ansi
$ g++ -E -Dd foo.cpp > foo.log.noFlag

比较日志很有用。 -std=c++11 中有“漏洞”和 -ansi例,而包含 mkstemp() 的 block 声明出现在“无旗帜”的情况下。这让我可以将标题中以不同方式处理的部分归零。

在文件中/usr/include/stdlib.h , 声明 mkstemp()如果__STRICT_ANSI__,一些其他功能将被拒绝已定义——例如当我们使用编译时标志 -ansi 时和 -std=c++11 .

同样,在文件 /usr/include/stdio.h 中, 声明 fdopen()出于同样的原因将被跳过。

C++ 头文件 <cstdlib><cstdio>两者都包括 stdlib.hstdio.h标题并将这两个函数(以及其他)的声明保留到这两个标题。所以如果我们使用 -ansi和/或 -std=c++11那么这两个函数将不会被声明,我们会得到编译错误。

似乎适用于玩具代码示例的解决方案是取消定义 __STRICT_ANSI__编译前:

$ g++ -std=c++11 -U__STRICT_ANSI__ foo.cpp

目前尚不清楚这会产生什么副作用,但从谷歌搜索来看,这似乎是一个常见问题,也是其他需要以 Cygwin 为目标的开发人员应用的常见修复方法。

关于c++ - Cygwin 1.7.28 中的 mkstemp() 和 fdopen(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21689124/

相关文章:

c - 如何确定环绕声音频信号的 channel 映射( channel 到轨道分配)

c++ - 大括号初始值设定项列表中是否允许显式转换运算符?

linux - pthread_create() : What is default priority and shceduling policy

c++ - C\C++ 中的简单翻转缓冲区(垂直)问题

c - 一群人在 c 中使用结构体?

c++ -/usr/../libstdc++.so.6 : version `GLIBCXX_3.4.11' not found (required by . ..)

c++ - 如何初始化这个填充有&functionA, &functionB 的数组?

c++ - 包含虚拟成员函数的类的大小

c++ - 防止自定义弃用或生成警告

c++ - C++ 字符串文字转义字符的规则