我总是在编译时遇到错误,它说的是未定义的引用`strtok_r'我在Windows 10中使用MinGW,请帮助解决这个问题。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv) {
char *test1, *test2;
char *state1, *state2;
char *cur1, *cur2;
test1 = malloc(20);
test2 = malloc(20);
strcpy(test1, "a,b,c,d");
strcpy(test2, "e,f,g,h");
cur1 = strtok_r(test1, ",", &state1);
cur2 = strtok_r(test2, ",", &state2);
while (cur1 != NULL && cur2 != NULL) {
if (cur1 != NULL) {
printf("cur1 : %s\n", cur1);
}
if (cur2 != NULL) {
printf("cur2 : %s\n", cur2);
}
cur1 = strtok_r(NULL, ",", &state1);
cur2 = strtok_r(NULL, ",", &state2);
};
}
以下是Windows 10中的错误和警告消息
strtok-test.c: In function 'main':
strtok-test.c:16:10: warning: implicit declaration of function 'strtok_r' [-Wimplicit-function-declaration]
cur1 = strtok_r(test1, ",", &state1);
^~~~~~~~
strtok-test.c:16:8: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
cur1 = strtok_r(test1, ",", &state1);
^
strtok-test.c:17:8: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
cur2 = strtok_r(test2, ",", &state2);
^
strtok-test.c:25:10: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
cur1 = strtok_r(NULL, ",", &state1);
^
strtok-test.c:26:10: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
cur2 = strtok_r(NULL, ",", &state2);
^
C:\Users\AppData\Local\Temp\ccvzdBJh.o: In function `main':
C:\Users\Desktop\New folder/strtok-test.c:16: undefined reference to `strtok_r'
C:\Users\Desktop\New folder/strtok-test.c:17: undefined reference to `strtok_r'
C:\Users\Desktop\New folder/strtok-test.c:25: undefined reference to `strtok_r'
C:\Users\Desktop\New folder/strtok-test.c:26: undefined reference to `strtok_r'
collect2.exe: error: ld returned 1 exit status
答案 0 :(得分:0)
strtok_r不是标准的C方法(它是POSIX)。您的编译器可能不符合POSIX,因此其标准库可能不包含该方法。查看string.h文件,看看它的原型是否在那里声明。