I installed codeblocks-8.02mingw-setup.exe and gtk-dev-2.12.9-win32-2.exe on WinVista.
Then I added "pkg-config --cflags --libs glib-2.0" to the compiler setting of the build options. ("pkg-config --cflags --libs glib-2.9" output "-IC:/GTK/include/glib-2.0 -IC:/GTK/lib/glib-2.0/include -LC:/GTK/lib -lglib-2.0 -lintl" in Cmd Line)
Then I tried to compile the codes below:
/* until.c 用来测试实用功能 */
#include <glib.h>
int main(int argc, char *argv[])
{
GRand *rand;
GTimer *timer;
gint n;
gint i, j;
gint x = 0;
rand = g_rand_new(); //创建随机数对象
for(n=0; n<20; n++)
{ //产生随机数并显示出来
g_print("%d\t",g_rand_int_range(rand,1,100));
}
g_print("\n");
g_rand_free(rand); //释放随机数对象
//创建计时器
timer = g_timer_new();
g_timer_start(timer);//开始计时
for(i=0; i<10000; i++)
for(j=0; j<3000; j++)
x++;//累计
g_timer_stop(timer);//计时结束
g_print("%ld\tall:%.2f seconds was used!\n",x,g_timer_elapsed(timer,NULL));
}
But it failed and output this:
-------------- Build: Debug in glibhello ---------------
Linking console executable: bin\Debug\glibhello.exe
obj\Debug\main.o: In function `main':
F:/CProject/glibhello/main.c:11: undefined reference to `_g_rand_new'
F:/CProject/glibhello/main.c:14: undefined reference to `_g_rand_int_range'
F:/CProject/glibhello/main.c:14: undefined reference to `_g_print'
F:/CProject/glibhello/main.c:16: undefined reference to `_g_print'
F:/CProject/glibhello/main.c:17: undefined reference to `_g_rand_free'
F:/CProject/glibhello/main.c:19: undefined reference to `_g_timer_new'
F:/CProject/glibhello/main.c:20: undefined reference to `_g_timer_start'
F:/CProject/glibhello/main.c:24: undefined reference to `_g_timer_stop'
F:/CProject/glibhello/main.c:25: undefined reference to `_g_timer_elapsed'
F:/CProject/glibhello/main.c:25: undefined reference to `_g_print'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
10 errors, 0 warnings
(:shock: I used XXXX but _XXXX is undefined.)
I do not know what is wrong :shock:
You should get the compiler flags with `pkg-config --cflags glib-2.0`
and the linker flags with `pkg-config --libs glib-2.0`
.
As Martin wrote put the first into the "Other options" of the projects "Compiler settings" and the second into "Other linker options" of the "Linker settings".
The "-I..."s are the include-dirs for the compiler and the "-L.." and the "-l.."s are the linker search-directory and the libs.
P.S.
Don't forget the backticks "`".
:DThank you. The problem have been solved!
But there is still a warning:
-------------- Build: Debug in glibhello ---------------
Compiling: main.c
F:\CProject\glibhello\main.c: In function `main':
F:\CProject\glibhello\main.c:25: warning: long int format, gint arg (arg 2)
Linking console executable: bin\Debug\glibhello.exe
Output size is 17.27 KB
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 1 warnings
It is in this line:
g_print("%ld\tall:%.2f seconds was used!\n",x,g_timer_elapsed(timer,NULL));
What is it wrong here?
Ihave same problem when run make:
/home/admin/LinuxCNC/emc2-2.3.5/src/hal/user_comps/modbus.c:948: undefined reference to `g_print'
/home/admin/LinuxCNC/emc2-2.3.5/src/hal/user_comps/modbus.c:962: undefined reference to `g_print'
/home/admin/LinuxCNC/emc2-2.3.5/src/hal/user_comps/modbus.c:1012: undefined reference to `g_print'
objects/hal/user_comps/modbus.o: In function `modbus_connect_tcp':
/home/admin/LinuxCNC/emc2-2.3.5/src/hal/user_comps/modbus.c:1231: undefined reference to `g_print'
objects/hal/user_comps/modbus.o: In function `error_treat':
/home/admin/LinuxCNC/emc2-2.3.5/src/hal/user_comps/modbus.c:135: undefined reference to `g_print'
objects/hal/user_comps/modbus.o:/home/admin/LinuxCNC/emc2-2.3.5/src/hal/user_comps/modbus.c:279: more undefined references to `g_print' follow
objects/hal/user_comps/modbus.o: In function `check_crc16':
/home/admin/LinuxCNC/emc2-2.3.5/src/hal/user_comps/modbus.c:437: undefined reference to `g_strdup_printf'
/home/admin/LinuxCNC/emc2-2.3.5/src/hal/user_comps/modbus.c:441: undefined reference to `g_free'
objects/hal/user_comps/modbus.o: In function `receive_response':
/home/admin/LinuxCNC/emc2-2.3.5/src/hal/user_comps/modbus.c:342: undefined reference to `g_print'
/home/admin/LinuxCNC/emc2-2.3.5/src/hal/user_comps/modbus.c:352: undefined reference to `g_print'
/home/admin/LinuxCNC/emc2-2.3.5/src/hal/user_comps/modbus.c:377: undefined reference to `g_print'
/home/admin/LinuxCNC/emc2-2.3.5/src/hal/user_comps/modbus.c:396: undefined reference to `g_print'
/home/admin/LinuxCNC/emc2-2.3.5/src/hal/user_comps/modbus.c:404: undefined reference to `g_print'
objects/hal/user_comps/modbus.o:/home/admin/LinuxCNC/emc2-2.3.5/src/hal/user_comps/modbus.c:1277: more undefined references to `g_print' follow
objects/hal/user_comps/modbus.o: In function `modbus_response':
/home/admin/LinuxCNC/emc2-2.3.5/src/hal/user_comps/modbus.c:537: undefined reference to `g_strdup_printf'
/home/admin/LinuxCNC/emc2-2.3.5/src/hal/user_comps/modbus.c:541: undefined reference to `g_free'
objects/hal/user_comps/modbus.o: In function `preset_multiple_registers':
/home/admin/LinuxCNC/emc2-2.3.5/src/hal/user_comps/modbus.c:847: undefined reference to `g_print'
objects/hal/user_comps/modbus.o: In function `force_multiple_coils':
/home/admin/LinuxCNC/emc2-2.3.5/src/hal/user_comps/modbus.c:796: undefined reference to `g_print'
objects/hal/user_comps/modbus.o: In function `read_input_registers':
/home/admin/LinuxCNC/emc2-2.3.5/src/hal/user_comps/modbus.c:684: undefined reference to `g_print'
objects/hal/user_comps/modbus.o: In function `read_holding_registers':
/home/admin/LinuxCNC/emc2-2.3.5/src/hal/user_comps/modbus.c:665: undefined reference to `g_print'
What have i do?