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

Mac版Visual Studio运行C程序报错求助:无法打开stdio.h文件

Fixing "stdio.h not found" Error in VS Code (Mac OS Catalina + Code Runner + GCC)

Hey there! I’ve dealt with this exact frustration on Catalina, so let’s get your C program up and running smoothly. The error you’re seeing is all about VS Code’s C/C++ extension not knowing where to find the system and GCC header files—here’s how to fix it step by step:

Step 1: Find Your GCC & System Header Paths

First, let’s grab the exact paths we need to add to VS Code’s configuration. Open Terminal and run these commands:

  1. Get your GCC installation path:

    which gcc
    

    You’ll get something like /usr/local/bin/gcc—keep this handy.

  2. Get the full list of header search paths GCC uses:

    gcc -v
    

    Scroll down to the section starting with #include <...> search starts here:. Copy all the paths listed (for example: /usr/local/lib/gcc/11/include, /usr/local/include, /usr/include, etc.).

  3. On Catalina, system headers are also stored in the Command Line Tools SDK, so add this path too if it isn’t listed:

    /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
    

Step 2: Configure VS Code’s C/C++ Extension

VS Code’s IntelliSense relies on the c_cpp_properties.json file to locate headers. Let’s edit it:

  1. Open your C file in VS Code, then press Cmd + Shift + P to open the Command Palette.
  2. Search for and select C/C++: Edit Configurations (JSON). This will open the c_cpp_properties.json file.
  3. Update the following sections with the paths you copied earlier:
    {
        "configurations": [
            {
                "name": "Mac",
                "includePath": [
                    "${workspaceFolder}/**",
                    "/usr/local/lib/gcc/11/include", // Replace with your GCC include path
                    "/usr/local/include",
                    "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include",
                    "/usr/include"
                ],
                "defines": [],
                "compilerPath": "/usr/local/bin/gcc", // Replace with your GCC path from `which gcc`
                "cStandard": "c17",
                "cppStandard": "c++17",
                "intelliSenseMode": "gcc-x64"
            }
        ],
        "version": 4
    }
    
    Make sure to replace the placeholder paths with the exact ones you got from gcc -v.

Step 3: Configure Code Runner to Use GCC

By default, Code Runner might use Clang instead of GCC—let’s adjust that:

  1. Press Cmd + , to open VS Code Settings.
  2. Search for Code-runner: Executor Map and click Edit in settings.json.
  3. Find the "c" entry and update it to use GCC:
    "c": "gcc -Wall -o $fileNameWithoutExt $fileName && ./$fileNameWithoutExt",
    
    This tells Code Runner to compile with GCC (with warnings enabled) and run the resulting executable.

Step 4: Test Your Code

Your code itself is totally fine—here it is formatted for clarity:

#include <stdio.h> 

int sum(int i, int j) {
    return i + j;
}

int main() { // Note: It's good practice to declare main as int main()
    int a, b;
    printf("Please enter two integers separated by a blank: ");
    scanf("%d %d", &a, &b); 
    printf("Entered %d and %d. Sum is %d\n", a, b, sum(a, b));
    return 0;
}

Save it, then click the Code Runner play button—this should compile and run without the stdio.h error now.

If you still run into issues, double-check that all paths in c_cpp_properties.json match exactly what gcc -v output, and that you’ve installed the Command Line Tools correctly with xcode-select --install.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 11:12:41