MacOSX 10.6上的getline崩溃C编译器?

use*_*180 3 compiler-construction macos getline osx-snow-leopard

我很难安装一个R库,需要在C中进行一些编译.我正在使用Mac OSX Snow Leopard机器并试图安装这个R包(这里).

我已经看过谈论mac上的getline 的线程,并尝试了一些这些修复,但没有任何工作!我是新手,不知道任何C,所以这可能是原因!谁能给我一些关于如何修改这个包中的文件以使其安装的技巧?任何帮助都会受到怀疑!这是我得到的错误:


** libs
** arch - i386
g++ -arch i386 -I/Library/Frameworks/R.framework/Resources/include -I/Library/Frameworks/R.framework/Resources/include/i386  -I/usr/local/include   -D_FASTMAP -DMAQ_LONGREADS   -fPIC  -g -O2 -c bed2vector.C -o bed2vector.o
In file included from /usr/include/c++/4.2.1/backward/strstream:51,
                 from bed2vector.C:8:
/usr/include/c++/4.2.1/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
bed2vector.C: In function ‘int get_a_line(FILE*, BZFILE*, int, std::string&)’:
bed2vector.C:74: error: no matching function for call to ‘getline(char**, size_t*, FILE*&)’
make: *** [bed2vector.o] Error 1
chmod: /Library/Frameworks/R.framework/Resources/library/spp/libs/i386/*: No such file or directory
ERROR: compilation failed for package 'spp'
Run Code Online (Sandbox Code Playgroud)

Jer*_*man 5

最简单的解决办法可能是添加静态定义getline()bed2vector.c.这可能足够好了:

/* PASTE AT TOP OF FILE */
#include <stdio.h>   /* flockfile, getc_unlocked, funlockfile */
#include <stdlib.h>  /* malloc, realloc */

#include <errno.h>   /* errno */
#include <unistd.h>  /* ssize_t */

extern "C" ssize_t getline(char **lineptr, size_t *n, FILE *stream);

/* PASTE REMAINDER AT BOTTOM OF FILE */
ssize_t
getline(char **linep, size_t *np, FILE *stream)
{
  char *p = NULL;
  size_t i = 0;

  if (!linep || !np) {
    errno = EINVAL;
    return -1;
  }

  if (!(*linep) || !(*np)) {
    *np = 120;
    *linep = (char *)malloc(*np);
    if (!(*linep)) {
      return -1;
    }
  }

  flockfile(stream);

  p = *linep;
  for (int ch = 0; (ch = getc_unlocked(stream)) != EOF;) {
    if (i > *np) {
      /* Grow *linep. */
      size_t m = *np * 2;
      char *s = (char *)realloc(*linep, m);

      if (!s) {
        int error = errno;
        funlockfile(stream);
        errno = error;
        return -1;
      }

      *linep = s;
      *np = m;
    }

    p[i] = ch;
    if ('\n' == ch) break;
    i += 1;
  }
  funlockfile(stream);

  /* Null-terminate the string. */
  if (i > *np) {
    /* Grow *linep. */
      size_t m = *np * 2;
      char *s = (char *)realloc(*linep, m);

      if (!s) {
        return -1;
      }

      *linep = s;
      *np = m;
  }

  p[i + 1] = '\0';
  return ((i > 0)? i : -1);
}
Run Code Online (Sandbox Code Playgroud)

这不处理行超过ssize_t可以表示的最大值的情况.如果遇到这种情况,你可能会遇到其他问题.