【问题标题】:getline on MacOSX 10.6 crashing C compiler?MacOSX 10.6 上的 getline 崩溃 C 编译器?
【发布时间】:2011-05-08 19:16:48
【问题描述】:

我很难安装需要在 C 中进行一些编译的 R 库。我正在使用 Mac OSX Snow Leopard 机器并尝试安装此 R 包 (here)。

我看过 the thread 谈论 mac 上的 getline 并尝试了其中一些修复,但没有任何效果!我是新手,不知道任何C,所以这可能就是原因!谁能给我一些关于如何修改此软件包中的文件以使其安装的提示? Anyhelp将不胜感激!这是我得到的错误:


** 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'

【问题讨论】:

    标签: macos compiler-construction osx-snow-leopard getline


    【解决方案1】:

    最简单的解决方案可能是将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);
    }
    

    这不处理行长于ssize_t 可以表示的最大值的情况。如果您遇到这种情况,您可能会遇到其他问题。

    【讨论】:

    • 谢谢谢谢谢谢!!!!这行得通,最后安装了所有东西!希望它有效(现在尝试!)
    • 我很高兴它成功了。如果我的帖子回答了您的问题,您应该单击它左侧的复选标记。请参阅* FAQ 中的the section "How do I ask questions here?" 了解更多信息。
    【解决方案2】:

    第零个问题:您是否考虑过使用像 finkMacPorts 这样的包管理器而不是自己编译?我知道 fink 有一个 R 包。

    第一个问题:如何管理 R 构建?有./configure 吗?如果是这样,你有没有看过它的选项?它使用make吗?斯康斯?其他一些依赖管理器?

    第二个问题:您是否告诉构建系统您正在使用 Mac?你能指定你没有带有原生 getline 的 libc 吗?

    如果构建系统不支持 Mac OS——但我认为 R 支持——你可能必须下载独立版本,然后破解构建以包含它。你如何做到这一点取决于构建系统。而且你可能需要破解一些源代码。

    【讨论】: