本文是关于Vscode,gcc编译器下的引入头文件配置。
先说情况:
把其它源文件以及头文件和main分开文件夹
但编译器不知道我们的头文件路径,所以我们要告诉编译器文件路径
打开c_cpp_properties.json文件(如果找不到的,可以百度搜一下)
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/include" //在此插入"头文件路径",
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.22000.0",
"compilerPath": "E:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.34.31933/bin/Hostx64/x64/cl.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
}
${workspaceFolder},表示根目录名称。我这用的是相对路径,方便转移文件,你也可以用绝对路径。
配置完这里,它就知道头文件路径在那里了
接下来我们要把他们连接起来
打开tasks.json文件
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 生成活动文件",
"command": "C:\\mingw64\\bin\\gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"${workspaceFolder}\\include\\hello.c",//这里添加源文件路径让它链接起来一起编译
"-o",
"${fileDirname}\\exe\\${fileBasenameNoExtension}.exe",
"-I",
"${workspaceFolder}\\include", //在此插入:"-I","源文件路径",
"-fexec-charset=GBK",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
还有,设置.exe的路径
打开launch.json
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "gcc.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\exe\\${fileBasenameNoExtension}.exe",//这里是设置.exe的执行路径
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true,//true为打开外控制平台
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe 生成活动文件"
}
]
}
测试代码
main.c
#include <stdio.h>
#include <stdlib.h>
#include "hello.h"
int main()
{
Hello();
Hello2();
return 0;
}
hello.c
#include<stdio.h>
#include <stdlib.h>
void Hello(void)
{
printf("Hello World!\n");
printf("!\n");
// 防止运行后自动退出,需头文件stdlib.h
}
void Hello2(void)
{
printf("Hello World!2\n");
printf("!\n");system("pause");
}
hello.h文章来源:https://www.toymoban.com/news/detail-608797.html
#ifndef _HELLO_H_
#define _HELLO_H_
void Hello(void);
void Hello2(void);
#endif
文章来源地址https://www.toymoban.com/news/detail-608797.html
到了这里,关于VScode 引入头文件配置的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!