Configure g++ to build c++ with wxwidget library - c++

I've been trying to compile and run a simple c++ program with Wxwidget in Linux but when I build it this is what I got when I try to build :
Executing task: g++ -c $(find /home/sopheak/Documents/WXWIDGET/ -type f -iregex '.*\.cpp') -g -D__WXGTK__ -D_FILE_OFFSET_BITS=64 -DWX_PRECOMP -fno-strict-aliasing -pthread -I/usr/local/lib/wx/include/gtk3-unicode-static-3.1/** -Iusr/include/** -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/gio-unix-2.0/** -I/usr/include/cairo/** -I/usr/include/pango-1.0/** -I/usr/include/fribidi/** -I/usr/include/harfbuzz/** -I/usr/include/atk-1.0/** -I/usr/include/pixman-1/** -I/usr/include/uuid/** -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/glib-2.0/** -I/usr/lib/x86_64-linux-gnu/glib-2.0/include/** -I/usr/include/gtk-3.0/unix-print/** -Wall
zsh:1: no matches found: -I/usr/local/lib/wx/include/gtk3-unicode-static-3.1/**
The terminal process "zsh '-c', 'g++ -c $(find /home/sopheak/Documents/WXWIDGET/ -type f -iregex '.*\.cpp') -g -D__WXGTK__ -D_FILE_OFFSET_BITS=64 -DWX_PRECOMP -fno-strict-aliasing -pthread -I/usr/local/lib/wx/include/gtk3-unicode-static-3.1/** -Iusr/include/** -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/gio-unix-2.0/** -I/usr/include/cairo/** -I/usr/include/pango-1.0/** -I/usr/include/fribidi/** -I/usr/include/harfbuzz/** -I/usr/include/atk-1.0/** -I/usr/include/pixman-1/** -I/usr/include/uuid/** -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/glib-2.0/** -I/usr/lib/x86_64-linux-gnu/glib-2.0/include/** -I/usr/include/gtk-3.0/unix-print/** -Wall'" failed to launch (exit code: 1).
Here my task.json :
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "Compile",
"linux": {
"command": "g++",
"args": [
"-c",
"$(find",
"${workspaceFolder}/",
"-type",
"f",
"-iregex",
"'.*\\.cpp')",
"-g",
"-D__WXGTK__",
"-D_FILE_OFFSET_BITS=64",
"-DWX_PRECOMP",
"-fno-strict-aliasing",
"-pthread",
"-I/usr/local/lib/wx/include/gtk3-unicode-static-3.1/**",
"-Iusr/include/**",
"-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/gio-unix-2.0/**",
"-I/usr/include/cairo/**",
"-I/usr/include/pango-1.0/**",
"-I/usr/include/fribidi/**",
"-I/usr/include/harfbuzz/**",
"-I/usr/include/atk-1.0/**",
"-I/usr/include/pixman-1/**",
"-I/usr/include/uuid/**",
"-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/glib-2.0/**",
"-I/usr/lib/x86_64-linux-gnu/glib-2.0/include/**",
"-I/usr/include/gtk-3.0/unix-print/**",
"-Wall"
]
},
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"type": "shell",
"label": "MoveObjects",
"linux": {
"command": "mv",
"args": [
"${workspaceFolder}/*.o",
"${workspaceFolder}/"
]
},
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [],
"dependsOn": [
"Compile"
]
}
]
}
I'm using Kali Linux and I've been trying looking for ways to build run wxwidgets library for weeks but I still couldn't find any good answer.
Thanks you for helping in advance !

I think when using wxWidgets in VS Code on Linux, the easiest thing is to use CMake. To get started, you'll need both the CMake and CMake Tools extensions:
To get started, open your project in VS Code:
Then open the command pallet and select "CMake:Quick Start":
Then enter a name for the project that will be used in the CMake files and select executable for the project type. I used "cmakewx" for the project. After you select executable, a "CMakeLists.txt" file and a build folder will be created.
Open the CMakeLists.txt file. In the middle of the file there should be a line looking something like:
add_executable(cmakewx main.cpp)
In my case "cmakewx" is the name I entered above for the project name. In your case, it will be the name you entered instead. Change this to
find_package(wxWidgets REQUIRED COMPONENTS net core base)
include(${wxWidgets_USE_FILE})
add_executable(cmakewx main.cpp)
target_link_libraries(cmakewx ${wxWidgets_LIBRARIES})
But replace "cmakewx" with the name you chose for the project.
Finally, open the command pallet again and select "CMake:Configure"
Now the project is ready to go. You can use buttons on the status bar for various project tasks. This area: can be used to change the project's configuration (debug/release/etc). This area will build the project, and these buttons will debug and run the project.
This may seem like a lot of steps, but when your familiar with the process it only takes about 30 seconds to get a project ready to go.
Intellisense should start working after you configure the project. But I have seen that sometimes it doesn't. I'm not sure why. If this happens, closing and reopening the project should get intellisense working.

Related

clang++ library not found even when providing library path

I was trying to create a new OpenGL project with glfw and glad using vs code on an m1 Mac, and setup the files such that I have the include folder and lib folder which contains the necessary headers and library (libglfw3.a) files and the src folder that contains the glad.c and main.cpp, all in my workspace folder.
I have modified the tasks.json file like such:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-fdiagnostics-color=always",
"-g",
"-std=c++17",
"-I${workspaceFolder}/include",
"-L${workspaceFolder}/lib",
"${workspaceFolder}/src/*.cpp",
"${workspaceFolder}/src/glad.c",
"-llibglfw3",
"-o",
"${workspaceFolder}/game"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/clang++"
}
]
}
However, I get the following error:
ld: library not found for -llibglfw3
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I thought that I had properly included the path to the glfw library, but it says that it could not find the library. What am I doing wrong?
As a side-note, I have followed this tutorial to setup the program, but this post assumes that you have a windows machine, so I had downloaded/changed some things to my knowledge of what would work with the m1 mac.
Normally when you give libraries to link with the -l flag, you omit the lib prefix. For example: "-lglfw3" links the file "libglfw3.a". It looks like you should change your -llibglfw3 option.

gcc how find -lglfw packages?

when cpp code Linking on vscode
must define json file like below.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ 활성 파일 빌드",
"command": "/usr/bin/g++",
"args": [
"-g",
"glad.c",
"-pthread",
"-o",
"main",
"main.cpp",
"-lglfw",
"-lGLU",
"-lGL",
"-lXrandr",
"-lXxf86vm",
"-lXinerama",
"-lX11",
"-lrt",
"-ldl",
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "컴파일러: /usr/bin/g++"
}
]
}
my question is
how gcc compiler detect args parameter.
Words like -g...
Because the compiler pre-specifies words, it looks for words and pre-specifies what to do.
The -lXrandr word is the name of the package you have installed.
When this name was found,
What does the compiler do?
Does the compiler search for pre-specified folders and check for packages?
I want to know how the cpp compiler connects files on the Linux system. Do you have any links to help with this knowledge?
how gcc compiler detect args parameter.
Gcc is a very old project with a lot of options. It has it's own command line options handling library.
The -lXrandr word is the name of the package you have installed
No, it's not the name of package. It's the name of the library.
What does the compiler do?
Compiler searches for a library name libXrandr.so. In short, the file is searched in multiple directories, the standard library paths are searched and gcc installation path and additionally paths given with -L parameter and paths with LIBRARY_PATH environment variable. See also man ld.so and man ldconfig.
Does the compiler search for pre-specified folders
Yes.
and check for packages?
No, a "package" is something else. Gcc is a compiler, it does not deal with "packages".
how the cpp [*gcc] compiler connects files on the Linux system
"How" is a broad question and gcc is a very complicated program. It opens the files, parses them properly, finds symbols, resolves references, generates final executable.
Do you have any links to help with this knowledge?
Gcc has extensive documentation and it's open source.

Visual Studio Code: how to add arguments for g++ compiler?

I want to use some C++17 features that Mac's clang doesn't currently support, so I use
brew install gcc --HEAD
to install the g++ 10.0.1 version. Codes run well in terminal by directly calling
g++-HEAD -std=c++17 test.cpp
I also created a link ln -s g++-HEAD g++ in bash, and added an alias alias g++='g++ -std=c++17' in .bash_profile, so that
g++ test.cpp
will do the same job.
I want to run C++ code in Visual Studio Code - Mac version. After installing its C/C++ Extension by Microsoft, and Code Runner Extension, I set up the settings.json file in VSCode to include compiler argument:
{
"C_Cpp.default.cppStandard": "c++17",
"C_Cpp.default.compilerPath": "/usr/bin/g++",
"C_Cpp.default.intelliSenseMode": "gcc-x64",
"C_Cpp.default.compilerArgs": [
"-std=c++17"
]
}
Then I tried to run the same code. However, I got warning:
[Running] cd "/some directory/" && g++ test.cpp -o test && "/some directory/"test
warning: fold-expressions only available with '-std=c++17' or '-std=gnu++17'
Clearly, it means the g++ compiler ran in VSCode is not the one I manually set with alias. More interestingly, if I run directly in VSCode TERMINAL, my previous code
g++ test.cpp -o test
works.
I'm confused by the setup in VSCode: why doesn't the runner use the same g++ compiler argument as used in VSCode's own terminal? Also, how should I modify the settings.json file or some other files in VSCode so that I can correctly add the -std=c++17 argument?
Assuming you use the C/C++ extension, create a task.json file, which will allow you to change settings such as the path to the compiler, include paths, C++ standard, optimization (default is C++17), and more.
From the code tutorial:
From the main menu, choose Terminal > Configure Default Build Task. A dropdown appears showing various predefined build tasks for C++ compilers. Choose C/C++: g++ build active file.
This will create a tasks.json file in a .vscode folder and open it in the editor.
Your new tasks.json file should look similar to the JSON below
My file looks like
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-std=c++17",
"-ggdb",
"-Og",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++"
}
]
}
Here is my solution to this predicament. I link it with my projects Makefile.
First, you'll need to build a Makefile, if your using GCC using a Makefile on its own is... controversial to say the least. But I think it serves the purposes of this example. As you may know, running "make" instead of g++ in the current directory will parse the Makefile and run the corresponding commands. But in your case, your Makefile may look similar to:
#You're gonna wanna make it think that your executable doesnt exist, otherwise,
#because the executable exists, make will assume its the most recent build.
.PHONY: debug
#using g++ and the flags inline like this, is generally seen as bad practice, it might be worth
#looking into using Makefiles to make this more acceptable, but this will get you started.
debug:
g++ -g -o debug main.cpp -std=c++17
clean:
rm debug
#if you copy this exactly, make you you replace the spaces with proper tab
#characters otherwise it will error out.
The Juicy part is in VS code; It has a very power feature known as tasks. Tasks are their very own special rabbit hole, but to put it bluntly, you add a task to the "tasks" array in a generated tasks.json file in your workspace, if you're not familar with how that might look, here is the syntax for a task:
{
"label": "[name of task]",
"type": "[type of task, usually its a shell command]",
"command": "[the actual command to call]"
}
There are many more features that tasks can offer, but for making a build tool this will be all you need, for me, this resulted in a file that looked like this:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build_debug",
"type": "shell",
"command": "make"
},
{
"label": "clean",
"type": "shell",
"command": "make clean"
},
{
"label": "build",
"dependsOn": [
"clean",
"build_debug"
],
"problemMatcher": [
"$gcc"
]
}
]
}
Why do we need to have a final build call? because your launch.json object can take a "preLaunchTask" that will automatically call prior to debug. You can slap in that final build call, and it will compile, debugger, and then run your app. it will even integrate GDB breakpoints and memory tracking into the workspace. My launch.json looks like this:
{
// 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/runnable",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
]
}
]
}
Sorry for the long replay, i hope it helps :)

VSCode dynamic linking - C++ Boost Test

I'm trying to get a Boost Unit Test program to run in VSCode, but I'm having trouble linking the library.
I've written a simple unit test setup with two classes, Boost Test, and FakeIt mocking framework, and easily verified that it works in VS2017. The motivation was to make it work in VSCode eventually though.
When I build the project, it fails to find the boost test files. I tried a few different include options; absolutely path, relative path, and with environment variables (as seen in c_cpp_properties.json below).
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "g++ -g -Wall main.cpp -L/c:/boost/lib64-msvc-14.1 -llibboost_unit_test_framework-vc141-mt-gd-x64-1_69",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:/MinGW/bin/gdb.exe",
"preLaunchTask": "echo",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"${env:BOOST_ROOT}",
"${env:FAKEIT}"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "C:/MinGW/bin/gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64",
"browse": {
"path": [
"${C:/MinGW/lib/gcc/mingw32/6.3.0/include/c++}"
]
}
}
],
"version": 4
}
What I get is
Executing task: g++ -g main.cpp <
main.cpp:3:10: fatal error: boost/test/unit_test.hpp: No such file or directory
#include <boost/test/unit_test.hpp>
^~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
The terminal process terminated with exit code: 1
Have I set up my build files incorrectly? Being used to VS2017, I'm fairly new to manually writing the build files, but I did make a working Hello World program in VSCode Cpp, so it's not like the compiler isn't working. Hope someone can help, I can provide the cpp/h files if requested, but it seems unnecessary given that I know it works in VS2017.
Edit1: I can even right click the Boost and FakeIt includes and "Go to Definition", and it opens the files, but when I try to build it says it can't...?!
Edit2: After some searching for makefile compiler commands, I updated the command to
g++ -g -Wall main.cpp -L/c:/boost/lib64-msvc-14.1
-llibboost_unit_test_framework-vc141-mt-gd-x64-1_69
, trying to tell the compiler to link the library (both .lib and .dll files are present in that folder) but it didn't change anything.
Cheers,
Tachyon
I'm not sure why you think this is a link problem, the error message is definitely telling you this is a compilation problem. The part of the message that lets you know this is for sure is:
compilation terminated.
The gcc linker would say something about "link failed" if this was a linking issue.
Another clue this is a compilation problem is:
main.cpp:3:10: fatal error: boost/test/unit_test.hpp: No such file or directory
This is the c preprocessor telling you it can't find a file you source code has asked for. Includes happen at compilation time not link time.
Your problem is just that you forgot to tell the compiler where the boost headers are in tasks.json on this line:
"command": "g++ -g -Wall main.cpp -L/c:/boost/lib64-msvc-14.1 -llibboost_unit_test_framework-vc141-mt-gd-x64-1_69",
The -L option tells the linker where to find files (not the compiler). It needs to read:
"command": "g++ -g -Wall main.cpp -Ic:/boost -L/c:/boost/lib64-msvc-14.1 -llibboost_unit_test_framework-vc141-mt-gd-x64-1_69",

G++ Compiling Visual Studio Code

I have installed Visual Studio Code on Ubuntu, but I can't seem to get it to compile. I have looked at https://code.visualstudio.com/docs/languages/cpp and https://code.visualstudio.com/Docs/editor/tasks and have tried all the examples but I keep getting the error "No build task defined. Mark a task with 'isBuildCommand' in the tasks.json file." The command I would like to run is g++ Main.cpp Classes.cpp -o Planets -lGL -lglut
I once encountered this problem. What I learned from that are these:
Make sure your Main.cpp, Classes.cpp are at the $workspaceRoot or use relative path.
Make sure your tasks.json file does not contain any error.
Make sure the tasks.json has a version number at the top of the code.
Make sure the type of the task is correct. (e.g. For a console app, it should be "shell")
I have created a tasks.json file for you. It works with my Main.cpp(put a Hello world program in it) and Classes.cpp(A simple class). Compare yours to mine to see if you did something wrong.
{
"version": "2.0.0",
"tasks": [
{
"taskName": "Build",
"type": "shell",
"command": "g++",
"args": [
"Main.cpp",
"Classes.cpp",
"-o",
"Planets",
"-lGL",
"-lglut"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
}
]
}