爲什麼我不能在vc 6.0中使用#include "getline.c"
或strindex.c
?在c中使用''#include'和這個鏈接錯誤?
-----test30src.c------
#include <stdio.h>
#include "getline.c"
#include "strindex.c"
#define MAXLINE 1000
char pattern[] = "ould";
int main()
{
char line[MAXLINE];
int found = 0;
while(getline(line, MAXLINE) > 0)
if(strindex(line, pattern) >= 0){
printf("%s", line);
found++;
}
return found;
}
------getline.c------
#include <stdio.h>
int getline(char s[], int lim)
{
int c, i;
i = 0;
while(--lim > 0 && (c = getchar()) != EOF && c != '\n')
s[i++] = c;
if(c=='\n')
s[i++] = c;
s[i] = '\0';
return i;
}
-----strindex.c-----
int strindex(char s[], char t[])
{
int i, j, k;
for(i = 0; s[i] != '\0'; i++){
for(j = i, k = 0; s[j] == t[k]; j++, k++)
;
if(k > 0 && t[k] == '\0')
return j;
}
return -1;
}
錯誤:
--------------------Configuration: test30 - Win32 Debug--------------------
Linking...
getline.obj : error LNK2005: _getline already defined in test30src.obj
strindex.obj : error LNK2005: _strindex already defined in test30src.obj
Debug/test30.exe : fatal error LNK1169: one or more multiply defined symbols found
你爲什麼'#include'-ING **。C **文件,而不是**。ħ**文件? –
我覺得'#include'只會複製'.c'文件?這樣對嗎? – Al2O3
getline.obj和strindex.obj不是必需的情況下這樣做。 – BLUEPIXY