-2

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.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Keith
  • 49
  • 5
  • 2
    Are you sure this is your actual `tasks.json`? It contains `-lSDL2`, yet I don't see it in the build log. *"my Makefile which works"* Why don't you run Make from a VSC task then? It's a better option than manually invoking the compiler. – HolyBlackCat Sep 01 '21 at 19:38
  • *"If you can't help"* That's exactly what I'm trying to do. *"where is your answer"* I can't answer without more information, that's why I'm commenting first. *"using the Makefile .. separate task with a lot of complexity"* Nah, `tasks.json` lets you run arbitrary commands, so you can just run `make` from it. *"I don't appreciate your down-vote"* I downvoted because the information in your question doesn't add up. That build log can't be produced by running this task. *"did not ask it to be told what to do"* I'm trying to help you by suggesting a better alternative, and this is how you reply? – HolyBlackCat Sep 01 '21 at 20:11
  • The task file is right and so is the output. I just ran it. – Keith Sep 01 '21 at 20:58
  • /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' /usr/bin/ld: /home/kdog/Code/gcc/hello/helloworld.cpp:17: undefined reference to `SDL_GetError' /usr/bin/ld: /home/kdog/Code/gcc/hello/helloworld.cpp:19: undefined reference to `SDL_GetWindowSurface' ... – Keith Sep 01 '21 at 21:00

1 Answers1

0

Change in direction. I added this CMakeLists.txt file to the folder where I have an example project. The project is called showpic. The close control 'X' over the window works:

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})

Adding the 'CMake Tools Extension for Visual Studio Code' plugin along with the C/C++ extensions plugin and all Visual Studio Code needs is this file.

I can build and debug a simple C++ SDL demo project. The controls to make it all work are on the status bar. Loaded there by the plugins. The file for my OS is simple but some systems need more help finding the SDL library than :

find_package(SDL2 REQUIRED)

Here is a github with my hello world project:
https://github.com/KeithHayes/-SDL-example-build-in-VC-Code

Keith
  • 49
  • 5