c++ 未找到sdl2/sdl.h文件

5ssjco0h  于 2024-01-09  发布在  其他
关注(0)|答案(1)|浏览(203)

当我尝试在mac上使用vscode和clang++创建一个窗口时,我遇到了一个问题,安装了sdl2(使用自制软件),但vscode找不到它。这是我在终端中得到的错误。

  1. * Executing task: C/C++: clang build active file
  2. Starting build...
  3. /usr/bin/clang -std=gnu++14 -std=c++17 -stdlib=libc++ -g
  4. /Users/jst_/Coding/VS.Code_C++/Main.CPP -o
  5. /Users/jst_/Coding/VS.Code_C++/Main
  6. /Users/jst_/Coding/VS.Code_C++/Main.CPP:1:10: fatal error: 'SDL2/SDL.h' file not found
  7. #include <SDL2/SDL.h>
  8. ^~~~~~~~~~~~
  9. 1 error generated.
  10. Build finished with error(s).
  11. * The terminal process failed to launch (exit code: -1).
  12. * Terminal will be reused by tasks, press any key to close it.
  13. The code for the window is
  14. #include <SDL2/SDL.h>
  15. int main() {
  16. SDL_Init(SDL_INIT_VIDEO);
  17. SDL_Window* window = SDL_CreateWindow("My Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
  18. if (!window) {
  19. // Handle window creation failure
  20. SDL_Quit();
  21. return 1;
  22. }
  23. SDL_Delay(3000); // Pause for 3 seconds
  24. SDL_DestroyWindow(window);
  25. SDL_Quit();
  26. return 0;
  27. }

字符串
Tasks json是

  1. {
  2. "tasks": [
  3. {
  4. "type": "cppbuild",
  5. "label": "C/C++: clang build active file",
  6. "command": "/usr/bin/clang",
  7. "args": [
  8. "-std=c++17",
  9. "-stdlib=libc++",
  10. "-g",
  11. "${file}",
  12. "-o",
  13. "${fileDirname}/${fileBasenameNoExtension}"
  14. ],
  15. "options": {
  16. "cwd": "${fileDirname}"
  17. },
  18. "group": {
  19. "kind": "build",
  20. "isDefault": true
  21. },
  22. "detail": "Task generated by Debugger."
  23. },
  24. {
  25. "type": "cppbuild",
  26. "label": "C/C++: clang++ build active file",
  27. "command": "/usr/bin/clang++",
  28. "args": [
  29. "-std=c++17",
  30. "-stdlib=libc++",
  31. "-g",
  32. "${file}",
  33. "-I",
  34. "/opt/homebrew/opt/sdl2", // Replace this path with the correct one
  35. "-o",
  36. "${fileDirname}/${fileBasenameNoExtension}"
  37. ],
  38. "options": {
  39. "cwd": "${fileDirname}"
  40. },
  41. "group": "build",
  42. "detail": "Task generated by Debugger."
  43. }
  44. ],
  45. "version": "2.0.0"
  46. }


C_cpp_properties json是

  1. {
  2. "configurations": [
  3. {
  4. "name": "Mac",
  5. "includePath": [
  6. "${workspaceFolder}/**",
  7. "/opt/homebrew/opt/sdl2",
  8. "/opt/homebrew/Cellar/sdl2/2.28.5/include"
  9. ],
  10. "defines": [],
  11. "macFrameworkPath": [
  12. "/System/Library/Frameworks",
  13. "/Library/Frameworks"
  14. ],
  15. "compilerPath": "/usr/bin/clang",
  16. "cStandard": "c17",
  17. "cppStandard": "c++17",
  18. "intelliSenseMode": "clang-x64"
  19. }
  20. ],
  21. "version": 4
  22. }


我尝试了多次,以找到不同的路径,我finnaly找到了正确的路径,但它没有什么区别,我仍然得到了同样的错误。

bvn4nwqk

bvn4nwqk1#

在使用clang编译的Tasks.json中的第一个任务中,您忘记将路径添加到args中,这就是为什么您会收到错误,因为您正在使用clang编译,并且在该任务中,包含路径(SDL/sdl2.h所在的位置)丢失。

相关问题