Ubuntu under Code::Blocks compilation run a simple gtk+2.0 project

Source: Internet
Author: User
Tags gtk

Before you do this, you need to install some of the necessary software first.
GCC is installed by default under Ubuntu, but the required header file is missing, and you can install the Build-essential suite by entering the following command on the commands line:
sudo apt-get install build-essential
To install the GTK Dev kit using gtk+2.0, enter the following instructions on the command line to install the GTK dev Kit Libgtk2.0-dev:
sudo apt-get install Libgtk2.0-dev
After the installation is complete, you can use Pkg-config to view the relevant compilation environment information for GTK:
Pkg-config--cflags--libs gtk+-2.0
Ubuntu installation code::blocks can be done by typing the following command on the line:
sudo apt-get install Codeblocks

The program HELLOWORLD.C shows how to open a desktop window program.

#include <gtk/gtk.h>int main (intChar *argv[]) {    *window;    Gtk_init (&argc, &argv);     = gtk_window_new (gtk_window_toplevel);    Gtk_widget_show (window);    Gtk_main ();     return 0 ;}
HELLOWORLD.C

We can compile the program at the command line by entering the following information:
Gcc-o HelloWorld helloworld.c ' pkg-config--cflags--libs gtk+-2.0 '
Enter the following command at the command line and you will see a window appears:
./helloworld

Create an empty project in Code::Blocks (where I named the project "HelloWorld"), creating a new Main.c file in the project with the same content as the Helloworld.c file above. Running the HelloWorld project at this point does not work properly, and in build messages you will be prompted with the following error:
Fatal error:gtk/gtk.h:no such file or directory
This is because we did not provide the necessary parameters ' Pkg-config--cflags--libs gtk+-2.0 ' When we compiled the HELLOWORLD.C program as before.
Open the HELLOWORLD.CBP file in the directory where the project is located (this file is similar to makefile, which contains some configuration information about the project) and is added in the project item's compiler item
<add option= "' Pkg-config gtk+-2.0--cflags '"/>
To add a linker item to a project item
<Linker>
<add option= "' Pkg-config gtk+-2.0--libs '"/>
</Linker>
Run the HelloWorld project again to get the same window that was previously manually compiled to run HELLOWORLD.C.
Complete HELLOWORLD.CBP code (after adding GTK parameters):

<?XML version= "1.0" encoding= "UTF-8" standalone= "yes"?><Codeblocks_project_file>    <FileVersionMajor= "1"Minor= "6" />    <Project>        <Optiontitle= "HelloWorld" />        <OptionPch_mode= "2" />        <Optioncompiler= "GCC" />        <Build>            <Targettitle= "Debug">                <OptionOutput= "Bin/debug/helloworld"Prefix_auto= "1"Extension_auto= "1" />                <OptionObject_output= "obj/debug/" />                <Optiontype= "1" />                <Optioncompiler= "GCC" />                <Compiler>                    <ADDoption= "-G" />                </Compiler>            </Target>            <Targettitle= "Release">                <OptionOutput= "Bin/release/helloworld"Prefix_auto= "1"Extension_auto= "1" />                <OptionObject_output= "obj/release/" />                <Optiontype= "1" />                <Optioncompiler= "GCC" />                <Compiler>                    <ADDoption= "-o2" />                </Compiler>                <Linker>                    <ADDoption= "-S" />                </Linker>            </Target>        </Build>        <Compiler>            <ADDoption= "' Pkg-config gtk+-2.0--cflags '" />            <ADDoption= "-wall" />        </Compiler>        <Linker>            <ADDoption= "' Pkg-config gtk+-2.0--libs '" />        </Linker>        <Unitfilename= "MAIN.C">            <OptionCompilervar= "CC" />        </Unit>        <Extensions>            <code_completion/>            <Debugger/>        </Extensions>    </Project></Codeblocks_project_file>
HELLOWORLD.CBP


Learning materials:
Ubuntu under Install GTK http://openhome.cc/Gossip/GTKGossip/GTKUnderUbuntu.html
Programming language: Introduction to GTK Programming http://www.turbolinux.com.cn/turbo/wiki/doku.php?id= programming Language: Introduction to GTK programming
gtk+2.0 Tutorial http://runmediaprocess.googlecode.com/svn/trunk/dataBase/UI Development/gtk/gtk_2.0_tutorial (Chinese version). pdf

Ubuntu under Code::Blocks compilation run a simple gtk+2.0 project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.