Mac版Visual Studio运行C程序报错求助:无法打开stdio.h文件
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:
Get your GCC installation path:
which gccYou’ll get something like
/usr/local/bin/gcc—keep this handy.Get the full list of header search paths GCC uses:
gcc -vScroll 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.).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:
- Open your C file in VS Code, then press
Cmd + Shift + Pto open the Command Palette. - Search for and select C/C++: Edit Configurations (JSON). This will open the
c_cpp_properties.jsonfile. - Update the following sections with the paths you copied earlier:
Make sure to replace the placeholder paths with the exact ones you got from{ "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 }gcc -v.
Step 3: Configure Code Runner to Use GCC
By default, Code Runner might use Clang instead of GCC—let’s adjust that:
- Press
Cmd + ,to open VS Code Settings. - Search for Code-runner: Executor Map and click Edit in settings.json.
- Find the
"c"entry and update it to use GCC:
This tells Code Runner to compile with GCC (with warnings enabled) and run the resulting executable."c": "gcc -Wall -o $fileNameWithoutExt $fileName && ./$fileNameWithoutExt",
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

