c++ - 带有 SDL 库的 Visual Studio Code,无法构建。未定义的引用问题
问题描述
我想使用 Visual Studio Code 使用 Simple DirectMedia 层进行开发。我按照这些出色的说明为gcc设置了Visual Studio Code。https://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 可以正常工作。
解决方案
改变方向。我将此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
推荐阅读
- flutter - InheritedWidget 中的 GlobalKey 导致重新父级
- algorithm - 是否存在平衡的 BST,每个节点都保持子树大小?
- java - 修改 Webview 中的一些文本
- git - 用于基于新发布标签触发詹金斯的 groovy 作业 DSL
- javascript - 为什么验证后输入值未定义?
- android - 离子本地通知声音不适用于 Android 8.0
- javascript - 使用 jQuery 切换导航栏,如果用户使用导航栏媒体查询停止工作
- button - 如何在 IconButton 上产生圆形波纹效果?
- arrays - 将 VBA 整数数组传递给 c# DLL int 数组
- string - 如何从声明性宏中返回新字符串?