我有一个程序调用具有未定义参数的函数,如下所示:
#include <stdargs.h>
... /* code */
int main () {
GArray *garray = g_array_new (FALSE, FALSE, sizeof (char *));
/* the code above initialize the GArray, and say that the garray expect a pointer to char. */
function_name (garray, "arg2", "arg3" /* and so on ... */);
... /* code */
}
Run Code Online (Sandbox Code Playgroud)
请注意,""之间的参数是字符串,因此,在function_name中:
static void function_name (GArray *garray, ...) {
... /* code */
char *data;
data = va_arg (garray, gchar *);
g_array_append_val (garray, data);
... /* code */ …Run Code Online (Sandbox Code Playgroud) 我似乎无法使用glib.h编译这个基本程序...
#include glib.h
#include stdio.h
int main ()
{
return ((glib_major_version) || (glib_minor_version) || (glib_micro_version)); ;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
glib.h位于/usr/local/include/glib-2.0中
所以我编译了
$ gcc -v -c -mcpu=v9 -I/usr/local/include/glib-2.0 testme2.c
Run Code Online (Sandbox Code Playgroud)
现在我错过了glibconfig.h.但它是在/usr/local/lib/glib-2.0/include/glibconfig.h
奇怪的是glibconfig.h是/usr/local/lib/glib-2.0/include目录中唯一的文件,更奇怪的是它不在/usr/local/include/glib-2.0目录中
这是一些更多的错误消息......
from /usr/local/include/glib-2.0/glib.h:32,
from testme.c:40:
:34:24: glibconfig.h: No such file or directory
Run Code Online (Sandbox Code Playgroud)
这是/usr/local/include/glib-2.0/glib/gtypes.h的摘录
ifndef __G_TYPES_H__
define __G_TYPES_H__
include glibconfig.h
include glib/gmacros.h
G_BEGIN_DECLS
typedef char gchar;
typedef short gshort;
Run Code Online (Sandbox Code Playgroud)
问题是GCC应该如何找到glibconfig.h?
我正在处理来自哈希表的大量数字.我想知道将它们添加到常量(100)中考虑可移植性的好方法.Glib的文档强调,使用GINT_TO_POINTER不会以任何方式移植.任何想法,将不胜感激!
gpointer v, old_key;
gint value; // ?
if(g_hash_table_lookup_extended(table, key, &old_key, &v)){
value = GPOINTER_TO_INT(v); // ?
value = value + 100;
}
g_hash_table_replace(table, g_strdup(key), GINT_TO_POINTER(value)); // ?
Run Code Online (Sandbox Code Playgroud) 我无法弄清楚如何使用GLib(GIO?)列出给定目录中的所有文件(文件名?).没有好的文档或教程.任何代码片段都是受欢迎的.如果使用GLib执行此操作是不可能的(或太麻烦),是否有任何好的C或C++第三方库来执行此操作.除了Boost.FileSystem,因为我在minGw上编译boost.filesystem时遇到了很多麻烦.当然有"dirent.h"作为最后的手段,但它不是标准的,虽然它在gnu gcc(mingw)上受支持,但它不包含在MSVC工具链中.那么建议使用dirent.h吗?欢迎任何好解决方案.
注意:我不知道这是否应标记为c或c ++.我使用的是c ++,但glib是ac库.
我在glib文档中找不到有关线程安全的任何信息。我猜这意味着我可能应该假设它不是线程安全的,但是我不确定应该锁定哪些共享资源。
有人使用glib有线程安全方面的经验吗?我可以使用什么准则来确保我的glib代码是线程安全的?
谢谢!
我刚刚开始学习GTK。我正在查看gtk + 3.0.0的源代码,发现某些gtk对象类型的_get_type()方法的实现,但有些则没有此方法的实现,例如GtkRange。有什么理由吗?据《 GObject参考手册》了解,_get_type()方法在类型系统中注册对象类型。
我正在开发一个服务应用程序(rpm 守护进程)。此服务提供一些实用程序(文件传输(FT)等)。
现在的情况是,当服务启动时,我为 FT 实用程序创建了一个单独的 g_thread。在 FT 实用程序代码中,我出于某种目的添加了 g_timeout_add()。
现在,我只想在 FT 线程中运行 (g_timeout_add) 中给出的源函数。目前它在我不想要的默认(主)上下文中运行。
我怎样才能实现它?
在GObject 手册的样板代码一章中,当使用G_DECLARE_FINAL_TYPE将 ViewerFile 声明为最终类型时,我们如何向其添加公共数据,因为它隐藏在未包含的viewer-file.c后面?
g_timeout_add_seconds(1, (GSourceFunc) update_status_tray, NULL);
Run Code Online (Sandbox Code Playgroud)
上面的代码会每秒刷新状态托盘,但我需要每半秒刷新一次.如何更改此行以获得0.5秒?
GLib库给了我无限的char**篇幅。如何遍历它,打印数组中的每个字符串?
我已经尝试了以下代码,但是即使数组包含多个字符串,它也只会给我第一个字符串。
#include <stdio.h>
#include <glib.h>
static gchar** input_files = NULL;
static const GOptionEntry command_entries[] = {
{"input", 'i', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING_ARRAY, &input_files, "Input files", NULL},
{NULL}
};
int main(int argc, char **argv) {
GOptionContext* option_context;
GError* error;
option_context = g_option_context_new(NULL);
g_option_context_add_main_entries(option_context, command_entries, NULL);
if (!g_option_context_parse(option_context, &argc, &argv, &error)) {
g_printerr("%s: %s\n", argv[0], error->message);
return 1;
}
g_option_context_free(option_context);
if (input_files) {
for (int i = 0; input_files[i]; i++) {
printf("%s", input_files[i]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
$ ./a.out -i One Two …Run Code Online (Sandbox Code Playgroud) glib ×10
c ×7
c++ ×2
gobject ×2
gtk ×2
filesystems ×1
gcc ×1
gtk3 ×1
oop ×1
performance ×1
portability ×1