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

gcc -I编译时_fread_nolock_s类型冲突问题求解

问题描述

我使用以下命令编译代码:

G:\cts\mingw64\bin\gcc -c G:\cts\test\10func-result\TestProject\test.c8\TestHarnessSource\test.c -I G:\cts\cts\include -I G:\cts\test\10func-result\TestProject\test.c8\TestCaseSource -w -g

编译时出现类型冲突错误:

In file included from G:/cts/mingw64/x86_64-w64-mingw32/include/stdio.h:1007:0,
                 from G:\cts\cts\include\json_tokener.h:15,
                 from G:\cts\cts\include\json.h:25,
                 from G:\cts\test\10func-result\TestProject\test.c8\TestCaseSource/_ts_main_0.c:5,
                 from G:\cts\test\10func-result\TestProject\test.c8\TestHarnessSource\_testSuite_defs_.h:1,
                 from G:\cts\test\10func\test.c:12:
G:/cts/mingw64/x86_64-w64-mingw32/include/sec_api/stdio_s.h:178:26: error: conflicting types for '_fread_nolock_s'
   _CRTIMP size_t __cdecl _fread_nolock_s(void *_DstBuf,size_t _DstSize,size_t _ElementSize,size_t _Count,FILE *_File);
                          ^
In file included from G:/cts/mingw64/x86_64-w64-mingw32/include/stdio.h:1007:0,
                 from G:\cts\test\10func\test.c:1:
G:/cts/mingw64/x86_64-w64-mingw32/include/sec_api/stdio_s.h:178:186: note: previous declaration of '_fread_nolock_s' was here

相关代码如下:
test.c:

#include<stdio.h>
int main(){
    printf("hello world\n");
 
    return 0;
}

json_tokener.h:

#ifndef _json_tokener_h_
#define _json_tokener_h_

#include <stdio.h>
#include "json_object.h"

#endif

已尝试更换新版本MinGW64,问题仍存在,求解决方法。


解决方案

1. 解决头文件重复包含问题

错误核心是stdio_s.h被重复引入,导致_fread_nolock_s声明冲突。可以:

  • 在编译命令中添加宏定义,关闭MSVC安全函数的重复声明:
    G:\cts\mingw64\bin\gcc -c G:\cts\test\10func-result\TestProject\test.c8\TestHarnessSource\test.c -D _CRT_SECURE_NO_WARNINGS -I G:\cts\cts\include -I G:\cts\test\10func-result\TestProject\test.c8\TestCaseSource -w -g
    
    或者添加-D _CRT_NONSTDC_NO_WARNINGS,两者任选其一即可。
  • 修改json_tokener.h,给<stdio.h>添加条件包含,避免重复引入:
    #ifndef _json_tokener_h_
    #define _json_tokener_h_
    
    #ifndef _STDIO_H_
    #include <stdio.h>
    #endif
    #include "json_object.h"
    
    #endif
    

2. 调整头文件包含优先级

当前编译命令中自定义头目录优先级高于系统目录,可能导致系统头文件加载异常。可以:

  • 使用-isystem替代-I来指定自定义头目录,将其视为系统头文件处理,避免冲突:
    G:\cts\mingw64\bin\gcc -c G:\cts\test\10func-result\TestProject\test.c8\TestHarnessSource\test.c -isystem G:\cts\cts\include -isystem G:\cts\test\10func-result\TestProject\test.c8\TestCaseSource -w -g
    
  • 或者将MinGW的系统头目录放在-I参数最前面,确保系统头文件优先加载:
    G:\cts\mingw64\bin\gcc -c G:\cts\test\10func-result\TestProject\test.c8\TestHarnessSource\test.c -I G:/cts/mingw64/x86_64-w64-mingw32/include -I G:\cts\cts\include -I G:\cts\test\10func-result\TestProject\test.c8\TestCaseSource -w -g
    

3. 检查测试框架自动生成文件

错误链指向_ts_main_0.c和_testSuite_defs_.h这类自动生成的测试文件,可能存在重复包含问题:

  • 打开_testSuite_defs_.h,检查是否有重复包含stdio.h的情况,若有则添加头文件保护(#ifndef ... #define ... #endif)。
  • 尝试重新生成测试框架的代码文件,排除自动生成过程中的异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 08:15:14