Configure C and GTK in Eclipse for Windows and Ubuntu11.10

Source: Internet
Author: User
Tags gtk
It took a long time to compile the C program using GTK in Eclipse. I checked a lot of information and found that most of the methods on the Internet were not good. I don't know if I am lucky or what's going on. After several hours of hard work, we finally set up the gtk environment in both Windows and Ubuntu. Here I will write down my experience. It is also convenient for others to refer. Run the GTK program in C language in Eclipse in Ubuntu. 1. Download the related package sudoapt-getinstallgnome.

It took a long time to compile the C program using GTK in Eclipse. I checked a lot of information and found that most of the methods on the Internet were not good. I don't know if I am lucky or what's going on. After several hours of hard work, we finally set up the gtk environment in both Windows and Ubuntu. Here I will write down my experience. It is also convenient for others to refer.

Run the GTK program in C language in Eclipse in Ubuntu

1. Download related packages

Sudo apt-get install gnome-core-devel build-essential libgtk2.0-dev libgtk2.0-doc

2. Test code

Open Eclipse, create -- C Project -- enter Project name, and select Hello World ansi c Project in Executable. By default, the Toolchains of Linux GCC are selected, click Finish. A sample code of Hello World is displayed. Replace the code of the "oh" function with the following test code. (In fact, you don't need to use the Hello World template. This is just a simplified process .)

  1. # Include
  2. IntMain (IntArgc,Char* Argv [])
  3. {
  4. GtkWidget * window;
  5. Gtk_init (& argc, & argv );
  6. Window = gtk_window_new (GTK_WINDOW_TOPLEVEL );
  7. Gtk_widget_show (window );
  8. Gtk_main ();
  9. Return0;
  10. }

3. Configure Eclipse

(1) Select the gtk Project and click Project -- Properties -- C/C ++ Build -- Settings -- gcc c Compiler -- Miscellaneous in the menu bar, add 'pkg-config-cflags gtk +-2.0 'to Other flags'

(2) In the same dialog box, select gcc c Linker -- Miscellaneous and add 'pkg-config-libs gtk +-2.0 'to Linker flags'

(3) Select gcc c Linker and adjust $ {INPUTS} in Command line pattern on the right to the end of $ {COMMAND}, for example: $ {COMMAND }$ {INPUTS }$ {FLAGS }$ {OUTPUT_FLAG }$ {OUTPUT_PREFIX }$ {OUTPUT}

(4) In gcc c Compiler -- nodes des, adding/usr/include/gtk-2.0 and/usr/include/glib-2.0 can eliminate gtk. h unresolved warning, while using the gtk function in the program, there will be a smart prompt.

After the above steps, a GTK program can run.

Run the GTK program in C language in Eclipse in Windows

1. install and configure gtk

(1) download the gtk package for windows. The address is ghost.

(2tk decompress the downloaded gtk0000-bundle_2.24.8-20111122_win32.zip and rename the decompressed folder to gtk, for example, put it on the drive C: \ gtk.

(3) add environment variables: My computer/computer -- Right-click, property -- select the advanced option on the right in the pop-up interface -- select the environment variable -- select the PATH option under the dialog box -- double-click to add C: \ gtk \ bin, note that different variables are separated by semicolons.

(4) Click Start, Enter cmd, open the command line, enter pkg-config -- cflags gtk +-2.0 +, and press enter to display a bunch of directory names, as shown below:

-Mms-bitfields-Ic:/gtk/include/gtk-2.0-Ic:/gtk/lib/gtk-2.0/include-Ic:/gtk/include/atk-1.0-Ic: /gtk/include/cairo-Ic:/gtk/include/gdk-pixbuf-2.0-Ic:/gtk/include/pango-1.0-Ic:/gtk/include/glib-2.0-Ic: /gtk/lib/glib-2.0/include-Ic:/gtk/include/freetype2-Ic:/gtk/include/libpng14

(5) Continue entering pkg-config -- libs gtk +-2.0 in the cmd window, and press enter to display another directory:

-Lc:/gtk/lib-lgtk-win32-2.0-lgdk-win32-2.0-latk-1.0-lgio-2.0-lpangowin32-1.0-lgdi32-lpangocairo-1.0-lgdk_pixbuf-2.0-lpango-1.0-lcairo-lgobject-2.0-lgmodule-2.0-lgthread-2.0-lglib-2.0-lintl

2. C-language gtk template program

Open Eclipse, create a Hello World for the C Project, and replace it with a gtk template, as in the second section of the previous section.

3. In Eclipse, select the gtk Project

(1) Select the gtk Project and click Project -- Properties -- C/C ++ Build -- Settings -- gcc c Compiler -- Miscellaneous in the menu bar, add 'pkg-config-cflags gtk +-2.0 'in the Other flags command line to get the content. You can copy it in the cmd window with the mouse;

(2) In the same dialog box, select MinGW C Linker -- Miscellaneous, add the following content to Linker flags and input 'pkg-config-libs gtk +-2.0 'in the command line;

(3) Select MinGW C Linker and adjust $ {INPUTS} in Command line pattern on the right to the end of $ {COMMAND}, for example:

$ {COMMAND }$ {INPUTS }$ {FLAGS }$ {OUTPUT_FLAG }$ {OUTPUT_PREFIX }$ {OUTPUT}

(4) In gcc c Compiler -- nodes des, add C: \ gtk \ include \ gtk-2.0 and so on (depending on where you extract gtk and the file name after decompression ), gtk can be eliminated. h unresolved warning, while using the gtk function in the program, there will be a smart prompt.

After the above steps, a GTK program can run. As for why I cannot directly use 'pkg-config-libs gtk +-2.0 'In Ubuntu as a parameter of Miscellaneous, I have no reasonable explanation. If anyone can answer this question, thank you.

Related Article

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.