c++ SDL. h VSCode中没有此类文件或目录

ui7jx7zq  于 2023-08-09  发布在  Vscode
关注(0)|答案(3)|浏览(167)

我试图添加相关的"-I"path_to_your_SDL_include_directory"",如在几个类似的帖子中所概述的,如this one。我尝试了三种方法;,将其添加到tasks.json、Makefile和c_cpp_properties.json。
我的文件结构如下。我的main.cpp在MyProject/src中。我已经将SDL的include文件夹的所有内容复制到MyProject/lib/SDL2_lib/include,并将lib文件夹复制到MyProject/lib/SDL2_lib/libSDL2.dll存在于MyProject/lib/SDL2_lib中。
下面是一个可视化的总结以及我的代码。
x1c 0d1x的数据
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;
}

字符串
生成文件

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
}


尽管如此,我还是得到了错误;


感谢您的帮助!
编辑:我还应该补充一点,添加一个随机文件名而不是SDL.h,会给整个include语句加下划线,而不仅仅是结尾。所以很明显,VSCode知道它的存在,只是没有将它添加到程序中,这是我的猜测
Edit 2:从powershell运行make给出以下错误;

cigdeys3

cigdeys31#

很明显,你有两个问题。

  1. VSCode的C++扩展对文件SDL2.h产生了抱怨
    1.从Makefile编译时出现链接问题
    让我们首先解决Makefile问题:
    Makefile中有一个拼写错误,它显示的是SDL2_lib/libr,而不是SDL2_lib/lib
    修复后,必须添加要链接的库。“从技术上讲,您只需要libSDL2.la(假设为动态链接),因为您已经编写了自己的main()函数(因此不需要SDL2main)。
    因此,Makefile中的命令行应该如下所示(请注意,我是如何将项目的文件放在libs之前以确保正确加载符号的,这在使用中间文件时变得更加重要):
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:
我不认为你的配置有什么问题。您可能希望尝试切换到反斜杠(无论如何,这不会有什么区别)或(是的,真实的的)重新加载VSCode。

rdlzhqv9

rdlzhqv92#

我没有使用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

ulydmbyx

ulydmbyx3#

标志之间不应有空格,即“-I lib/SDL2_lib/include”应为“-Ilib/SDL2_lib/include”“-L lib/SDL2_lib/lib”应为“-Llib/SDL2_lib/lib”

相关问题