Discussion on the use of Glib library in C language programming under Windows system

  • 2020-05-09 18:55:03
  • OfStack

In this world of C transformation, there are many useful libraries, of which the best known and most common (implementations across multiple platforms include Windows, and many useful programming libraries do not provide an implementation of Windows) is GLib, which has an implementation thread.

glib library is the most commonly used C language function library on Linux platform, which has good portability and practicability.
glib is the basis of the Gtk + library and Gnome. glib can be used on multiple platforms, such as Linux, Unix, Windows, etc. glib provides an alternative to many standard, commonly used C language constructs.
If you want to use the functions in the glib library in your program, you should include the glib.h header (glib.h is already included in the gtk.h and gnome.h headers).

But, because it is in Chinese, watching people not crooked nuts, most novice programmer or use Windows environment in China, also don't try so hard, many things, later will have a program as an example, which is a simple application for multithreading knowledge to write a backup software, the realization of the thread is using Windows own interface, all these interfaces can be found in the MSDN lookup to the corresponding document.

Configuration of the Glib library under Windows

The reason why I don't say *nix configuration is because it's too stupid to configure, especially Ubuntu, which is basically configured with the network.
The stable version 2.28.8 is used, and the highest stable version available so far is 2.46.x
The pre-configured glib is packaged in my network disk, which can be downloaded directly, and the path of IDE can be used. This is available for the Visual C++ series compiler. If you use the MinGW series compiler, you need to recompile
If you want to configure yourself, you can also go to this website to download, or go to the GNU project home page to download the latest source code and project files for self-compilation, there are many ways, do not use the existing 2 base system but choose to compile by yourself probably want to use MinGW, MinGW project home page is also introduced
If you have too few resources, you can refer to how to compile the GTK project, since GLib was originally part 1 of GTK, but has since become a separate part.

Microsoft's universal compiler Visual Studio does not support the standard beyond C89 (C90), but it implements the features early (there is no option to open the standard, but the features described in the new standard are available, usable, and even more advanced)

So the following backup program will be written using Visual Studio 2013.
Configuration glib - 2.28.8
Download the compiled binary package, preprocess it (some operations, not to say, there are tutorials on the Internet, remember to use Google, or go to the blog garden to find similar, but the version is older and may have some way out of my use, but you can follow), and then configure the path into the project:
Create one Win32 program and create the property sheet in the property manager (find at the bottom of the left column) (1 for Debug and 1 for Release, all set the same)
Open the new property sheet
General attributes - > VC + + directories > Include directory - > Edit and add the glib\ glib2.28 \include directory in the downloaded file. If you are worried, you can add another glib\ glib2.28 \lib\ glib-2.0 \include directory
General attributes - > VC + + directories > Library directory - > Edit and add the glib\ glib2.28 \lib directory
General attributes - > The linker - > Input - > Additional dependencies add all the glib\ glib2.28 \lib files in the directory.lib, which means that the names of these files are entered manually, if using my version
gio-2.0.lib glib-2.0.lib gthread-2.0.lib gmodule-2.0.lib gobject-2.0.lib
General attributes - > C/C++- > Code generation - > The runtime opens multithreading /MT
Okay! a
Under the rest 1

In fact, for programmers of C, the most important thing is to use series 1 open source library, rather than the pursuit of new standard, because the lower the standard is, the easier it is to cross the platform. For libraries, this is a series 1 practical data structure and algorithm summarized by predecessors, and even a practical framework. We don't necessarily need to configure them, but we take some of their technology from them and convert it to our own code. After all, libraries are overkill for many programmers who write programs, but sometimes we have to use the necessary data structures and algorithms.
This few years in college, perhaps because of but is a crane tail 1, so I can't feel the teaching, teacher professor brings but also made me deeply exposed to open source, open source gave me a lot, such as wider programming ideas, a broader mind, more progress, more friend. Of course, I also know my own smallness.
Answer is a lot of people (such as zhihu and questioner), are mentioned to see C source code, but this for beginners, and even now I feel is not a easy job, let alone the early introduction of the classmate, especially the great god for many of the last century, in order to save space and improve efficiency, it is no means! Although some usage is acceptable today, can you tell from the first eye that in order to construct a red-black tree node, both the tree pointer and the color information of the node are hidden in a pointer address?


 /*  Suppose you have 1 A pointer to a node  p_node */
 node_color = p_node->node_color & 1; /*  The principle is to use the last 1 position bit To store the color  */

In Linux, p_node- > node_color is set to be unsigned long shaping, storing Pointers and color information in integer form rather than pointer types.

  node_pointer = (node_type*)p_node- > node_color & ~ 3; Clear the bit value on the last two bits */
That is to clear the color information, leaving the value of the pointer, that is, the address.

Why? As long as we can make sure that the node is created in 32-bit / 64-bit alignment, we can make sure that the last two /3 bits are empty and will never be used.


 /* 32 position  */
 sizeof(void*); /*  is  4 */
 /* xxxx xxxx xxxx xxxx xxxx xxxx xxxx xx00 */
 /* 64 position  */
 sizeof(void*); /*  is  8 */
 /*  In front of the omitted 48 position  xxxx xxxx xxxx x000 */

What that means is that for Pointers, because the compiler wants to be efficient at addressing so that when it allocates addresses, it allocates the addresses in memory in multiples of the size of the pointer, and that leads to the fact that several of the bit values of pointer variables in programs with different bits are not used, so it stores them.

The specific situation, the detailed explanation on the Internet is more than 10 points, a start is good. But is that really what we should be doing in the first place?
is
Well, in many cases, the C language gives us functions that are not safe and reliable, but what do we do when we can't use the functions provided by the new standard (10 out of 10)? Of course is to write their own, how to write more perfect, natural is to see how others write, rather than their own 1 smear disorderly, because the fact that they think good to the end will fall 1, although not a bad thing.
The easiest way to do this is to wrap macro 1 and do some preprocessing, or someone who doesn't like macro mechanics will choose to wrap it with a function.
note

Write it at the end and fill in the holes you dug in front of you.
Don't know whether I intentionally, 1 GNU son home page of the project, to find, it's hard to find) the corresponding download link of the project, is also looking at how to light how cattle, how to use, but just don't tell you where to go next, at that time, first make sure you have to under the name of this software, then go to GNU project program to find the list in the home page, where one can find, and not in the introduction page points, the results could not find any.
The most typical example is an open source software called GMP, which is used to compile the dependencies used by MinGW.
The reason I used 2.28.8 instead of 2.46.x is because I really don't want to compile on Windows, because most of the time I write on Linux, so I'm going to be lazy.
As for whether my files are poisonous or not, I said poisonous, there is a kind of poison called let you reprogram with Windows.
Well, I'll admit that Visual Studio is the unsurpassed compiler of the universe.
At the end of

I'll use a backup program throughout the rest of part 3

Operating system: Windows
Cross-platform: no
API call: Win32 API
Compiler: Visual Studio 2013
Language: Pure C Programing Language
Will be introduced in 1, often in the open source code to see 1 strange things, for example


 #ifdef __cplusplus
 extern "c" {
 #endif
 ...
 #ifdef __cplusplus
 }
 #endif


Related articles: