更新时间:2023-11-17 08:54:28
构建方式一定存在问题.要编译使用GLib的C程序,您需要包 libglib2.0-dev
.您可以直接安装它,也可以安装 libgtk2.0-dev
,将其作为依赖项引入.这样您便有了所需的软件包.
编译GLib程序的正确方法是将 -I
与GLib包含文件的路径一起使用.一个示例(来自如何在askubuntu上编译helloworld GLib程序?):
gcc $(pkg-config --cflags --libs glib-2.0)hello_glib.c
这应该让您编译该程序:
#include< stdio.h>#include< glib.h>int main(int argc,char ** argv){GList * list = NULL;list = g_list_append(list,"Hello world!");printf(第一项是'%s'\ n",g_list_first(list)-> data);返回0;}
您收到的错误表明您没有正确设置包含路径( -I
).如何执行此操作取决于您的构建系统/IDE.
在Code :: Blocks中,必须在适当的配置对话框中设置包含路径和链接器选项.运行 pkg-config --cflags --libs glib-2.0
,它将输出类似
-I/usr/include/glib-2.0 -I/usr/lib/i386-linux-gnu/glib-2.0/include -lglib-2.0
必须在项目的编译器选项中设置 -I
之后的目录(应在Project-> Build Options-> Search Directories下),并在 -l 必须在链接器设置中设置.另一种选择是创建一个Makefile,然后让Code :: Blocks使用它.
例如参见问:什么时候需要知道什么使用Code :: Blocks Wiki中的第三方库?.
I have a sample C project that use GLib Library. In that source code, it use :
#include <glib.h>
When I compile, I found this error : "Glib.h : no such file or folder". I have google and find out that I should install this lib. So I use those command:
apt-get install libgtk2.0-dev
apt-get install glade
After that, I have checked and see already exist this header file in my system: usr/include/glib-2.0/glib.h
But when I compile, I still meet problem above.
So I have change include line to :
#include <glib-2.0/glib.h>
So, after that, when I compile, I meet error inside glib.h
header :
#ifndef __G_LIB_H__
#define __G_LIB_H__
#define __GLIB_H_INSIDE__
#include <glib/galloca.h>
#include <glib/garray.h>
// more code here
glib/galloca.h : no such file or directory.
Because this error is inside system header file, I cannot modify anymore and still cannot compile.
I don't know how to fix this. I have read some post, that they change makefile. But, because my project is compiled automatically by IDE (CodeBlock) and I cannot really write a makefile, so that method doesn't suitable for me.
Please tell me a way to fix this.
Thanks :)
There must be some problem with how you build. To compile C programs that use GLib, you need package libglib2.0-dev
. You can either install it directly, or install libgtk2.0-dev
, which pulls it in as a dependency. So you have the packages you need.
The correct way to compile a GLib program is to use -I
with the path to the GLib include files.
An example (from How to compile a helloworld GLib program? on askubuntu):
gcc $(pkg-config --cflags --libs glib-2.0) hello_glib.c
This should let you compile this program:
#include <stdio.h>
#include <glib.h>
int main(int argc, char** argv) {
GList* list = NULL;
list = g_list_append(list, "Hello world!");
printf("The first item is '%s'\n", g_list_first(list)->data);
return 0;
}
The errors you are getting indicate that you are not setting the include path (-I
) correctly. How to do this depends on your build system/IDE.
In Code::Blocks, you must set the include path and the linker options in the appropriate configuration dialog. Run pkg-config --cflags --libs glib-2.0
, which will output something like
-I/usr/include/glib-2.0 -I/usr/lib/i386-linux-gnu/glib-2.0/include -lglib-2.0
The directories after -I
must be set in the compiler options of your project (should be under Project -> Build Options -> Search Directories), and the names after -l
must be set in the linker settings. Another option is to create a Makefile, and let Code::Blocks use that.
See e.g. Q: What do I need to know when using 3rd party libs? in the Code::Blocks Wiki.