1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | glibc和libc都是Linux下的C函数库, libc是Linux下的ANSI C的函数库; glibc是Linux下的GUN C的函数库; 1.ANSI C: 美国国家标准学会(American National Standards Institute) C是基本的C语言函数库,包含了C语言最基本的库函数。 //脱离glibc的demo.c #include<stdio.h> int main() { printf("hello world\n"); return 0; } # gcc demo.c 2.Glib C:GNU自由软件组织 而GNUC函数库是一种类似于第三方插件的东西,由于Linux是用C语言写的,所以Linux的一些操作是用C语言实 现的,所以GUN组织开发了一个C语言的库用于我们更好的利用C语言开发基于Linux操作系统的程序。其实我们可 以把它理解为类似于Qt是一个C++的第三方函数库一样。不过现在的不同的Linux的发行版本对这两个函数库有不 同的处理方法,有的可能已经集成在同一个库里了,例如Ubuntu就只有glibc。 C语言自身并没有提供IO,内存管理,字符串操作等类似的机制。作为弥补,C语言有一个标准库帮助C语言实现 这些机制。我们在编译C程序的时候基本上都需要链接到这些库文件。 GNU C Library定义ISO C标准还有其他一些标准(POSIX)规定的函数库。 //glibc demo.c static gint repeats = 2; static gint max_size = 8; static gboolean verbose = FALSE; static gboolean beep = FALSE; static gboolean rand = FALSE; static gchar *string; static GOptionEntry entries[] = { { "repeats", 'r', 0, G_OPTION_ARG_INT, &repeats, "Average over N repetitions", "N" }, { "max-size", 'm', 0, G_OPTION_ARG_INT, &max_size, "Test up to 2^M items", "M" }, { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL }, { "beep", 'b', 0, G_OPTION_ARG_NONE, &beep, "Beep when done", NULL }, { "rand", 0, 0, G_OPTION_ARG_NONE, &rand, "Randomize the data", NULL }, { "str_test", 's', 0, G_OPTION_ARG_STRING, &string, "test the stirng", NULL}, { NULL } }; int main (int argc, char *argv[]) { GError *error = NULL; GOptionContext *context; context = g_option_context_new ("- test tree model performance"); g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); // g_option_context_add_group (context, gtk_get_option_group (TRUE)); if (!g_option_context_parse (context, &argc, &argv, &error)) { g_print ("option parsing failed: %s/n", error->message); exit (1); } /* ... */ } //GOptionEntry的结构定义为: typedef struct { const gchar *long_name; // 完整命令 如:--name gchar short_name; // 简写命令 如:-n gint flags; // GOptionFlags枚举的值 GOptionArg arg; // GOptionArg枚举的值 gpointer arg_data; // 解析出来的数据,所要存储的位置 const gchar *description; // 参数描述,--help可以查看到 const gchar *arg_description; } GOptionEntry; //编译 # gcc test.c -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include \ -L/usr/lib/x86_64-linux-gnu -lglib-2.0 -o test # ./test -r 10 -b -s test 注意: -b后面不能跟参数,因为这个参数类型为:G_OPTION_ARG_NONE。存储它的变量是一个bool型的值,当有这个 参数的值,这个bool值是TRUE,否则是FALSE。 |