简体   繁体   中英

undefined reference to `strtok_r' Windows 10 MinGW

i am always getting error while compile , it's saying undefined reference to `strtok_r' i am using MinGW in windows 10 , please help to solve this issue.

#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);
  };
}

Here is error and warning messages in 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

strtok_r is not a standard C method (it is POSIX). Your compiler may not be POSIX compliant and therefore its standard library might not contain the method. Look in your string.h file and see whether its prototype is declared there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM