Building a wxWidgets project in VS Code - c++

I try to start using Linux(Fedora 33) and now I've got a trouble. I want to use VS Code and it builds simple cpp projects correctly, but I want to make GUI using wxWidgets. So I created simple example, but it doesn't want to build it. I installed wxWidgets from rpm package. I also added pathes into includePath in configuration file. Now I want to show you the code and errors.
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
int main() { return 0; }
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ сборка активного файла",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "компилятор: /usr/bin/g++"
}
],
"version": "2.0.0"
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Сборка и отладка активного файла",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Включить автоматическое форматирование для gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ сборка активного файла",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/**",
"/usr/lib64/wx/include/**"
],
"defines": [],
"compilerPath": "/usr/bin/clang++",
"cStandard": "c17",
"cppStandard": "c++14",
"intelliSenseMode": "linux-clang-x64",
"compilerArgs": []
}
],
"version": 4
}
Now, there are no errors given me by intellisence, I think.
Compilers errors:
Запуск сборки…
/usr/bin/g++ -g /home/nikita/Workfiles/wxex/main.cpp -o /home/nikita/Workfiles/wxex/main
/home/nikita/Workfiles/wxex/main.cpp:1:10: фатальная ошибка: wx/wxprec.h: Нет такого файла или каталога
1 | #include <wx/wxprec.h>
| ^~~~~~~~~~~~~
компиляция прервана.
Сборка завершена с ошибками.
The terminal process failed to launch (exit code: -1).

Replace
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
with
#include <wx/wx.h>

wxWidgets provides wx-config script under Unix which must be used to obtain the options necessary for compiling and linking programs using the library. Please run wx-config --cxxflags to obtain the full flags you need to put in your c_cpp_properties.json and also wx-config --libs to get the linker flags that you will need to use to make the program link, and not just compile, successfully.

As the person who starts using Linux AND wants to learn new library id recommend followinh:
Grab the source code from www.wxwidget.org/download
Unpack it somewhere
Open the terminal
cd wxWidgets-3.1.4
mkdir buildGTK
cd buildGTK
../configure --enable-debug
make
cd samples/minimal
Make
Now the last make command will display everything you need to include and link against. You will also have a samples code you can refer to when needed.
If you don't want to build the library from source - try the following
Open terminal
Type "which wx-config" (without quotes) and press ebter
Run "/path/to/wx-config --cxxflags" (without quots)
Run "/path/to/wx-config --libs" (without quotes)
Use the output from the commands above in you IDE.
HTH.

Related

Setting up OpenCV for C++ in VIsual Studio Code in Ubuntu 18.04

There may be several similar questions, I read most of them but it did not sovle my problem. This is my first time setting this up. I will sum up what I have done.
First I installed OpenCV from source (both opencv and opencv_contrib) (version 4.5.2) following this link. After that I find out I need CMakeLists.txt and I put this content inside it:
cmake_minimum_required(VERSION 3.0.0)
project(opeet)
find_package( OpenCV 4 REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable(opeet moje.cpp)
target_link_libraries( opeet ${OpenCV_LIBS} )
This CMake file says this:
Found OpenCV: /usr/local (found suitable version "4.5.2", minimum required is "4")
So I suppose it found OpenCV properly.
When I had run this program:
#include<iostream>
#include<opencv2/opencv.hpp>
int main() {
std::cout<<"hahahahha"<<std::endl;
return 0;
}
I got error: opencv2/opencv_modules.hpp not found.
Then I made little change in my directories organization. When I installed OpenCV it installed in directory:
/usr/local/include/opencv4/opencv2
I moved directory opencv2 to include (so opencv4 is deleted).
So now directory for OpenCV is:
/usr/local/include/opencv2
Now program in the above compiles fine.
But now if I add this line (just declaring variable) cv::Mat q;, when I compile it gives me error:
/home/faris/Desktop/Opeet/moje.cpp:5: undefined reference to `cv::Mat::Mat()'
/home/faris/Desktop/Opeet/moje.cpp:5: undefined reference to `cv::Mat::~Mat()'
Now my agony starts. I can't solve this. I tried a lot of things. I tried adding path to c_cpp_properties.json but it did not help. I will post files from .vscode so maybe there is some mistake.
c_cpp_properties.json:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/include"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "linux-clang-x64",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"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",
"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}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++"
}
]
}
I would really appreciate if someone could help me with this, I don't know what to do anymore.

Visual Studio Code - configure OpenCV libraries for C++

I've installed OpenCV On Ubuntu successfully and I managed to run a sample code as:
g++ main.cpp -o testoutput -std=c++11 `pkg-config --cflags --libs opencv`
I've tried to run it with Visual Studio Code, I've installed the extension of C/C++ and code runner and ran it with the following configuration:
tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-std=c++11","`pkg-config","--cflags","--libs opencv`"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": ["-std=c++11","`pkg-config","--cflags","--libs opencv`"],
"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++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
I got the following error:
[Running] cd "/home/kfir/code/opencv_test/" && g++ main.cpp -o main && "/home/kfir/code/opencv_test/"main
main.cpp:1:10: fatal error: opencv2/core.hpp: No such file or directory
#include <opencv2/core.hpp>
^~~~~~~~~~~~~~~~~~
compilation terminated.
[Done] exited with code=1 in 0.032 seconds
Note: I'm using VSCode on mac and connect Ubuntu remote machine by ssh, the terminal works fine with the command of g++ above
Be sure to add the location to openCV header files in your includePath.
c_cpp_properties.json file :
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${default}",
"~/opencv4.5-custom/include/opencv4"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
Mine says "~/opencv4.5-custom/include/opencv4" but it could be "/usr/include/opencv4" or something else, depending on how and where you installed openCV.
You also need to modify task.json to add the arguments to the compiler as given by the pkg-config --cflags --libs opencv4 command, if you would run it in the terminal. You'll need to give the path to the shared object files.
Here's the content of my task.json file :
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-I", "~/opencv4.5-custom/include/opencv4",
"-L", "~/opencv4.5-custom/lib",
"-l", "opencv_core",
"-l", "opencv_videoio",
"-l", "opencv_imgproc",
"-l", "opencv_highgui"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
You'll also need the change the paths depending to you setup.
Here I have only included the modules that are used by my program (with the lines "-l", "opencv_core" and so on...) so add or remove modules according to your needs.

Include Path is not recognized in vscode

I've been reading at almost every question related to include paths in vscode but I just can't solve my issue.
My project folder's custom libraries is in the following path
"/home/gdl/DSS2-DEV/infra"
The infra folder is structured as such:
>Infra
>Include
... some header files
>lib1
>Include
... header files
>src
... cpp files
>lib2
>Include
... header files
>src
... cpp files
So, in order to use any of these libraries, I set up the vscode environment
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [ {"name": "LD_LIBRARY_PATH", "value": "/usr/local/lib:$LD_LIBRARY_PATH" }],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-I",
"/home/gdl/DSS2-DEV/infra"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/home/gdl/DSS2-DEV/infra"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
and yet, not a single library can be included:
cpp file
#include <cstdio>
#include <libadm.h>
int main()
{
return 0;
}
Executing task: /usr/bin/g++ -g /home/gdl/DSS2-DEV/infra/parser.cpp -o /home/gdl/DSS2-DEV/infra/parser -I /home/gdl/DSS2-DEV/infra <
/home/gdl/DSS2-DEV/infra/parser.cpp:2:20: fatal error: libadm.h: No such file or directory
#include <libadm.h>
I tried using slashes but it doesn't change a thing. What drives me crazy is that if I CTRL-click the library it actually shows me the header file of that library, meaning that vscode is correctly linking files togheter.
I also tried using quotes instead of brackets for the include but nothing works

vscode clang++: no such file or directory: 'main.cpp'

Okay, so this is terribly frustrating.
I am simply trying to using clang++ in my VSCode editor to compile a very simple main.cpp program, located in the 'main' module of my project.
The project hierarchy is as follows:
${workspaceFolder}/
cpp/
main/
main.cpp
There are many other folders and files that I have purposefully omitted for the sake of simplicity.
Here is my terminal output from my tasks.json build task:
> Executing task: clang++ -Wall -std=c++17 -stdlib=libc++ -o main.out main.cpp -I/Users/ajm/Projects/restaurant/cpp/main/ --debug <
clang: error: no such file or directory: 'main.cpp'
clang: error: no input files
The terminal process terminated with exit code: 1
main.cpp is clearly in the folder that I am trying to include with -I (see the following screenshot):
What the heck am I missing here??? I am very tempted to switch my C++ IDE to either Visual Studio or Xcode, as besides this issue, VSCode's C++ autocomplete is buggy and terribly slow.
Note that I am new to using clang++. I have read through 5-10 other SO posts of others going through similar issues, none of which have helped. I also haven't found anything helpful on Clang quickstart guides on LLVM or Microsoft's website.
If anyone needs more info to answer the question, just let me know.
Someone, please help! Thanks in advance.
EDIT:
I figured I would also include my tasks.json, c_cpp_properties.json, and launch.json files.
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build with Clang",
"type": "shell",
"command": "clang++",
"args": [
"-Wall",
"-std=c++17",
"-stdlib=libc++",
"-o",
"main.out",
"main.cpp",
"-I${workspaceFolder}/cpp/main/",
"--debug"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
c_cpp_properties.json:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/boost_1_71_0"
],
"defines": [],
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Frameworks"
],
"compilerPath": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "${default}",
"browse": {
"path": []
}
}
],
"version": 4
}
launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/main.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"logging": {
"trace": true,
"traceResponse": true,
"engineLogging": true
}
}
]
}

How to configure VSCode for GTK3 for intellisense / build / debug and g++

I'm using
g++
GTK3
VSCode
How do I get the following to work:
Intellisense / code completion for gtk
Buiding inside VSCode
Debugging with VSCode
Problem:
VSCode does not find includes - especially #include <gtk/gtk.h> is red in source.
The important thing to note is, that you need to tell VSCode the include paths and compiler flags to work properly.
First step: Open the target folder in VSCode.
Now you should have a new hidden folder .vscode in there. Open it.
You want to apply the output of pkg-config --cflags gtk+-3.0 and pkg-config --libs gtk+-3.0 to their respective configs.
Make intellisense / code completion work
Create a file .vscode/c_cpp_properties.json.
Add the following content.
{
"env": {
"myDefaultIncludePath": [
"${workspaceFolder}",
"${workspaceFolder}/include"
],
"myCompilerPath": "/usr/local/bin/g++"
},
"configurations": [
{
"name": "include paths",
"intelliSenseMode": "g++-8",
"includePath": [
"/usr/include/gtk-3.0",
"/usr/include/at-spi2-atk/2.0",
"/usr/include/at-spi-2.0",
"/usr/include/dbus-1.0",
"/usr/lib/x86_64-linux-gnu/dbus-1.0/include",
"/usr/include/gtk-3.0",
"/usr/include/gio-unix-2.0",
"/usr/include/cairo",
"/usr/include/libdrm",
"/usr/include/pango-1.0",
"/usr/include/harfbuzz",
"/usr/include/pango-1.0",
"/usr/include/fribidi",
"/usr/include/atk-1.0",
"/usr/include/cairo",
"/usr/include/pixman-1",
"/usr/include/freetype2",
"/usr/include/libpng16",
"/usr/include/gdk-pixbuf-2.0",
"/usr/include/libmount",
"/usr/include/blkid",
"/usr/include/uuid",
"/usr/include/glib-2.0",
"/usr/lib/x86_64-linux-gnu/glib-2.0/include"
],
"compilerPath": "/usr/local/bin/g++",
"cStandard": "c11",
"cppStandard": "c++17",
"browse": {
"path": [
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 4
}
Note, that the content of "includePath" is the output of pkg-config --cflags gtk+-3.0 without the preceeding -Is and with double quotes and commas. You may have to adjust the values according the output of your machine
Make building work
You want to create a new task inside .vscode/tasks.json with the following content:
{
"type": "shell",
"label": "gcc debug build active file - with GTK",
"command": "/usr/bin/gcc",
"args": [
"-g",
"-pthread",
"-I/usr/include/gtk-3.0",
"-I/usr/include/at-spi2-atk/2.0",
"-I/usr/include/at-spi-2.0",
"-I/usr/include/dbus-1.0",
"-I/usr/lib/x86_64-linux-gnu/dbus-1.0/include",
"-I/usr/include/gtk-3.0",
"-I/usr/include/gio-unix-2.0",
"-I/usr/include/cairo",
"-I/usr/include/libdrm",
"-I/usr/include/pango-1.0",
"-I/usr/include/harfbuzz",
"-I/usr/include/pango-1.0",
"-I/usr/include/fribidi",
"-I/usr/include/atk-1.0",
"-I/usr/include/cairo",
"-I/usr/include/pixman-1",
"-I/usr/include/freetype2",
"-I/usr/include/libpng16",
"-I/usr/include/gdk-pixbuf-2.0",
"-I/usr/include/libmount",
"-I/usr/include/blkid",
"-I/usr/include/uuid",
"-I/usr/include/glib-2.0",
"-I/usr/lib/x86_64-linux-gnu/glib-2.0/include",
"${file}",
"-lgtk-3",
"-lgdk-3",
"-lpangocairo-1.0",
"-lpango-1.0",
"-latk-1.0",
"-lcairo-gobject",
"-lcairo",
"-lgdk_pixbuf-2.0",
"-lgio-2.0",
"-lgobject-2.0",
"-lglib-2.0",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
Note the two more indented parts within args.
The top one is again the output of pkg-config --cflags gtk+-3.0. (This time with the -Is, though.)
The bottom part is the output of pkg-config --libs gtk+-3.0 (quoted and commated)
You might need to adjust these values as well, according to the actual output of the commands on your machine
Make debugging work
You want to create a new configuration inside the .vscode/launch.json file. On my setup vscode kept using the wrong configuration, so I deleted the others. Below is the full content of the file with only one configuration.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "debug with gdb (no build)",
"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": "",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
how about using
"`pkg-config --cflags --libs gtk4`"
note the
`pkg-config`
between pkg-config, it is diferent from
'pkg-config'
to see the full file, here's the gist I've uploaded my tasks.json and c_cpp_properties.json files https://gist.github.com/KesunyianAyam/48810a1f4339f496e192f4e94adc6e3b
mine is gtk4 though, but it can be used for gtk3 by using
`pkg-config --cflags --libs gtk+-3.0`
and use the includes associated with gtk3