Author Topic: Help:undefined reference to `_XXXX` while the codes uses XXXX.  (Read 9936 times)

catdog

  • Guest
Help:undefined reference to `_XXXX` while the codes uses XXXX.
« on: September 25, 2008, 04:52:00 pm »
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:

Code
/* 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:

Quote
-------------- 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:





Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Help:undefined reference to `_XXXX` while the codes uses XXXX.
« Reply #1 on: September 25, 2008, 06:36:59 pm »
"pkg-config --cflags --libs glib-2.0" to the compiler setting
I don't really know pkg-config. But from what I see you only provide the required compiler options. The linker does know nothing about GTK. You need to pass the required linker options to the linker (under linker options, obviously), too.
« Last Edit: September 25, 2008, 06:38:30 pm by MortenMacFly »
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Help:undefined reference to `_XXXX` while the codes uses XXXX.
« Reply #2 on: September 25, 2008, 06:50:19 pm »
You should get the compiler flags with
Code
`pkg-config --cflags glib-2.0`
and the linker flags with 
Code
`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 "`".

catdog

  • Guest
Re: Help:undefined reference to `_XXXX` while the codes uses XXXX.
« Reply #3 on: September 26, 2008, 07:43:31 am »
 :DThank you. The problem have been solved!
But there is still a warning:
Quote
-------------- 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:
Code
	g_print("%ld\tall:%.2f seconds was used!\n",x,g_timer_elapsed(timer,NULL));

What is it wrong here?

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Help:undefined reference to `_XXXX` while the codes uses XXXX.
« Reply #4 on: September 26, 2008, 07:51:27 am »
Code
	g_print("%ld\tall:%.2f seconds was used!\n",x,g_timer_elapsed(timer,NULL));

What is it wrong here?


It's not C::B related, but it's exactly what the warning says: you format a variable of type "gint" as "long int".
And "gint" is afaik a typedef for "int" and not "long int". So removing the "l" in "%ld" should remove the warning.

tuco2009

  • Guest
Re: Help:undefined reference to `_XXXX` while the codes uses XXXX.
« Reply #5 on: May 01, 2010, 08:36:49 am »
Ihave same problem when run make:
Code
/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?

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Help:undefined reference to `_XXXX` while the codes uses XXXX.
« Reply #6 on: May 01, 2010, 09:03:43 am »
What have i do?

Read the documentation or ask in a related forum.

Note: our forum is not dedicated to answer general programming questions or questions related to special toolkits or libs.
It's "just" a C::B forum, and C::B is "just" an IDE and not a compiler, toolkit, library or whatever.