Use wxWidgets to develop cross-platform GUI programs

Source: Internet
Author: User
Tags gtk wxwidgets

1. Background:

1.1 Introduction

It has been almost five years since I started to use C/C ++ encoding. During this period, I completed numerous projects and wrote numerous programs, in addition, most programs complete the Win32/* nix version at the same time, but cross-platform applications are limited to the Win32 console/* nix textmode mode, I have been sticking to psdk/MFC and cannot do a good job in developing cross-platform GUI programs. Today, I started to explore.

There is no doubt that a relatively ideal cross-platform GUI development library has a great demand. The general options include Java, QT, and fltk (fast light Tool Kit), Fox, SDL (Simple DirectMedia Layer), Allegro, GTK +, kylix (based on QT)/Mozilla framework, and so on.

WxWidgets is a GUI api library that can be called by python, Perl, C ++, Java, Lua, C #, basic, Ruby, and other languages. Home: http://wxwidgets.sourceforge.net

Comparison of 1.2

1.2.1 Java

First of all, Java is platform, and wxWidgets is an api library. Therefore, the comparison between the two may not be equal.
Although Java can implement cross-platform GUI programs, it is essentially not a cross-platform Java language, but a cross-platform Java virtual machine. In other words, Java is not a native code, it is a special language between the compilation language and the scripting language. During the compilation period, only the code for Java VM can be converted, actually compiled into bytecode is completed at runtime (the features of the script language), which means that the Java program needs to be loaded for a long time at the first startup, although Java compilers similar to gjc can be put in place at one time, most of them cannot fully support Java features. WxWidgets is directly compiled into machine code to gain a speed advantage.
In addition, some java libraries use wxWidgets + C ++ for performance reasons, such as wx4j.
The great idea of "write once, run anywhere" seems to be not very well implemented in Java VM. Of course, wxWidgets can't be said to avoid all the problems, but in fact it does well.
In terms of development speed, considering that the UI library selected by bcbx is wxWidgets, we can compare the speed difference between the bcbx and Java Development interfaces.

1.2.2 QT

QT is not a real C ++ program, but a pre-compilation technique called meta object Compiler (MOC. I don't know much about QT.

1.2.3 fltk

Fltk is a lightweight GUI library, and wxWidgets has complete features, such as supporting networks and printing. For a list of wxWidgets features, see "signature.

1.2.4 GTK +

WxWidgets has a version based on GTK +. In addition to the features of GTK +, wxWidgets provides a simple and easy-to-use gui api, which sounds like no problem. Pai_^

1.2.5 others

If you are interested, you can play with all these things, and then write down some text for everyone to share.

If you don't talk nonsense, start a short trip:

2. Installation

For detailed installation guide, you can download the official documentation for further study. The following describes only my solutions:

2.1 Windows platform (Using XP, other windows to be studied)

(1) download and install wxdevcpp:
Http://sourceforge.net/project/showfiles.php? Group_id = 95606 & package_id = 101971 & release_id = 389486
The latest version is wx-devcpp-6.9beta_setup.exe
This is a useful rad tool that integrates wxWidgets in Dev-CPP. From rad design to project management, some BCB are basically available and are constantly being updated, unfortunately, it seems that it does not support platforms other than win. Home: http://wxdsgn.sourceforge.net

Note: If you are interested, try other RAD tools, such as wxglade (platforms other than win are not as easy to use as wxdevcpp), wxdesigner, and dialogblocks. The latter two are commercial software.

Wxdevcpp can be used only to complete the complete process of uidesign, compilation link, and debugging. However, it is obvious that GDB debugging is not easy to use. Replace:

(2) install the independent version of wxWidgets (the latest version is 2.6.3), configure the VC environment to this version, use wxdevcpp only for uidesign, and use VC for debugging, compilation, and control project generation.

This combination can well complete the work.

2.2 Linux platform (use RedHat, other nix to be studied)

WxWidgets provides two options: x11-based and GTK-based. The latter is used in the author's journey.

(1gz download wxgtk-2.6.3.tar.gz, Which is the source code package of the GTK version. Installation Process:

> Guzip wxGTK-2.6.3.tar.gz
> Tar xvf wxGTK-2.6.3.tar.gz
> Cd wxGTK-2.6.3
>./Configure -- With-GTK
(Note: GTK + 2.xis used by default. If you need to use GTK + 1.2, change it to -- With-GTK = 1)
> Make
> Su <type Root Password>
> Make install
> Ldconfig

To uninstall wiwidgets,
> Cd wxGTK-2.6.3
> Su <type Root Password>
> Make uninstall
> Ldconfig

Note: If the GCC version is earlier than 3.3 (GCC -v1_release, download and install wxwidgets-2.6.3-patch-2.tar.gz; otherwise, make will fail; or upgrade gcc

(2) check whether the installation is successful:

> WX-config -- cxx
Normally, output g ++. Otherwise, follow me,
> Cd/usr/local/bin
> Ls WX-config (check whether it exists)
If yes, add/usr/local/bin to the user PATH variable.
If it does not exist, follow me,
> Ln-S/usr/local/lib/wx/config/gtk2-ansi-release-2.6 WX-config

3. Design and Development

Use wxdevcpp, see using WX-devcpp to create the wxWidgets Minimal sample, address: http://sourceforge.net/docman/display_doc.php? Docid = 27235 & group_id = 95606

4. Compile and run

4.1 Windows platform (completed on xp pro)

Because the sample is relatively simple, you can complete the uidesign and compilation link process in wxdevcpp. If the process succeeds, the corresponding EXE file will be generated soon. OK. This part is easy and a pleasant experience.

4.2 Linux platform (RedHat es 3.0)

First, I want to explain the purpose of the author, hoping to quickly transfer the interface program designed and developed on the Win32 platform to the nix platform for operation. This section describes how to quickly generate executable codes for codes completed in section 4.1 on the Linux platform.

Select an option from the official guide:

(1) Using kdevelop and GUI is obviously inefficient, pass;

(2) Use bakefile, which is a third-party automake tool. It claims that it can quickly generate cross-platform makefile. If it is no longer used, it will be annoying. pass;

(3) Write makefile by yourself. I like this. makefile is much written and conforms to the DIY spirit.

Okay, that's it. I started to write makefile. Since the makefile I have previously written is very flexible, I can use it if I want to modify it for reference:

######################################## #######################
# Makefile model for wxWidgets application Vers 1.0
# Created by taiI on 20060520
######################################## #######################

Program = wxtest

Libpath = ./
Objpath = ./
Srcpath = ./
Binpath = ./

Compiler = $ (shell WX-config -- cxx)
Linker = $ (shell WX-config -- cxx)

Defines =-G
Wxcxxflag = 'wx-config -- cxxflags'
Flags = $ (defines) $ (wxcxxflag)

Oslib =
Wxlib = 'wx-config -- libs'
Libs = $ (oslib) $ (wxlib)

Sources = $ (wildcard $ (srcpath) *. cpp)

Objs: =$ (addprefix $ (objpath), $ (patsubst $ (srcpath) %. cpp, %. O, $ (sources )))

Exes = $ (binpath) $ (Program)

. Phony: All clean veryclean rebuild

ALL: $ (exes)

$ (Program): $ (binpath) $ (Program)

$ (Binpath) $ (Program): $ (objs)
$ (Linker) $ (objs )/
$ (Libs)-o $ @

$ (Objpath) %. O: $ (srcpath) %. cpp $ (srcpath) *. h
$ (Compiler) $ (flags)-C $ <-o $ @

Clean:
Rm $ (objpath) *. o

Veryclean: clean
Rm $ (exes)

Rebuild: veryclean all

######################################## ######################

It can be used as a general template for the wxWidgets program makefile. Change the string after program = To Your executable program name.
For more information about makefile, Google is recommended.

After the makefile has been written, let's see if it can be switched. In the project completed in section 4.1 *. CPP ,*. H (except wx * _ private. h ),*. upload XPM (image resource) to a directory on the Linux server, such as wx, and put the makefile you just wrote under wx. Then run the make command under wx.

Wait, the exciting time is up, wait patiently...
If the program goes well (at least I am doing well), the executable program wxtest (or custom) will be generated in the Wx directory soon ).

How wise is xianzhe? Don't be too happy. Run it again,
>./Wxtest
Bad news:
Error while loading shared libraries: libwx_gtk2_xrc-2.6.so.0: cannot open shared object file: no such file or directory

Well, don't be nervous. Some Linux developers may have thought of the problem. That's right. You need to configure the import path for the dynamic library. Action:

> Su <type Root Password>
> VI/etc/lD. So. conf

If this file exists, you only need to add the Wx dynamic library location
/Usr/local/lib (when wxWidgets is installed by default)

If the path does not exist, add all common lib paths (including the Wx lib path ):
/Lib
/Usr/lib
/Usr/local/lib
/Usr/x11r6/lib

Save the file and then re-configure the import sequence of the dynamic library file:
> Ldconfig

(There is another way to solve this problem,
Export LD_LIBRARY_PATH = "/usr/local/lib: $ LD_LIBRARY_PATH"
However, re-login fails.
)

Obviously, if the file in your system contains the Wx lib path, the above error message will not appear.

Okay, run another one (under XWindow Of course). It's great. Congratulations...

5. Summary

Finally, we found a better way to implement cross-platform GUI programs. After experience testing, the GUI program developed by wxWidgets on Win32 can quickly run in Linux, I don't know if you have noticed it. The code hasn't changed. Oh, it's my goal. It seems that today we are not busy with the day. We have to cook a few dishes and drink a little wine to celebrate.

 

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.