こんばんは、stdio.hでgetline関数のカスタムバージョン(CLANG、OSのX)を実装する方法(ANSWER:コンパイルに使用変更POSIX標準)
私はカーニハンの中に演習を通して働いているとリッチーの古典を"Cプログラミング言語"
いくつかの場所で、演習では、標準ライブラリの関数の名前を複製する独自のバージョンの関数を作成します。私のバージョンの代替名を作成するのではなく、実際に私のバージョンの関数と標準ライブラリ関数を使用することをコンパイラーに伝えたいと思います。
具体的には、1-18行目の行末の空白とタブを入力行から削除するソリューションをコンパイルしようとすると、関数getlineを使ってstdinから行を読み込みます。残念ながら、getlineはstdio.hで定義されているため、コンパイラエラーが発生します。
私は#undefを使ってみましたが、うまく動作していないようです。
これまでに同様の質問がありましたが、[this one] [1]が見つかりました。しかし、それは私がむしろしない標準ライブラリのヘッダーをハッキングする必要があるようです。
ありがとうございます。
#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 1000
static size_t getline(char s[], size_t lim) {
char c;
size_t i = 0;
while (--lim > 0 && (c = (char)getchar()) != (char)EOF && c != '\n')
s[i++] = c;
if (c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
int main(void) {
char line[MAXLINE] = "";
size_t len = 0;
while ((len = getline(line, MAXLINE)) > 0)
if (len > MAXLINE)
printf("%s", line);
exit(EXIT_SUCCESS);
}
そして、私が手にエラーがある:
cc -std=c99 -Wall -g -I. -c -o obj/cleantrailsnblanks.o cleantrailsnblanks.c
cleantrailsnblanks.c:14:15: error: static declaration of 'getline' follows non-static declaration
static size_t getline(char s[], size_t lim) {
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/stdio.h:442:9: note: previous declaration is here
ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
^
cleantrailsnblanks.c:35:40: error: too few arguments to function call, expected 3, have 2
while ((len = getline(line, MAXLINE)) > 0)
~~~~~~~ ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/stdio.h:442:1: note: 'getline' declared here
ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
^
2 errors generated.
make: *** [obj/cleantrailsnblanks.o] Error 1
UPDATE 1
私が落とした後、ここで
は(息切れのために私のコメントを剥奪)のコードです私の定義から「静的」、エラーは次のように変わります:cc -std=c99 -Wall -g -I. -c -o obj/cleantrailsnblanks.o cleantrailsnblanks.c
cleantrailsnblanks.c:14:8: error: conflicting types for 'getline'
size_t getline(char s[], size_t lim) {
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/stdio.h:442:9: note: previous declaration is here
ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
^
cleantrailsnblanks.c:35:40: error: too few arguments to function call, expected 3, have 2
while ((len = getline(line, MAXLINE)) > 0)
~~~~~~~ ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/stdio.h:442:1: note: 'getline' declared here
ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
^
2 errors generated.
make: *** [obj/cleantrailsnblanks.o] Error 1
ANSWER
この議論を参照してください。Why do I get a "conflicting types for getline" error when compiling the longest line example in chapter 1 of K&R2?
溶液前stdio.hのを含めてこれらの2行を追加することである。
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200112L
#include <stdio.h>
これは、使用してコンパイルにコードを設定しますGNU getline()拡張が標準に追加されたPOSIX.1-2008標準とは対照的に、POSIX.1-2001標準です。
'static'修飾子を削除します。 –
関数の宣言が標準関数と同じであることを確認してください。だから '静的'を取り除く。 – Barmar
これは、エラーメッセージによって提供されるガイダンスであり、それでも動作しないので、あまりにもそれを試みました。私はその結果で私の質問を更新します。 – ptdecker