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

怎甘沉沦 提交于 2019-12-06 07:21:14

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.

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.

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/

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