私はオンラインで見ており、正しい構文を得ることはできません。私は別のCソースファイルで関数を使うことができるようにヘッダファイルを作成しようとしています。この関数はreadLine()
と呼ばれ、myown.c
にあります。私はmyown3.c
でそれを使いたいです。ヘッダーファイルを使用して単純なCライブラリを作成する方法
ここでヘッダファイルmyown.h
さ:
#define LINECAP 81
#ifndef MYOWN_H
#define MYOWN_H
void readLine (char buffer [], LINECAP);
#endif
ここでは、ここでmyown.c
#include "myown.h"
#include <stdio.h>
void readLine (char buffer [], int lineCapacity) {
char myCharacter;
int i = 0 ;
do {
myCharacter = getchar();
buffer[i] = myCharacter;
i++;
} while (myCharacter != '\n' && i < lineCapacity);
buffer [i-1] = '\0';
}
void cleanBuffer (char buffer []) {
int i;
for (i=0; i<81; i++)
buffer[i] =0;
}
int main (int argc, char *argv[]) {
/* void readLine (char buffer [], int lineCapactiy); */
void cleanBuffer(char buffer []);
int i;
char line[81];
for (i=0; i<3; i++) {
//cleanBuffer(line);
readLine(line,LINECAP);
printf("%s", line);
//cleanBuffer(line);
}
}
である私は、関数を使用したいmyown3.c
、次のとおりです。
#include "myown.h"
#include <stdio.h>
#include <stdbool.h>
bool alphabetic (const char c) {
//if ((c > 65 && c < 90) || (c > 97 && c < 122))
if ((c > 'a' && c < 'z') || (c > 'A' && c < 'Z'))
return true;
else
return false;
}
int countWords (const char string[]) {
int i, wordCount =0;
bool lookingForWord= true, alphabetic (const char c);
for (i = 0; string[i] != '\0'; ++i) {
if (alphabetic(string[i])) {
wordCount++;
lookingForWord = false;
} else {
lookingForWord = true;
}
}
return wordCount;
}
int main (int argc, char *argv[]) {
char text[81];
int totalWords =0;
int countWords (const char string[]);
/* void readLine (char buffer[], int lineCapacity); */
bool endOfText = false;
printf ("Type in your text.\n");
printf ("Wehn you are done, press 'RETURN'.\n");
while (! endOfText) {
readLine (text, 81);
if (text[0] == '\0')
endOfText = true;
else
totalWords == countWords (text);
}
printf ("\nThere are %i words in the above text.\n", totalWords);
return 0;
}
私はコンパイルしようとすると、次のエラーが表示されます。
gcc -I. myown.c -o myown
In file included from myown.c:1:0:
myown.h:1:17: error: expected declaration specifiers or '...' before numeric constant
#define LINECAP 81
^
myown.h:4:33: note: in expansion of macro 'LINECAP'
void readLine (char buffer [], LINECAP);
^
make: *** [myown] Error 1
すべてが機能しました。私は部品を別々にコンパイルしてリンクする必要があることを理解するまでには時間がかかりました。
$ gcc -I. myown3.c -o myown3
C:\Users\sansari\AppData\Local\Temp\ccYfmI6U.o:myown3.c:(.text+0xd1): undefined reference to `readLine'
collect2.exe: error: ld returned 1 exit status
しかし、次のコマンドは、働いていた....
gcc myown.c -c -o myown.o
gcc myown3.c -c -o myown3.o
gcc myown3.o myown.o -o myown3
'readLine'の第2引数は何ですか?副読本に – fuz
、あなたは英数字のチェックのようなもののための標準的なライブラリを使用することができます。 'isalnum(文字)'。より多くのもののために 'ctype.h'をチェックしてください。 –
2つの一般的なコメントは、まずヘッダーファイルのインクルード保護でその#defineを定義します。再宣言の警告を避けるのに役立ちます。 第2に、関数プロトタイプで定数を使用していますが、これは関数の定義とは異なります。プロトタイプを一致させる( 'int'を使う)ようにしたいでしょう。 – engineer14