vscode中的sdl.h没有这样的文件或目录

发布于 2025-02-06 08:53:07 字数 3588 浏览 3 评论 0原文

I'm trying to add the relevant "-I"path_to_your_SDL_include_directory"" as outlined in several similar posts such as

我的文件结构如下。我的main.cpp在myproject/src中。我已经复制了SDL的所有内容。 sdl2.dll生活在myproject/lib/sdl2_lib中。

以下是视觉摘要以及我的代码。

main.cpp

#include <iostream>
#include <SDL.h>


const int WIDTH = 800, HEIGHT = 600;

int main( int argc, char *argv[] )
{
    SDL_Init( SDL_INIT_EVERYTHING );

    SDL_Window *window = SDL_CreateWindow( "Hello SDL WORLD", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI );

    if ( NULL == window )
    {
        std::cout << "Could not create window: " << SDL_GetError( ) << std::endl;
        return 1;
    }

    SDL_Event windowEvent;

    while ( true )
    {
        if ( SDL_PollEvent( &windowEvent ) )
        {
            if ( SDL_QUIT == windowEvent.type )
            { break; }
        }
    }

    SDL_DestroyWindow( window );
    SDL_Quit( );

    return EXIT_SUCCESS;
}

makefile

all:
    g++ -I lib/SDL2_lib/include -Llib/SDL2_lib/lib -o Main src/main.cpp

任务

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\MinGW\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-I lib/SDL2_lib/include",
                "-L lib/SDL2_lib/lib",
                "-lmingw32",
                "-lSDL2main",
                "-lSDL2",
                "-o",
                "${workspaceFolder}/bin\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "${workspaceFolder}/lib/SDL2_lib/include"
                        ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\MinGW\\bin\\g++.exe",
            "cStandard": "gnu11",
            "cppStandard": "c++14",
            "intelliSenseMode": "windows-gcc-x86",
            "configurationProvider": "ms-vscode.makefile-tools",
            "compilerArgs": [
                "-I lib/SDL2_lib/include",
                "-L lib/SDL2_lib/lib",
                "-lmingw32",
                "-lSDL2main",
                "-lSDL2"
            ]
        }
    ],
    "version": 4
}

png“ rel =” nofollow noreferrer“> ”

编辑:我还应该添加一个添加一个随机文件名而不是sdl.h强调整个Include语句,而不仅仅是结束。很明显,VSCODE确实知道它存在,它只是不会将其添加到程序中,这就是我猜测

Edit2:运行Make从PowerShell发出以下错误:

I'm trying to add the relevant "-I"path_to_your_SDL_include_directory"" as outlined in several similar posts such as this one. I have tried three approaches;, adding it to tasks.json, Makefile and c_cpp_properties.json.

My file structure is as follows. My main.cpp is in MyProject/src. I have copied all the contents of SDL's include folder to MyProject/lib/SDL2_lib/include and copied the lib folder to MyProject/lib/SDL2_lib/lib. SDL2.dll lives in MyProject/lib/SDL2_lib.

The following is a visual summary as well as my code.

File Structure

main.cpp

#include <iostream>
#include <SDL.h>


const int WIDTH = 800, HEIGHT = 600;

int main( int argc, char *argv[] )
{
    SDL_Init( SDL_INIT_EVERYTHING );

    SDL_Window *window = SDL_CreateWindow( "Hello SDL WORLD", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI );

    if ( NULL == window )
    {
        std::cout << "Could not create window: " << SDL_GetError( ) << std::endl;
        return 1;
    }

    SDL_Event windowEvent;

    while ( true )
    {
        if ( SDL_PollEvent( &windowEvent ) )
        {
            if ( SDL_QUIT == windowEvent.type )
            { break; }
        }
    }

    SDL_DestroyWindow( window );
    SDL_Quit( );

    return EXIT_SUCCESS;
}

Makefile

all:
    g++ -I lib/SDL2_lib/include -Llib/SDL2_lib/lib -o Main src/main.cpp

tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\MinGW\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-I lib/SDL2_lib/include",
                "-L lib/SDL2_lib/lib",
                "-lmingw32",
                "-lSDL2main",
                "-lSDL2",
                "-o",
                "${workspaceFolder}/bin\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "${workspaceFolder}/lib/SDL2_lib/include"
                        ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\MinGW\\bin\\g++.exe",
            "cStandard": "gnu11",
            "cppStandard": "c++14",
            "intelliSenseMode": "windows-gcc-x86",
            "configurationProvider": "ms-vscode.makefile-tools",
            "compilerArgs": [
                "-I lib/SDL2_lib/include",
                "-L lib/SDL2_lib/lib",
                "-lmingw32",
                "-lSDL2main",
                "-lSDL2"
            ]
        }
    ],
    "version": 4
}

Despite all this, I am getting the error:

enter image description here

Edit: I should also add that adding a random file name instead of SDL.h underlines the entire include statement instead of just the end. So clearly, VSCode does know it exists, its just not adding it to the program which is what I'm guessing

Edit2: Running make from powershell gives the following error:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

╰沐子 2025-02-13 08:53:09

显然,您有两个问题。

  1. VSCODE的C ++扩展程序抱怨文件SDL2.h
  2. 当您从makefile中编译时,有一个链接问题

,让我们首先解决makefile的内容:

您的makefile中有一个错字,它说sdl2_lib/code而不是<< 代码> sdl2_lib/lib 。

修复此操作后,您必须添加库以链接。从技术上讲,您只需要libsdl2.la(假设动态链接),因为您已经编写了自己的main()函数(因此您不需要sdl2main

代码

g++ -Ilib/SDL2_lib/include -Llib/SDL2_lib/lib src/main.cpp \
    -lmingw32 -lSDL2main -lSDL2 -mwindows \
    -o Main

> 请提供编译器输出(作为文本,而不是图像),但请勿在尝试此操作之前:

选项:指定整个库的文件名:

g++ -Ilib/SDL2_lib/include -Llib/SDL2_lib/lib src/main.cpp \
    -lmingw32 -lSDL2main.la -llibSDL2.la -mwindows \
    -o Main

选项二:静态(您不需要DLL)

g++ -Ilib/SDL2_lib/include -Llib/SDL2_lib/lib src/main.cpp \
    -lmingw32 lib/SDL2_lib/lib/libSDL2main.a lib/SDL2_lib/lib/libSDL2.a -mwindows \
    -o Main

关于VSCODE:

我不认为有您的配置有任何错误。

It is clear that you have two problems.

  1. The VSCode's C++ extension complains about the file SDL2.h
  2. There's a linking problem when you compile from your Makefile

Let's address the Makefile thing first:

There's a typo in your Makefile, it says SDL2_lib/libr instead of SDL2_lib/lib.

After fixing that, you must add the libraries to link with. Technically, you only need libSDL2.la (assuming dynamic linking) since you already wrote your own main() function (therefore you don't need SDL2main.

So, the command line in your Makefile should look like this (note how I put the project's files before the libs to guarantee symbols are loaded correctly, this becomes more important when using intermediate files):

g++ -Ilib/SDL2_lib/include -Llib/SDL2_lib/lib src/main.cpp \
    -lmingw32 -lSDL2main -lSDL2 -mwindows \
    -o Main

If that doesn't work, please provide the compiler output (as text, not image) but not before trying this:

Option one: Specify the whole library's filename:

g++ -Ilib/SDL2_lib/include -Llib/SDL2_lib/lib src/main.cpp \
    -lmingw32 -lSDL2main.la -llibSDL2.la -mwindows \
    -o Main

Option two: Link statically (you won't need the DLL)

g++ -Ilib/SDL2_lib/include -Llib/SDL2_lib/lib src/main.cpp \
    -lmingw32 lib/SDL2_lib/lib/libSDL2main.a lib/SDL2_lib/lib/libSDL2.a -mwindows \
    -o Main

About VSCode:

I don't think there's anything wrong with your configuration. You may want to try switching to backslashes (which should't make a difference anyway) or (yes, for real) reloading VSCode.

顾忌 2025-02-13 08:53:09

我没有使用Windows和Vscode,但我想您可以尝试

g++ -I lib/SDL2_lib/include -Llib/SDL2_lib/lib -lsdl -o Main src/main.cpp

g++ -I lib/SDL2_lib/include -Llib/SDL2_lib/lib -lsdl2 -o Main src/main.cpp

I haven't used Windows and VSCode but I guess you can try

g++ -I lib/SDL2_lib/include -Llib/SDL2_lib/lib -lsdl -o Main src/main.cpp

or

g++ -I lib/SDL2_lib/include -Llib/SDL2_lib/lib -lsdl2 -o Main src/main.cpp
橘虞初梦 2025-02-13 08:53:09

标志之间不应该有空间
IE
“ -i lib/sdl2_lib/include”应该是“ -ilib/sdl2_lib/include”
“ -l lib/sdl2_lib/lib”应为“ -llib/sdl2_lib/lib”

There shouldn't be space between flags
ie
"-I lib/SDL2_lib/include" should be "-Ilib/SDL2_lib/include"
"-L lib/SDL2_lib/lib" should be "-Llib/SDL2_lib/lib"

木緿 2025-02-13 08:53:09

MACOS

安装工具和LIB

brew install gcc
brew install sdl2
brew install sdl2_image

创建项目结构

mkdir -p game_app/{include,lib,src,build/debug}

cd game_app
cat << src/main.c << _EOF
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>

typedef struct App
{
  SDL_Window *window;
  SDL_Renderer *renderer;
} App;

int main(void)
{
  printf("Hello, world!\n");
  return EXIT_SUCCESS;
}

_EOF

code .

我的makefile

CC = LIBRARY_PATH+="/opt/homebrew/lib" /opt/homebrew/bin/gcc-13

BUILD_DIR = build/debug

SRC_DIR = src
SRC_FILES = $(wildcard $(SRC_DIR)/*.c)
OBJ_NAME = shooter
INCLUDE_PATHS = -I/opt/homebrew/Cellar/gcc/13.2.0/include/c++/13 -Iinclude
LIBRARY_PATHS = -L/opt/homebrew/Cellar/gcc/13.2.0/lib/gcc/13 -Llib
COMPILER_FLAGS = -std=c17 -Wall -Wextra -Werror -g
LINKER_FLAGS = -lSDL2 -lSDL2_image

all: $(SRC_FILES)
    $(CC) $(COMPILER_FLAGS) $(LINKER_FLAGS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(SRC_FILES) -o $(BUILD_DIR)/$(OBJ_NAME)

clean:
    rm -rf $(BUILD_DIR)/*

我的vscode配置

.vscode/settings.json file> file

{
  "C_Cpp.intelliSenseEngine": "Tag Parser",
  "C_Cpp.errorSquiggles": "disabled",
  "C_Cpp.enhancedColorization": "enabled",
}

> code> .vscode/c_cpp_properties。 json 文件

{
  "configurations": [
    {
      "name": "Mac",
      "compilerPath": "/opt/homebrew/bin/gcc-13",
      "intelliSenseMode": "macos-gcc-arm64",
      "includePath": [
        "${workspaceFolder}/**",
        "/opt/homebrew/Cellar/gcc/13.2.0/include/c++/13",
        "${workspaceFolder}/include"
      ],
      "defines": [],
      "macFrameworkPath": [
        "${workspaceFolder}/**",
        "/System/Library/Frameworks",
        "/Library/Frameworks"
      ],
      "cStandard": "c17",
      "cppStandard": "c++17",
      "configurationProvider": "ms-vscode.makefile-tools",
      "browse": {
        "path": [
          "/opt/homebrew/Cellar/gcc/13.2.0/include/c++/13",
          "${workspaceFolder}/include"
        ]
      }
    }
  ],
  "version": 4
}

.vscode/laining.json文件

{
  "version": "2.0.0",
  "configurations": [
    {
      "name": "Debug",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/build/debug/shooter",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "lldb",
      "preLaunchTask": "build"
    }
  ]
}

.vscode/tasks.json文件

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "cppbuild",
      "label": "build",
      "command": "make",
      "args": [],
      "options": {
        "cwd": "${workspaceFolder}"
      },
      "problemMatcher": [
        "$gcc"
      ],
      "group": "build",
      "detail": "Build our program using make"
    }
  ],
}

MacOS

Install the tools and libs

brew install gcc
brew install sdl2
brew install sdl2_image

Create project structure

mkdir -p game_app/{include,lib,src,build/debug}

cd game_app
cat << src/main.c << _EOF
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>

typedef struct App
{
  SDL_Window *window;
  SDL_Renderer *renderer;
} App;

int main(void)
{
  printf("Hello, world!\n");
  return EXIT_SUCCESS;
}

_EOF

code .

My Makefile

CC = LIBRARY_PATH+="/opt/homebrew/lib" /opt/homebrew/bin/gcc-13

BUILD_DIR = build/debug

SRC_DIR = src
SRC_FILES = $(wildcard $(SRC_DIR)/*.c)
OBJ_NAME = shooter
INCLUDE_PATHS = -I/opt/homebrew/Cellar/gcc/13.2.0/include/c++/13 -Iinclude
LIBRARY_PATHS = -L/opt/homebrew/Cellar/gcc/13.2.0/lib/gcc/13 -Llib
COMPILER_FLAGS = -std=c17 -Wall -Wextra -Werror -g
LINKER_FLAGS = -lSDL2 -lSDL2_image

all: $(SRC_FILES)
    $(CC) $(COMPILER_FLAGS) $(LINKER_FLAGS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(SRC_FILES) -o $(BUILD_DIR)/$(OBJ_NAME)

clean:
    rm -rf $(BUILD_DIR)/*

my vscode configuration

.vscode/settings.json file

{
  "C_Cpp.intelliSenseEngine": "Tag Parser",
  "C_Cpp.errorSquiggles": "disabled",
  "C_Cpp.enhancedColorization": "enabled",
}

.vscode/c_cpp_properties.json file

{
  "configurations": [
    {
      "name": "Mac",
      "compilerPath": "/opt/homebrew/bin/gcc-13",
      "intelliSenseMode": "macos-gcc-arm64",
      "includePath": [
        "${workspaceFolder}/**",
        "/opt/homebrew/Cellar/gcc/13.2.0/include/c++/13",
        "${workspaceFolder}/include"
      ],
      "defines": [],
      "macFrameworkPath": [
        "${workspaceFolder}/**",
        "/System/Library/Frameworks",
        "/Library/Frameworks"
      ],
      "cStandard": "c17",
      "cppStandard": "c++17",
      "configurationProvider": "ms-vscode.makefile-tools",
      "browse": {
        "path": [
          "/opt/homebrew/Cellar/gcc/13.2.0/include/c++/13",
          "${workspaceFolder}/include"
        ]
      }
    }
  ],
  "version": 4
}

.vscode/launch.json file

{
  "version": "2.0.0",
  "configurations": [
    {
      "name": "Debug",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/build/debug/shooter",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "lldb",
      "preLaunchTask": "build"
    }
  ]
}

.vscode/tasks.json file

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "cppbuild",
      "label": "build",
      "command": "make",
      "args": [],
      "options": {
        "cwd": "${workspaceFolder}"
      },
      "problemMatcher": [
        "$gcc"
      ],
      "group": "build",
      "detail": "Build our program using make"
    }
  ],
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文