Skip to main content
gamedev.net
🔒 Locked

Error while linking a static library

Started by Linkin_T 7 replies 10.4k views
Original Post
Linkin_T
Linkin_T
Sorry if i sound somehow like a newbie, but the fact is that i AM newbie. And today i tried to do a static library with a code snippet i did yesterday. I made the header and the source file in C correctly to keep compatibility with C++ compilers. So, i compiled the source file and my static library was created. Then, i wanted to test my code and i created another src file, including the header and linking the library into my binary. The result is that the linker seems to not link it to the executable, it gives me an undefined reference for any of the methods of the library i try to execute. I´ve put the library in the right place, and nothing... I´m using Dev-Cpp and mingw32. Here is a link to a zip file with my library and the complete source code. If someone could try to compile and link it to some test program, and see what happen, i would be very grateful ^^ Here is the code for my binary, compiled with the options -lmingw32 -dary for the linker:
#include <stdio.h>
#include <stdlib.h>
#include "dary.h"

int main(int argc, char *argv[])
{
  dary* ary;
  dary_init(ary, 10, sizeof(int));
  dary_dispose(ary); 
  system("PAUSE");	
  return 0;
}
And the log the compiler gives to me:
Compilador: Default compiler
Building Makefile: "D:\Dev\Projetos\C_C++\RGE\C\dary try 1\Makefile.win"
Executando  make clean
rm -f obj/main.o  bin/dary.exe

gcc.exe -c main.c -o obj/main.o -I"D:/Dev/Dev-Cpp/include"  -I"D:/Dev/_lib/C/SDL-devel-1.2.11-mingw32/SDL-1.2.11/include"  -I"D:/Dev/Dev-Cpp"  -I"D:/Dev/Projetos/C_C++/RGE/C/include/dary"  -I"D:/Dev/Projetos/C_C++/RGE/C/include"   

gcc.exe obj/main.o  -o "bin\dary.exe" -L"D:/Dev/Dev-Cpp/lib" -L"D:/Dev/_lib" -L"D:/Dev/_lib/C/SDL-devel-1.2.11-mingw32/SDL-1.2.11/lib" -L"D:/Dev/Dev-Cpp" -L"D:/Dev/Projetos/C_C++/RGE/C/dary lib try 1/bin" -lmingw32 -dary  

obj/main.o(.text+0x41):main.c: undefined reference to `dary_init'

obj/main.o(.text+0x4c):main.c: undefined reference to `dary_dispose'
collect2: ld returned 1 exit status

make.exe: *** [bin/dary.exe] Error 1

Execução terminada
Please someone help me, i depend on that to continue on my project development! xD
Sneftel
Sneftel
"-dary" isn't a real linker option, at least not one that you're interested in. Perhaps you're looking for "-ldary"?
Linkin_T
Linkin_T
Dumb me, now it compiles, but at the execution it gives me another error, came from Windows. It says it encountered a problem and needs to be closed. Maybe i´m accessing some protected area of memory?

BTW, it happens when i try to execute a method used to initialize an array, the dary_init. Here is the code for it:

/*-------------------------------------------------------dary_init  - Description: Initializes correctly an array to be                 used.   - Returns: TRUE if succeeded, else FALSE  - Arguments: ary - Pointer to a dary struct created               cap - Capacity for the new array               elem_size - size in bytes for each element-------------------------------------------------------*/int dary_init(dary_p ary, d_int cap, d_int elem_size) {  if (cap < DARY_MIN_CAP) {    ary->cap = DARY_MIN_CAP;  } else {    ary->cap = cap;  }  ary->elem_size = elem_size;  ary->size = 0;  d_int len = dary_length(ary);  ary->data = calloc(ary->cap, ary->elem_size);  if (!ary->cap) {    ary->state = DARY_DISPOSED;    return FALSE;  } else {    ary->state = DARY_STARTED;    return TRUE;  }}


If there´s need for any more code, just get the zip file, it contains everything.

http://www.badongo.com/file/3538666
Sneftel
Sneftel
Quote:
Original post by Linkin_T
Dumb me, now it compiles, but at the execution it gives me another error, came from Windows. It says it encountered a problem and needs to be closed. Maybe i´m accessing some protected area of memory?

Maybe. Why don't you use the debugger to find out?
Linkin_T
Linkin_T
Following message were thrown:
"An access violation (segmentation fault) was found in your program."
... I´m clueless... In my view everything is right.
Linkin_T
Linkin_T
Yah it´s strange, hopefully any moderator can fix it.

But apart from this, i hope someone can see my code that is in the zip file and comment for me ^^
Sneftel
Sneftel
Quote:
Original post by Linkin_T
Following message were thrown:
"An access violation (segmentation fault) was found in your program."
... I´m clueless... In my view everything is right.

Right. Which is why you USE THE DEBUGGER.
christian h
christian h
int main(int argc, char *argv[]){  dary* ary;  dary_init(ary, 10, sizeof(int));  dary_dispose(ary);   system("PAUSE");	  return 0;}


You are sending ary the pointer as instance to the functions, so for example the dary_init only initializes a parameter as local variable.
What you should do is to send and receive it as pointer to pointer:
dary *ary = 0;dary_init ( &ary, 10, sizeof(int) );-----void dary_init ( dary **ary, int count, int size ) {// write to ary:*ary = (dary*)malloc ( count*size );}

This would have showed up with a debugger ;)

ch.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.