compiling c++ code using gnu/c getline() on mac osx?

余生颓废 提交于 2019-12-08 01:32:36

问题


I'm trying to compile a pre-existing c++ package on my mac osx leopard machine, and get the following error:

    error: no matching function for call to 'getline(char**, size_t*, FILE*&)'

This is probably because getline() is a GNU specific extension.

Can I somehow make the osx default g++ compiler recognize such GNU specific extensions?

(if not, I could always supply my own implementation or GNUs original one, but I prefer to have a "cleaner" solution if possible)


回答1:


getline is defined in stdio.h in glibc version 2.10 and later, but not in earlier versions, nor (so far; added 10.5 definitely didn't have getline, and 10.7 definitely does) in the BSD derived libc.

The change in the GNU libraries came about because of a change in the POSIX 2008 standard, which now includes getline.

Presumably, this will propogate to other libc over time. In the mean time, I understand that it is causing trouble for a lot of projects.

You can download a stand alone version from GNU.




回答2:


Generally, the solution is to use autoconf or some similar tool to determine whether the current platform already has getline or not, and supply your own definition only when needed.

Example:

# configure.ac
AC_CHECK_FUNCS([getline])

// compat.h
#include "config.h"
#ifndef HAVE_GETLINE
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
#endif

// getline.c
ssize_t getline(char **lineptr, size_t *n, FILE *stream) {
    /* definition */
}

# Makefile.am
program_LDADD = $(if $(HAVE_GETLINE),,getline.o)

or something along those lines, you'll have to adjust it to match your own program.




回答3:


I'm not sure I've ever seen that specific definition of 'getline' here is the one I know of which uses c++ streams+strings.

http://www.cplusplus.com/reference/string/getline/



来源:https://stackoverflow.com/questions/1117108/compiling-c-code-using-gnu-c-getline-on-mac-osx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!