首页 > 解决方案 > 带有 SDL 库的 Visual Studio Code,无法构建。未定义的引用问题

问题描述

我想使用 Visual Studio Code 使用 Simple DirectMedia 层进行开发。我按照这些出色的说明为gcc设置了Visual Studio Codehttps://code.visualstudio.com/docs/cpp/config-linux。我使用薄荷 20.2。

我很快就能够在带有断点的 helloworld.cpp 中构建和使用 Visual C 和 GDB 调试器,但是当我将 helloworld.cpp 更改为使用 STL 的文件时,尝试添加 SDL2 构建失败。我无法再从“终端菜单=>运行构建任务”执行我的 VC 构建任务。这是文件。

#include <SDL2/SDL.h>
#include <stdio.h>

const int SCREEN_WIDTH = 640;   // Screen dimensions
const int SCREEN_HEIGHT = 480;

int main( int argc, char* args[] )
{
    SDL_Window* window = NULL;          // The window to render
    SDL_Surface* screenSurface = NULL;  // The window surface.

    if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() ); } // Initialize
    else {
        //Create window
        window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
        if( window == NULL ) {
            printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
        } else {
            screenSurface = SDL_GetWindowSurface( window ); // get window surface
            SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) ); // Fill the surface
            SDL_UpdateWindowSurface( window );              // update window surface
            SDL_Delay( 2000 );                              // wait two seconds
        }
    }
    SDL_DestroyWindow( window ); // destroy window
    SDL_Quit(); // quit SDL
    return 0;
}

单独的 Makefile 可以正常工作。

如果我尝试执行构建任务,这就是我得到的:

/usr/bin/g++ -g /home/kdog/Code/gcc/hello/helloworld.cpp -o /home/kdog/Code/gcc/hello/helloworld
/usr/bin/ld: /tmp/ccJXBwmX.o: in function `main':/usr/bin/ld: /tmp/ccJXBwmX.o: in function `main':   
/home/kdog/Code/gcc/hello/helloworld.cpp:12: undefined reference to `SDL_Init'                           
/usr/bin/ld: /home/kdog/Code/gcc/hello/helloworld.cpp:12: undefined reference to `SDL_GetError'           
/usr/bin/ld: /home/kdog/Code/gcc/hello/helloworld.cpp:15: undefined reference to `SDL_CreateWindow'

...

然后,列表在 10 个未定义的引用后停止。SDL 没有被链接,但我添加了 .vscode 配置目录文件,这个文件显示应该没问题?

c_cpp_properties.json:

{
"configurations": [
    {
        "name": "Linux",
        "includePath": [
            "${workspaceFolder}/**","/usr/include/SDL2"
        ],
        "defines": [],
        "compilerPath": "/usr/bin/gcc",
        "cStandard": "gnu17",
        "cppStandard": "gnu++14",
        "intelliSenseMode": "linux-gcc-x64"
    }
],
"version": 4

}

我添加了,"/usr/include/SDL2"

启动.json:

"version": "0.2.0",
"configurations": [
    {
        "name": "g++ - Build and debug active file",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/${fileBasenameNoExtension}",
        "args": [],
        "stopAtEntry": true,
        "cwd": "${fileDirname}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ],
        "preLaunchTask": "C/C++: g++ build active file ver(1)",
        "miDebuggerPath": "/usr/bin/gdb"
    }
]

}

任务.json

{
"version": "2.0.0",
"tasks": [
    {
        "type": "cppbuild",
        "label": "C/C++: g++ build active file",
        "command": "/usr/bin/g++",
        "args": [
            "-g",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}",
            "-lSDL2",

        ],
        "options": {
            "cwd": "${fileDirname}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": "build",
        "detail": "compiler: /usr/bin/g++"
    }
]

}

请帮忙。我错过了什么?我想我需要包括(LINKER_FLAGS = -lSDL2)?它在我的 Makefile 中有效。我想使用 VS 代码进行 GDB 调试。如果我只想构建,我的 Makefile 可以正常工作。

标签: c++visual-studio-codesdlvscode-settingsundefined-reference

解决方案


改变方向。我将此CMakeLists.txt文件添加到我有一个示例项目的文件夹中。该项目称为showpic。窗口上的关闭控件“X”有效:

cmake_minimum_required(VERSION 3.7)
project(Xout)

find_package(SDL2 REQUIRED)
include_directories(Xout ${SDL2_INCLUDE_DIRS})

add_executable(Xout src/xout.cpp)
target_link_libraries(Xout ${SDL2_LIBRARIES})

添加“CMake Tools Extension for Visual Studio Code”插件以及 C/C++ 扩展插件和 Visual Studio Code 所需的所有内容就是此文件。

我可以构建和调试一个简单的 C++ SDL 演示项目。使其全部工作的控件位于状态栏上。由插件加载在那里。我的操作系统的文件很简单,但有些系统需要更多帮助才能找到 SDL 库,而不是:

find_package(需要 SDL2)

这是我的 hello world 项目的 github:
https ://github.com/KeithHayes/-SDL-example-build-in-VC-Code


推荐阅读