Author Topic: Configuring Code Blocks in Windows to create GTK applications when using Msys2  (Read 1646 times)

Offline tuchin

  • Multiple posting newcomer
  • *
  • Posts: 11
Installed Msys2, compiler and GTK+:
Code
pacman -S mingw-w64-x86_64-gcc
pacman -S mingw-w64-x86_64-gdb
pacman -S make
pacman -S mingw-w64-x86_64-cmake
pacman -S mingw-w64-x86_64-toolchain base-devel
pacman -S mingw-w64-x86_64-gtk3
pacman -S mingw-w64-x86_64-glade
Installed Code Blocks, specified the compiler:
Code
C:\msys64\mingw64
Set up the project like this:
Code
Search directories Compiler
C:\msys64\mingw64\include
C:\msys64\mingw64\include\atk-1.0
C:\msys64\mingw64\include\cairo
C:\msys64\mingw64\include\glib-2.0
C:\msys64\mingw64\include\gtk-3.0
C:\msys64\mingw64\include\gtk-3.0\gtk
C:\msys64\mingw64\include\gtk-3.0\gdk
C:\msys64\mingw64\include\gdk-pixbuf-2.0
C:\msys64\mingw64\include\harfbuzz
C:\msys64\mingw64\include\pango-1.0
C:\msys64\mingw64\lib\glib-2.0\include
Search directories Linker
C:\msys64\mingw64\lib
In a simple program:
Code
int main(int argc, char *argv[])
{
  gtk_init (&argc, &argv);
  GtkWidget *win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_widget_show (win);
  gtk_main ();
  return 0;
}
I get errors:
Code
||=== Build: Debug in cbTestGTK (compiler: GNU GCC Compiler) ===|
C:\msys64\mingw64\bin\..\lib\gcc\x86_64-w64-mingw32\12.2.0\..\..\..\..\x86_64-w64-mingw32\bin\ld.exe: obj\Debug\main.o||in function `main':|
D:\MyProgramming\cbTestGTK\main.cpp|5|undefined reference to `gtk_init_abi_check'|
C:\msys64\mingw64\bin\..\lib\gcc\x86_64-w64-mingw32\12.2.0\..\..\..\..\x86_64-w64-mingw32\bin\ld.exe: D:\MyProgramming\cbTestGTK\main.cpp|6|undefined reference to `gtk_window_new'|
C:\msys64\mingw64\bin\..\lib\gcc\x86_64-w64-mingw32\12.2.0\..\..\..\..\x86_64-w64-mingw32\bin\ld.exe: D:\MyProgramming\cbTestGTK\main.cpp|7|undefined reference to `gtk_widget_show'|
C:\msys64\mingw64\bin\..\lib\gcc\x86_64-w64-mingw32\12.2.0\..\..\..\..\x86_64-w64-mingw32\bin\ld.exe: D:\MyProgramming\cbTestGTK\main.cpp|8|undefined reference to `gtk_main'|
||error: ld returned 1 exit status|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
Please help me set up the project correctly.

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1252
You added the search directories for the linker, but did not add the gtk library itself.

Look in C:\msys64\mingw64\lib for something similar to "libgtk-3.a" and add it to Linker settings -> Link libraries (I do not use neither GTK nor MSYS2).

Offline tuchin

  • Multiple posting newcomer
  • *
  • Posts: 11
It didn't help. There are even more errors.

Offline tumanovalex

  • Multiple posting newcomer
  • *
  • Posts: 18
I would also like to get an answer to this question. I followed the same steps and got the same results. By searching through the files, I found that there are links to gtk_init_api_check in C:\msys64\mingw64\lib\libgtk-3.a and C:\msys64\mingw64\lib\libgtk-3.dll.a , but directly connecting them to the project did not help either.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7334
    • My Best Post
5 years ago, I wrote an CB Project; it might still work.

https://github.com/stahta01/MSys2_CodeBlocks_Projects/tree/master/gtk

Edit: I am trying to test it; but, my Internet Bandwidth is working on a large download; so, it could be a while.
Edit2: I had to add an include path and the project worked.

Tim S.
« Last Edit: October 22, 2022, 05:23:45 am by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline AndrewCot

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 678
I would suggest starting with a GTK tutorial. There are a bunch on the internet.
The following is an example:https://docs.gtk.org/gtk3/getting_started.html

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7334
    • My Best Post
Code
-------------- Build: Debug in minimal GTK3 (compiler: GNU GCC Compiler)---------------

g++.exe -Wall -g -IC:\msys64\mingw64\include\gtk-3.0 -IC:\msys64\mingw64\include\glib-2.0 -IC:\msys64\mingw64\lib\glib-2.0\include -IC:\msys64\mingw64\include\pango-1.0 -IC:\msys64\mingw64\include\cairo -IC:\msys64\mingw64\include\gdk-pixbuf-2.0 -IC:\msys64\mingw64\include\atk-1.0 -IC:\msys64\mingw64\include\harfbuzz -c H:\repos\git\devel\IDE_git_repos\MSys2_CodeBlocks_Projects\gtk\main.cpp -o obj\Debug\main.o
g++.exe  -o bin\Debug\gtk3_msys2.exe obj\Debug\main.o   -lgtk-3 -lgobject-2.0 -lglib-2.0
Output file is bin\Debug\gtk3_msys2.exe with size 125.40 KB
Process terminated with status 0 (0 minute(s), 5 second(s))
0 error(s), 0 warning(s) (0 minute(s), 5 second(s))
 
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7334
    • My Best Post
After looking at my working build log and one of the failed ones, it looks like an bad toolchain setup.
Edit: And, likely the missing libs from library linker list

This is my setup.

Compiler Installation Dir: C:\msys64\mingw64
C Compiler: gcc.exe
C++ Compiler: g++.exe
Linker for Dynamic Libs: g++.exe
Linker for static libs: ar.exe
Res. Compiler: windres.exe
Make program: mingw32-make.exe

Additional Paths: $(TARGET_COMPILER_DIR)../usr/bin

Tim S.
« Last Edit: October 22, 2022, 08:40:48 am by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7334
    • My Best Post
If the poster would post the build log instead of the build messages it would be easier to help them.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline AndrewCot

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 678
The project in the zip file did not have any link libraries and therefore will fail.

The tutorial examples include linking with libraries and run.
If you add the following then the project will work, if you have the right MSYS2 packages installed:        <Compiler>            <Add option="`pkg-config --cflags gtk+-3.0`"/>
        </Compiler>
        <Linker>
            <Add option="`pkg-config --libs gtk+-3.0`"/>
        </Linker>

Offline tuchin

  • Multiple posting newcomer
  • *
  • Posts: 11
AndrewCot, thank you very much, everything worked out! And where can I download  tutorial examples?