You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows 10下MinGW编译报错:undefined reference to `strtok_r'求助

Fixing "undefined reference to strtok_r" with MinGW on Windows 10

Let's break down why you're hitting these errors and walk through straightforward fixes to get your code compiling.

Why This Happens

strtok_r is a POSIX-standard function, not part of the core ANSI C library. MinGW, which targets Windows (a non-POSIX system), doesn't enable POSIX extensions like strtok_r by default. This causes two problems:

  1. The compiler can't find the function's declaration, leading to "implicit declaration" warnings.
  2. The linker can't locate the function's implementation, resulting in the "undefined reference" errors you see.

Solution 1: Enable POSIX Support in MinGW

You can force MinGW to include POSIX functions by defining the _POSIX_C_SOURCE macro—either in your code or during compilation.

Option A: Add the Macro to Your Code

Insert this line before any header includes in your C file:

#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Rest of your code stays exactly the same
int main(int argc, char **argv) {
    // ... your existing code ...
}

Option B: Define the Macro During Compilation

If you don't want to modify your code, pass the macro as a compiler flag when building:

gcc strtok-test.c -o strtok-test -D_POSIX_C_SOURCE=200809L

This tells MinGW to enable POSIX-compliant functions for this build.

Solution 2: Use Windows' Native Thread-Safe Alternative (strtok_s)

If you don't need cross-POSIX compatibility, Windows has its own thread-safe string tokenizer: strtok_s. It works almost identically to strtok_r and is natively supported by MinGW.

Modify Your Code to Use strtok_s

Replace all instances of strtok_r with strtok_s. The parameter order is exactly the same, so no other code changes are needed. You can add an optional macro to suppress secure-function warnings if they pop up:

#define _CRT_SECURE_NO_WARNINGS  // Optional: Suppresses secure function warnings
#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_s(test1, ",", &state1);  // Replaced strtok_r with strtok_s
    cur2 = strtok_s(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_s(NULL, ",", &state1);
        cur2 = strtok_s(NULL, ",", &state2);
    };
}

Compile normally with:

gcc strtok-test.c -o strtok-test

Verify the Fix

After applying either solution, your code should compile without errors and run as expected, printing the tokenized strings from both test1 and test2.

内容的提问来源于stack exchange,提问作者Rajesh Kummar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:54:25