1

I was hoping to run GStreamer Hello World example in a Linux Virtualbox with VS Code.

Gstreamer install directions here.
GStreamer HelloWorld info here.

The manual C build/compile command is $ gcc basic-tutorial-1.c -o basic-tutorial-1 'pkg-config --cflags --libs gstreamer-1.0' Works great that way. But, I was hoping to use Visual Studio Code, and I'm trying to push the 'pkg-config --cflags --libs gstreamer-1.0' content into my launch.json file. It's not clear to me on how exactly to do that.

I started with a launch.json file which I believe was created by the C/C++ plugin from Microsoft within VS Code. I did not add a CMakeLists file. There are no other extensions installed within VS Code.

My current launch.json file: (test #17 or so...)

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [{ "name": "pkg-config", "value": " --cflags"},{"name": "--libs", "value":  "gstreamer-1.0"}],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

The error I'm seeing? cannot open source file "gst/gst.h" I don't understand what launch.json is looking for.

Edit/Comment: Apparently this is not a new issue.

And I'm just not seeing a clear solution. The solutions from DarkTrick work, but are pretty ugly. Ugly enough to push one over to Visual Studio instead of VS Code. Another option is to use CMakeLists.txt with VS Code. That uses multiple .vscode files, but at least they are generated as opposed to just hacked.

Anybody else got a simple solution to use pkg-config with VS code and launch.json?

zipzit
  • 3,778
  • 4
  • 35
  • 63
  • But why are you modifying `launch.json`? You need to change `tasks.json`, specifically the `C/C++: gcc build active file` task. – HolyBlackCat May 04 '21 at 18:10
  • 1
    Huh? I thought tasks.json was a generated file, based on input from launch.json? – zipzit May 04 '21 at 18:11
  • Nope. `launch.json` is the debugger settings. `tasks.json` contains commands used for compilation (or anything else). – HolyBlackCat May 04 '21 at 18:12
  • Right there at the bottom of the generated tasks.json file is the content "detail": "Task generated by Debugger". Clear as day. I know I could force the action onto that generated file, but that just seems like cheating. Do you have a reference of how this is supposed to work? Many thx. – zipzit May 04 '21 at 18:33
  • You're supposed to modify `tasks.json`. Even if the task was generated automatically, I presume it's a just template that you're free to modify. – HolyBlackCat May 04 '21 at 18:36
  • reference, reference, reference? From what I can see this [is pretty ugly.](https://code.visualstudio.com/docs/editor/tasks) Reminds me of when I worked in a car assembly plant. A crescent wrench would fit every nut and bolt on the car, yet was never ever used anywhere. And VS Code is definitely a one tool that fits everything crescent wrench (JS/ C/C++ C# Python, etc...). – zipzit May 04 '21 at 18:40
  • Your link *is* the reference. What else do you need? There's a good chance you can plug your whole command directly into that file (or with minimal modifications). – HolyBlackCat May 04 '21 at 18:42
  • I'm also working with gstreamer and found a more clean way of compiling for release/debug builds using a Makefile (with pkg-config). Still had to do the intellisense stuff manually though. – Xosrov Jul 28 '22 at 09:12

3 Answers3

8

So, learned a few things here. Three files, all within the .vscode directory, manage the process.

  • launch.json deals with the "run and debug" process.
  • c_cpp_properties.json deals with intellisense but NOT compilation.
  • tasks.json deals with the build and compile process.

Although I was able to determine that in order to "run" the 'pkg-config --cflags --libs gstreamer-1.0' it needed to be surrounded in double quotes, then reverse single quote, I could never get any of the tooling to work harmoniously that way.

Instead, just run $ pkg-config --cflags --libs gstreamer-1.0 in the terminal (without quotes). That shell command returns:

-pthread -I/usr/include/gstreamer-1.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lgstreamer-1.0 -lgobject-2.0 -lglib-2.0

Manually grab those include (-I) and library (-l) elements and place them in the appropriate places within tasks.json and c_cpp_properties.json files. And that works. I can use intellisense to understand the code, I can debug step thru the content.

There were a couple of tricks along the way. Use VS Code to generate each of the three files. tasks.json and launch.json will propagate when you try to "Run and debug". And you can generate the c_cpp_properties.json file by finding an intellisense red squiggly line error. Look for the light bulb icon, select it. Add xxx or edit xxx to generate the c_cpp_properties.json file within your project for you.

VSCode warning 1

VSCode Intellisense aid 2

And although its a bit lengthy, here are the three .json control files for GStreamer hello world.

launch.json:

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

tasks.json:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc build active file",
            "command": "/usr/bin/gcc",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-pthread",
                "-I/usr/include/gstreamer-1.0",
                "-I/usr/include/glib-2.0",
                "-I/usr/lib/x86_64-linux-gnu/glib-2.0/include",
                "-lgstreamer-1.0",
                "-lgobject-2.0",
                "-lglib-2.0"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

and c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/include/gstreamer-1.0/**",
                "/usr/include/glib-2.0",
                "/usr/lib/x86_64-linux-gnu/glib-2.0/include"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64",
            "compilerArgs": [
                "-pthread",
                "-lgstreamer-1.0",
                "-lgobject-2.0",
                "-lglib-2.0"
            ]
        }
    ],
    "version": 4
}
zipzit
  • 3,778
  • 4
  • 35
  • 63
  • A possible way to avoid copy-pasting `pkg-config` output is to put your compiler command into a file, then run it as a shell script from `tasks.json`. Not sure if there's a cleaner way. – HolyBlackCat May 05 '21 at 18:01
  • 1
    Right you are.. but I will say. doing it manually really reminds me 1) of why we love JS and Python 2) how C/C++ stuff really works in the details. And if you don't understand how the details work, you will get stuck somehow, someway. – zipzit May 05 '21 at 18:03
  • I think this is a good solution so far. I've run the code succesfully. – Hung Le Jun 24 '21 at 23:25
4

I'm writing this as an answer because I don't have enough reputation to comment (literally just made an account). In case you have not stumbled upon this already, you can directly run the pkg-config command in tasks.json by using "`pkg-config --libs --cflags gstreamer-1.0`" in "args".

Found this from here originally and recently found this which shows the updated functionality.

0

You could do all that....

or in one line you can just add "gstreamer" to your vcpkg.json, run vcpkg install and you'd be done

Jonathan
  • 6,741
  • 7
  • 52
  • 69
  • He already installed the library though other means, and you can tell since the `pkg-config` output is correct. Why would he go out of his way to install the thing again and change his tooling in the process? His setup works, the convoluted thing here is not his build process, but the fact that VS Code was refusing to work with pkg-config, which other answers show how to do in betters way. – Sebastian Gudiño Mar 02 '23 at 12:54