问题
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