User avatar
MikeG58
Posts: 32
Joined: Thu Jan 05, 2017 7:09 am

GTK+ Programming with Code::Blocks

Tue Feb 27, 2018 7:37 pm

I've recently installed Code::Blocks on my Raspberry Pi. I prefer it as an IDE over Geany, but, I can't seem to pass the command line to it that is needed to develope GTK+ programs.

I've gone into the Build Options and entered the string $(pkg-config gtk+-3.0 --cflags --libs) as I do with Geany. I enter them into the Other options box of the Compiler settings. When I look at the Build Log, it always shows $(pkg-config gtk+-3.0 --cflags --libs) truncated to -config gtk+-3.0 --cflags --libs)

-Mike

User avatar
PeterO
Posts: 5829
Joined: Sun Jul 22, 2012 4:14 pm

Re: GTK+ Programming with Code::Blocks

Tue Feb 27, 2018 7:49 pm

Have you tried the old-style `pkg-config gtk+-3.0 --cflags --libs` ?
You would be better off asking questions like this on a code::blocks specific forum.
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
MikeG58
Posts: 32
Joined: Thu Jan 05, 2017 7:09 am

Re: GTK+ Programming with Code::Blocks

Wed Feb 28, 2018 4:34 am

Thanks, Peter O.

I looked for a forum on this community, specifically for Code::Blocks questions, but didn't see one. I'll look again.

Yes I did try it with the back quotes as well. When you set up the Code::Blocks IDE, for a GTK+ project, as I did last night, I noticed that it added the string `pkg-config gtk-+3.9 --cflags --libs`. Now, I just tried to duplicate that, and can't.

I've played and played with the examples that you've given in your tutorials, and, have learned a lot, but, the questions I'd like to ask you keep coming. The problem is, I've learned that, when it comes to programming, you almost have to know the answer before you ask the question, or else the answer will be more enigmatic than the original question.

Here's one. I'm trying to alter your second Glade Exercise, Ex 8. I want to figure out the minimum program I have to write to have four button and a GtkEntry Window. I want four buttons, one that puts a predefined text in the window, one that enters another predefined text in the window, a third that clears the window, and a button to quit.

I couldn't get it to work, but then I noticed that you needed these two lines:

Code: Select all

window  = GTK_WIDGET (gtk_builder_get_object (builder,"window"));
timer  = GTK_WIDGET (gtk_builder_get_object (builder,"timer"));
OK, so, you need to invoke 'builder' for window, that I understand. And, I can see you have to invoke 'builder' for timer, which is a GtkEntry widget. OK. But, why don't you have to invoke it for the buttons as well?

-Mike

User avatar
MikeG58
Posts: 32
Joined: Thu Jan 05, 2017 7:09 am

Re: GTK+ Programming with Code::Blocks

Wed Feb 28, 2018 5:18 am

OK, Peter O.

I just watched an Andr.oid Eric video, and he shows that you have to enter `pkg-config --cflags gtk+-3.0` into the Other Compiler Options window, and then `pkg-config --libs gtk+-3.0` additionally, into the Linker Settings - Other linker options window.

Time now to test.

Nevermind. It didn't make a darn bit of difference.

I just tried it with the built in "Hello GTK!" project, that Andr.oid Eric show. No, no difference.

I hate these times, when trying to learn how to program, that you just get stuck, and, no where to turn to to get unstuck.

OK. I just went on CodeBlocks Forum and registered as a member.

-Mike

User avatar
PeterO
Posts: 5829
Joined: Sun Jul 22, 2012 4:14 pm

Re: GTK+ Programming with Code::Blocks

Wed Feb 28, 2018 8:20 am

MikeG58 wrote:
Wed Feb 28, 2018 4:34 am
Thanks, Peter O.
I looked for a forum on this community, specifically for Code::Blocks questions, but didn't see one. I'll look again.
http://forums.codeblocks.org/ :roll:
Here's one. I'm trying to alter your second Glade Exercise, Ex 8. I want to figure out the minimum program I have to write to have four button and a GtkEntry Window. I want four buttons, one that puts a predefined text in the window, one that enters another predefined text in the window, a third that clears the window, and a button to quit.

I couldn't get it to work, but then I noticed that you needed these two lines:

Code: Select all

window  = GTK_WIDGET (gtk_builder_get_object (builder,"window"));
timer  = GTK_WIDGET (gtk_builder_get_object (builder,"timer"));
OK, so, you need to invoke 'builder' for window, that I understand. And, I can see you have to invoke 'builder' for timer, which is a GtkEntry widget. OK. But, why don't you have to invoke it for the buttons as well?

-Mike
Your code only needs to get pointers to those widgets that it is going to access or configure directly. Simple buttons rarely need any configuration beyond that performed when they are built from the glade file. Their handlers are automatically linked by calling "gtk_builder_connect_signals (builder, NULL);"

An exception is where you might want to make a button insensitive. For example you might want a "save" button to only be sensitive when a form has been fully filled-in. In that case your code would need to use gtk_builder_get_object to get a widget pointer that can be used with
gtk_widget_set_sensitive (See https://developer.gnome.org/gtk3/stable ... -sensitive).

HTH

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
MikeG58
Posts: 32
Joined: Thu Jan 05, 2017 7:09 am

Re: GTK+ Programming with Code::Blocks

Wed Feb 28, 2018 7:50 pm

Thanks, Peter O.,

For sticking with me on this.

Yes, I did go to CodeBlocks forum and initiated registration. I also searched their forum, but, they seem to have the same answer that Andr.oid Eric did in his video.

I'll work on it again tonight.

-Mike

User avatar
MikeG58
Posts: 32
Joined: Thu Jan 05, 2017 7:09 am

Re: GTK+ Programming with Code::Blocks

Thu Mar 01, 2018 5:14 am

Peter O.

I did get my test GTK/Glade program working. It just required that I add that second line above:

Code: Select all

textField = GTK_WIDGET ( gtk_builder_get_object (builder, "textField"));
Thanks for all of the help.

I'm waiting for an email verification email back from CodeBlocks so that I can finally get on with using their IDE. I did a search on the forum, though, and they offered the same solution as Andr.oid Eric, so, I guess I'm gonna have to ask them about a specific error code.

-Mike

User avatar
PeterO
Posts: 5829
Joined: Sun Jul 22, 2012 4:14 pm

Re: GTK+ Programming with Code::Blocks

Thu Mar 01, 2018 10:31 am

Mike,

I decided to take a look at code::blocks for my self, and I have a solution for you :-)

1) Start code::blocks
2) On the opening screen choose "Create a new project" and scroll across to the GTK+ project
3) Right click on the GTK+ Project and choose "Edit this script". It will initially appear with a "locked" padlock, so first use "save file" to create an editable version
4) Scroll down to find the lines
project.AddCompilerOption(_T("`pkg-config gtk+-2.0 --cflags`"));
project.AddLinkerOption(_T("`pkg-config gtk+-2.0 --libs`"));
and change them to read
project.AddCompilerOption(_T("`pkg-config gtk+-3.0 --cflags`"));
project.AddLinkerOption(_T("`pkg-config gtk+-3.0 --libs` -rdynamic"));
Take care to get -rdynamic between the back-quote and the double-quotes
5) Save the file and exit code::blocks
6) Restart code::blocks and again choose "Create new project". The text shold now be "GTK+ project" to show it has been modified
7) Choose the GTK+ project and fill in suitable names/paths
8) The template code in main.c is GTK+2 so discard that and add one of your own GTK+3 source files.
9) If all is well you should now be able to press build and run successfully :-)

I'm looking for the file holding the template code to replace with some GTK3 code.


HTH

PeterO
Last edited by PeterO on Thu Mar 01, 2018 4:19 pm, edited 1 time in total.
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
PeterO
Posts: 5829
Joined: Sun Jul 22, 2012 4:14 pm

Re: GTK+ Programming with Code::Blocks

Thu Mar 01, 2018 10:50 am

The template code is in /usr/share/codeblocks/templates/wizard/gtk/files/main.c

If you create your own version in /home/pi/.local/share/codeblocks/templates/wizard/gtk/files/main.c
it will use that instead :-)

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
MikeG58
Posts: 32
Joined: Thu Jan 05, 2017 7:09 am

Re: GTK+ Programming with Code::Blocks

Fri Mar 02, 2018 5:26 am

Wow, Peter O.,

Thanks!

Um, I'm not quite seeing what you're describing, but, I will work at it.

I'm running Code::Blocks IDE version 13.12. It is the one I installed from my repository.

Oops, I got the file you're referring to the second time around, but, it is read only. Not for sure how to unset that. I opened the containing folder, and so I know where it is. I will try to edit it outside of Code::Blocks IDE.

Got it, I did a Save As to the original file, and now, I think I can edit the wizard.script file. This is like nailing jelly to a tree. But, I sure am glad that you're helping me with this.

In case you can't tell, I'm typing this real time tonight. Giving you a blow-by-blow account of my struggles.

Hmm. Not good. A blank gray screen.

Ooph! I'll keep you posted. I thought I followed your instructions verbatim.

-Mike ;)

User avatar
MikeG58
Posts: 32
Joined: Thu Jan 05, 2017 7:09 am

Re: GTK+ Programming with Code::Blocks

Fri Mar 02, 2018 6:03 am

Wow, Peter O.,

Thanks!

Um, I'm not quite seeing what you're describing, but, I will work at it.

I'm running Code::Blocks IDE version 13.12. It is the one I installed from my repository.

Oops, I got the file you're referring to the second time around, but, it is read only. Not for sure how to unset that. I opened the containing folder, and so I know where it is. I will try to edit it outside of Code::Blocks IDE.

Got it, I did a Save As to the original file, and now, I think I can edit the wizard.script file. This is like nailing jelly to a tree. But, I sure am glad that you're helping me with this.

In case you can't tell, I'm typing this real time tonight. Giving you a blow-by-blow account of my struggles.

Hmm. Not good. A blank gray screen.

Ooph! I'll keep you posted. I thought I followed your instructions verbatim.

OK. I did go back and look at the wizard script, and, it is changed just as you instructed. I also placed my template file in /home/pi/.local/share/codeblocks/templates/wizard/gtk/files/main.c and renamed it to main.c as well.

I went and discarded all my changes, and still a blank gray screen. I just don't get it. I'm back to Geany.

-Mike ;)

User avatar
PeterO
Posts: 5829
Joined: Sun Jul 22, 2012 4:14 pm

Re: GTK+ Programming with Code::Blocks

Fri Mar 02, 2018 8:30 am

So I've wasted my time ? :(
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
MikeG58
Posts: 32
Joined: Thu Jan 05, 2017 7:09 am

Re: GTK+ Programming with Code::Blocks

Fri Mar 02, 2018 2:13 pm

No, Peter O.

I'll play with it.

I think it might be something stupid on my part, like I closed after I had changed Code::Blocks perspective. I couldn't get any code to display in the IDE window afterwards. Then, I went to view, and saw that the Default Perspective was not checked. I checked it, and the usual perspective appeared.

Unfortunately, I discarded the changes you suggested and went back to the default wizard.script.

I tried to go back and modify it again before I wen to bed, but, I couldn't get it out of read only mode.

I will play around with it, and keep you updated.

In the meantime, I kind of wonder if Code::Blocks IDE is really worth it. I don't want to "Create a Project" every time I want to program. I just want to get a C++ program going. I think that "Projects" are just for more complex, bigger programs than the ones I'm used to writing.

By the way, I applied for a job over on Mare Island, which is an old Navy facility close to Vallejo, California. The base has been turned over to civilian contractors. Anyway, the job would have been with a bunch of engineer. They worked on a platform for an automated military firing system. Anyway, the engineer in charge of programming there was a Brit. I asked him what programs he programmed in, and he told me C. I asked C++, and he told me, no, C.

-Mike

User avatar
PeterO
Posts: 5829
Joined: Sun Jul 22, 2012 4:14 pm

Re: GTK+ Programming with Code::Blocks

Fri Mar 02, 2018 7:44 pm

MikeG58 wrote:
Fri Mar 02, 2018 2:13 pm
In the meantime, I kind of wonder if Code::Blocks IDE is really worth it. I don't want to "Create a Project" every time I want to program. I just want to get a C++ program going. I think that "Projects" are just for more complex, bigger programs than the ones I'm used to writing.
Projects in this context are nothing more than a few configuration files. They work perfectly well for a projects that have only a single source file. The startup template is useful once you workout what bits of code are common to most of your programmes.
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
MikeG58
Posts: 32
Joined: Thu Jan 05, 2017 7:09 am

Re: GTK+ Programming with Code::Blocks

Fri Mar 02, 2018 9:03 pm

Peter O.,

Along those lines. Is there a way to load more than one source code for a project? A lot of the time, most of the time, I'm just typing in small tutorial problems. It seems a lot of overhead to have to "Create a Project" every time I want to test 20 - 50 lines of code.

-Mike

User avatar
MikeG58
Posts: 32
Joined: Thu Jan 05, 2017 7:09 am

Re: GTK+ Programming with Code::Blocks

Fri Mar 02, 2018 9:04 pm

I'm helping out another person, a newbie like myself to GTK+ programming. I pointed him in the direction of your tutorial.

At least I can follow along with your examples, and get them to work. That's most of the battle.

-Mike

User avatar
PeterO
Posts: 5829
Joined: Sun Jul 22, 2012 4:14 pm

Re: GTK+ Programming with Code::Blocks

Fri Mar 02, 2018 9:07 pm

MikeG58 wrote:
Fri Mar 02, 2018 9:03 pm
Peter O.,

Along those lines. Is there a way to load more than one source code for a project? A lot of the time, most of the time, I'm just typing in small tutorial problems. It seems a lot of overhead to have to "Create a Project" every time I want to test 20 - 50 lines of code.

-Mike
You make it sound like "Create a Project" is some huge task :lol:
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
MikeG58
Posts: 32
Joined: Thu Jan 05, 2017 7:09 am

Re: GTK+ Programming with Code::Blocks

Fri Mar 02, 2018 11:20 pm

No, not really. It just seems like a waste having to come up with a name for the Project, then, it seems, you have to delete the source and find the file that has your source. Not that much work, really, but, I want to code, not to fight with my IDE. I'm also going about it a roundabout way, trying to see if you knew of anyway to bypass all of this, and just get to programming, like I do with the Geany IDE.

It seems like most of the struggle with learning a new IDE, is just how to pass commands onto gcc or g++. Ah, that reminds me. One of the examples, maybe not one of yours, I tried to compile with g++. Are there any issues with that?

You're probably used to these types of inconveniences if you've been a coder for a while. I just do it as a hobby, although, for the last two months or so, I've been hitting it hard. I'm trying to get Linux down, C++, GTK+, then, I have these great videos to watch on how to build web pages.

I'm just impatient, I guess.

Oh, another question. You shouldn't have gotten me started. In .NET, there is a widget called GridView. It allows you to create a very primitive spreadsheet. I don't see anything like it, well, there is a grid container, for other widgets, in GTK+. Are there extensions to gtk+ that would supply more that just the widgets that come with GTK?

-Mike

User avatar
MikeG58
Posts: 32
Joined: Thu Jan 05, 2017 7:09 am

Re: GTK+ Programming with Code::Blocks

Sat Mar 03, 2018 5:22 am

OK, Peter O,

I edited the changes to my wizard.script that you posted above.

I have my usual Code::Blocks IDE interface back, which is great.

But, I'm still not compiling.

This is the output of the build log when I click on Build:


-------------- Build: Debug in GTKProject001 (compiler: GNU GCC Compiler)---------------

gcc -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/arm-linux-gnueabihf/dbus-1.0/include -I/usr/include/gtk-3.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libpng12 -I/usr/include/glib-2.0 -I/usr/lib/arm-linux-gnueabihf/glib-2.0/include -Wall -g -config gtk+-3.0 --cflags --libs) -c "/home/pi/workspace/C++_Code/Glade Projects/GTKProject001/main.c" -o obj/Debug/main.o
/bin/sh: 1: Syntax error: ")" unexpected
Process terminated with status 2 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

This has been the same problem I've had from the start. I'm now on my Raspberry Pi, so, it is a lot easier to send you the text of the Build log.

This is after trying to compile the "Hello World!" template source code supplied by Code::Blocks.

To my admittedly amateurish eyes, it appears to be expanding the $(pkg-config gtk+-3.0) string.

I'm not sure what it's referring to when it complains about the closing parenthesis sign, but, I just got an idea where to look. Perhaps that is some artefact left over from the Compiler Option, when I tried the newer $( ) form of expansion?

No, in both the Compiler Options and the Linker options I'm getting the 'pkg-config gtk+-3.0 ....` form displayed.

I wish I could ask about the error on the Codeblocks forum, but, so far, they have not sent me a validation email link.

-Mike

P. S. Line number 7 of the Build log. I do see an offending closing parenthesis there. That is probably the offender, but, I'm not sure where it is being generated. I know that the -Wall option, is something that you suggest, and I have been using it when I pass compiler commands to gcc with Geany, but, I don't think I've ever used that option with the Code::Blocks IDE, so, it must be part of the script.

I tried some detective work, but, I can't figure it out. It kind of looks like something might have gotten corrupted. Do you think I should delete and reinstall Code::Blocks IDE at this point?

User avatar
PeterO
Posts: 5829
Joined: Sun Jul 22, 2012 4:14 pm

Re: GTK+ Programming with Code::Blocks

Sat Mar 03, 2018 8:28 am

It looks like there is something left over in the configuration from your previous attempts to solve this problem.

I would go further than just reinstalling code:blocks. I would "start from fresh" with a new SD card image and a new installation of code::blocks.

I'll put my modified wizzard file on my web server for you (in a few minutes when I've had my breakfast!).
PeterO

PS: I've put them in http://www.peteronion.org.uk/CodeBlocks/

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
MikeG58
Posts: 32
Joined: Thu Jan 05, 2017 7:09 am

Re: GTK+ Programming with Code::Blocks

Sat Mar 03, 2018 8:41 pm

:o Ooph!

I've never burnt a new Raspian image to an SD card, but, I suppose it's time to learn. I think that the Operating System was already on the SD card when I bought the kit.

I did buy a couple of SD cards, which I have lying around. I think that the one I have installed now is 8MB, but, the ones I bought are 16GB, so, I was gonna get around to it sooner or later.

I know that the instructions for burning a new SD card are on this site, the raspberrypi.org site, but, what program do you use to burn your SD card with. I think I've heard the program name "Etcher" brandied about.

If I'm not mistaken, a fresh copy of Raspian, would force me to use a new installation of the Code::Blocks IDE.

-Mike

User avatar
PeterO
Posts: 5829
Joined: Sun Jul 22, 2012 4:14 pm

Re: GTK+ Programming with Code::Blocks

Sat Mar 03, 2018 9:40 pm

I write SDcards with "dd" but then I'm old-fashioned :-)

Etcher is a windows app and since I don't have any windows PCs it's not something I know about.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
rpdom
Posts: 17029
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: GTK+ Programming with Code::Blocks

Sat Mar 03, 2018 9:53 pm

PeterO wrote:
Sat Mar 03, 2018 9:40 pm
I write SDcards with "dd" but then I'm old-fashioned :-)
So do I :-)
Etcher is a windows app and since I don't have any windows PCs it's not something I know about.
Seems it works on x86 Linux (32 and 64 bit) and MacOS too.

User avatar
PeterO
Posts: 5829
Joined: Sun Jul 22, 2012 4:14 pm

Re: GTK+ Programming with Code::Blocks

Sat Mar 03, 2018 10:04 pm

TBH I've never looked to see if it ran on Linux as I've only ever seen it mention in a windows context.
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
MikeG58
Posts: 32
Joined: Thu Jan 05, 2017 7:09 am

Re: GTK+ Programming with Code::Blocks

Sat Mar 03, 2018 11:11 pm

I don't know if it means anything or not, but, I believe there is a discussion about it here on the Raspberry Pi site, and, I did download it from my repository.

I was wrong. The SD card supplied in the kit is a 32GB one. Just like the extras I bought.

I'm gonna go to the download section and download Raspian on my laptop. I have a SDHC slot there.

-Mike

Return to “C/C++”