Compile GLib C program

Compile GLib C program

GLib is required GTK + utility library, but can also be used independently in a non-GUI applications. This article describes how to compile the C program in Linux GLib. It also shows how the version number installed on your system GLib install the correct version of the document, and how to find the currently installed GLib library.

Commands and libraries used herein has been tested on Linux Mint 18, but it should run on all Linux distributions based on Debian (including Ubuntu). Before compiling, make sure you have installed the GLib development libraries on your system:

sudo apt-get install libglib2.0-dev

 

Compile a simple GLib C program

A simple "Hello World" C Program The following is a sample program written using GLib functions. Listing below shows the instructions for Linux in the compiler.

 

#include <glib.h>

int main(void)
{
    g_print("Hello, world!\n");
    return 0;
}

 To compile the above program using the following command at the command line:

gcc main.c `pkg-config --cflags --libs glib-2.0` -o hello

In the above lines using anti-quotation marks ( ') instead of a single quote (') and the pkg-config option enclosed. Or use:

gcc main.c $(pkg-config --cflags --libs glib-2.0) -o hello

You can run the following command line named hello compiled program :

./hello

Help install GLib

Install GLib version installed on the system GLib help documentation.

sudo apt-get install libglib2.0-doc

You can view the help file in Devhelp in. Use the following command to install Devhelp:

 

sudo apt-get install devhelp

Can under the Linux Mint 18 Mate menu menu → program → Devhelp found under Devhelp . The following figure shows the opening in Devhelp GLib Reference Manual.

 

 

 

Find the version number of GLib

Click Devhelp left pane GLib Reference Manual , should display the version number of the document, the version number of the library should be installed version number, respectively. There are two other methods to determine the installed version of the library - using pkg-config and programmatically determined as follows.

Use pkg-config

Enter the following command in a terminal window displays the currently installed version of GLib.

 

pkg-config --modversion glib-2.0

Programmatically

When you compile and run this program, it will display the current version of GLib.

#include <glib.h>

int main(void)
{
    g_print("glib version number is %d.%d.%d\n", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
    g_print("GLIB_MAJOR_VERSION = %d\n", GLIB_MAJOR_VERSION);
    g_print("GLIB_MINOR_VERSION = %d\n", GLIB_MINOR_VERSION);
    g_print("GLIB_MICRO_VERSION = %d\n", GLIB_MICRO_VERSION);
    return 0;
}

Compiled using:

gcc main.c`pkg-config --cflags --libs glib-2.0` -o version

Either:

gcc main.c $ (pkg-config --cflags --libs glib-2.0) -o version

run:

 

Coin sample program GLib C

Another GLib C program for simulating a coin toss. When you run the program, it will "  Heads" or "  Tails" printed to the terminal window. Throwing a coin heads or tails ) g_random_boolean ( function determined .

 

#include <glib.h>

int main(void)
{
    gboolean result;
    
    result = g_random_boolean();
    if (result == TRUE) {
        g_print("Heads\n");
    }
    else {
        g_print("Tails\n");
    }
    return 0;
}

Compile using:

gcc main.c `pkg-config --cflags --libs glib-2.0` -o coin_toss

Or:

gcc main.c $(pkg-config --cflags --libs glib-2.0) -o coin_toss

Run:

./coin_toss

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/dgwblog/p/12152816.html