QNX环境搭建完成了,准备编译几个常用的基础库,结果一不小心选了个最难的glib,大致的编译过程整理如下:
1. 环境变量
修改qnx660-env.sh,增加pkgconfig设置:
PKG_CONFIG_PATH="$QNX_TARGET/usr/lib/pkgconfig"
PKG_CONFIG_SYSROOT_DIR="$QNX_TARGET"
CC="arm-unknown-nto-qnx6.6.0eabi-gcc --sysroot=$QNX_TARGET"
export PKG_CONFIG_PATH PKG_CONFIG_SYSROOT_DIR CC
保存之后:
$source qnx660-env.sh
2. configure
进入glib目录,执行:
./configure --prefix=/usr --host=arm-unknown-nto-qnx6.6.0eabi
3. 修正错误
3.1 checking for libintl.h… no
下载gettext,编译安装后解决
3.2 configure: error: cannot run test program while cross compiling
有几个问题会导致这个错误,原因是arm-gcc编译出来的程序无法在主机上运行,解决方法是加上configure时,加上cache文件,关闭这个验证过程:
$vim qnx.cache
glib_cv_stack_grows=no
glib_cv_uscore=no
ac_cv_func_posix_getpwuid_r=no
ac_cv_func_posix_getgrgid_r=no
configure加入参数–cache-file=qnx.cache
3.3 checking for res_query… configure: error: not found
res_query在QNX系统中,是在socket库中实现的,这个要修改configure脚本
到此configure基本上就能通过了,如果提示缺msgfmt,需要在host上安装gettext,交叉编译出搂msgfmt无法运行。
接下来就是编译了。
4. 编译
4.1 pthread.h:41:2: error: #error POSIX Threads needs P1003.1b-1993 or later
在configure时,加上CFLAGS=”-D_QNX_SOURCE”
4.2 gclosure.c:27:17: fatal error: ffi.h: No such file or directory
gobject的跨语言封装使用了ffi,下载libffi,编译安装
4.3 gunixmounts.c:718:2: error: #error No _g_get_unix_mounts() implementation for system
glib未实现qnx的_g_get_unix_mounts(),先写个空函数,其它的类同。
至此应该能编译过了,执行:
make install DESTDIR=$QNX_TARGET
5.测试程序
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
/*
* === FUNCTION ======================================================================
* Name: callback
* Description:
* =====================================================================================
*/
static gboolean callback( gpointer user_data )
{
g_message( "Hello, glib world" );
sleep( 1 );
return G_SOURCE_CONTINUE;
} /* ----- end of static function callback ----- */
/*
* === FUNCTION ======================================================================
* Name: main
* Description:
* =====================================================================================
*/
int main ( int argc, char *argv[] )
{
GMainLoop * _mainloop = g_main_loop_new( NULL, FALSE );
g_idle_add( callback, NULL );
g_main_loop_run( _mainloop );
g_main_loop_unref( _mainloop );
return 0;
} /* ---------- end of function main ---------- */
编译:
$CC test.c `pkg-config --cflags --libs glib-2.0`
运行:
/a.out