包含路径在VSCode中的C调试器中不起作用 – Raspberry Pi

huangapple go评论85阅读模式
英文:

Include path not working for C debugger in VSCode - Raspberry Pi

问题

你正在使用VS Code通过SSH远程连接到树莓派进行我的第一个C项目。代码可以成功编译,但调试器找不到头文件。我已经查看了现有的问题,但建议的解决方案未能解决我的问题。

如果我将头文件放在项目的根目录中,调试器就可以找到该文件。

源代码,第18行:

  1. #include "mfrc522.h"

该文件位于include文件夹中:
包含路径在VSCode中的C调试器中不起作用 – Raspberry Pi

tasks.json:

  1. {
  2. "tasks": [
  3. {
  4. "type": "cppbuild",
  5. "label": "C/C++: gcc build active file",
  6. "command": "/usr/bin/gcc",
  7. "args": [
  8. "-fdiagnostics-color=always",
  9. "-g",
  10. "${file}",
  11. "-o",
  12. "${fileDirname}/${fileBasenameNoExtension}",
  13. "--include-directory=${fileDirname}/**",
  14. "--include-directory=${fileDirname}/include/**",
  15. "--include-directory=${fileDirname}/src/*/*.c",
  16. "--include-directory=${fileDirname}/*.c"
  17. ],
  18. "options": {
  19. "cwd": "${fileDirname}"
  20. },
  21. "problemMatcher": [
  22. "$gcc"
  23. ],
  24. "group": {
  25. "kind": "build",
  26. "isDefault": true
  27. },
  28. "detail": "Task generated by Debugger."
  29. }
  30. ],
  31. "version": "2.0.0"
  32. }

c_cpp_properties.json

  1. {
  2. "configurations": [
  3. {
  4. "name": "Linux",
  5. "includePath": [
  6. "${workspaceFolder}/include/**",
  7. "${workspaceFolder}/**",
  8. "${workspaceFolder}/src",
  9. "${workspaceFolder}/src/*",
  10. "${workspaceFolder}/src/*/*.c"
  11. ],
  12. "browse": {
  13. "path": [
  14. "${workspaceFolder}/include/**",
  15. "${workspaceFolder}/**",
  16. "${workspaceFolder}/src",
  17. "${workspaceFolder}/src/*",
  18. "${workspaceFolder}/src/*/*.c"
  19. ]
  20. },
  21. "defines": [],
  22. "compilerPath": "/usr/bin/gcc",
  23. "cStandard": "c17",
  24. "cppStandard": "gnu++14",
  25. "intelliSenseMode": "linux-gcc-arm64"
  26. }
  27. ],
  28. "version": 4
  29. }

结果错误:

main.c:18:10: 致命错误: mfrc522.h: 没有那个文件或目录
编译已终止。

英文:

I am using VS Code remote via SSH to Raspberry Pi for my first C project. The code compiles without a problem, but the debugger can't find the header files. I have reviewed existing questions but the suggested solutions are not solving my problem.

If I place the header file in the root of my project, the file is found by the debugger.

Source, line 18:

  1. #include "mfrc522.h"

The file is available in include folder:
包含路径在VSCode中的C调试器中不起作用 – Raspberry Pi

tasks.json:

  1. {
  2. "tasks": [
  3. {
  4. "type": "cppbuild",
  5. "label": "C/C++: gcc build active file",
  6. "command": "/usr/bin/gcc",
  7. "args": [
  8. "-fdiagnostics-color=always",
  9. "-g",
  10. "${file}",
  11. "-o",
  12. "${fileDirname}/${fileBasenameNoExtension}",
  13. "--include-directory=${fileDirname}/**",
  14. "--include-directory=${fileDirname}/include/**",
  15. "--include-directory=${fileDirname}/src/*/*.c",
  16. "--include-directory=${fileDirname}/*.c"
  17. ],
  18. "options": {
  19. "cwd": "${fileDirname}"
  20. },
  21. "problemMatcher": [
  22. "$gcc"
  23. ],
  24. "group": {
  25. "kind": "build",
  26. "isDefault": true
  27. },
  28. "detail": "Task generated by Debugger."
  29. }
  30. ],
  31. "version": "2.0.0"
  32. }

c_cpp_properties.json

  1. {
  2. "configurations": [
  3. {
  4. "name": "Linux",
  5. "includePath": [
  6. "${workspaceFolder}/include/**",
  7. "${workspaceFolder}/**",
  8. "${workspaceFolder}/src",
  9. "${workspaceFolder}/src/*",
  10. "${workspaceFolder}/src/*/*.c"
  11. ],
  12. "browse": {
  13. "path": [
  14. "${workspaceFolder}/include/**",
  15. "${workspaceFolder}/**",
  16. "${workspaceFolder}/src",
  17. "${workspaceFolder}/src/*",
  18. "${workspaceFolder}/src/*/*.c"
  19. ]
  20. },
  21. "defines": [],
  22. "compilerPath": "/usr/bin/gcc",
  23. "cStandard": "c17",
  24. "cppStandard": "gnu++14",
  25. "intelliSenseMode": "linux-gcc-arm64"
  26. }
  27. ],
  28. "version": 4
  29. }

Resulting error:
> main.c:18:10: fatal error: mfrc522.h: No such file or directory
compilation terminated.

答案1

得分: 0

我之前没有看到bko这个答案,似乎已经解决了包含头文件的问题。

对我来说解决问题的方法就是只输入文件夹路径,不要加上/*或/**,然后它会提示我是否要为这个文件夹启用智能感知,我允许了并且它起作用了。

英文:

I had not seen this answer by bko earlier, which seems to have overcome the issue of including the header files.

> What solved it for me was to just type folder path without /* or /** Then it prompted me if I want to enable Intellisense for this folder and I allowed it and it worked.

huangapple
  • 本文由 发表于 2023年6月29日 11:55:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定