I want to work with Simple DirectMedia Layer using Visual Studio Code for development. I followed these excellent instructions to set up Visual Studio Code for gcc. https://code.visualstudio.com/docs/cpp/config-linux. I use Mint 20.2.
I quickly was able to build and use Visual C and the GDB debugger in a helloworld.cppwith breakpoints, but then attempts to add a SDL2 build fail when I change my helloworld.cpp to a file that uses STL. I can no longer execute my VC build task from the 'Terminal Menu=>run build task'. This is the file.
#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;
}
A separate Makefile works fine.
This is what I get if I try to execute the build task:
/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'
...
And on it goes, the listing stops after 10 undefined references. SDL is not being linked but I added the .vscode config directory files and this file shows things should be OK?
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
}
I added the ,"/usr/include/SDL2"
launch.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"
}
]
}
tasks.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++"
}
]
}
Please help. What am I missing?. I think I need to include (LINKER_FLAGS = -lSDL2)? It is in my Makefile which works. I want to use VS code for GDB debugging. If I only wanted to build, my Makefile works fine.