User forums > Using Code::Blocks

Help:undefined reference to `_XXXX` while the codes uses XXXX.

(1/2) > >>

catdog:
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));
}
--- End code ---

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


--- End quote ---
(:shock: I used XXXX but _XXXX is undefined.)

I do not know what is wrong :shock:




MortenMacFly:

--- Quote from: catdog on September 25, 2008, 04:52:00 pm ---"pkg-config --cflags --libs glib-2.0" to the compiler setting

--- End quote ---
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.

Jenna:
You should get the compiler flags with
--- Code: ---`pkg-config --cflags glib-2.0`
--- End code ---
and the linker flags with 
--- Code: ---`pkg-config --libs glib-2.0`
--- End code ---
.

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:
 :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
--- End quote ---

It is in this line:

--- Code: --- g_print("%ld\tall:%.2f seconds was used!\n",x,g_timer_elapsed(timer,NULL));

--- End code ---

What is it wrong here?

Jenna:

--- Quote from: catdog on September 26, 2008, 07:43:31 am ---
--- Code: --- g_print("%ld\tall:%.2f seconds was used!\n",x,g_timer_elapsed(timer,NULL));

--- End code ---

What is it wrong here?


--- End quote ---

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.

Navigation

[0] Message Index

[#] Next page

Go to full version