1

I have following code:

.Lib file:

contain TRNG_GetRandomData( int *Data, int Size ); Method

.H file (header file):

extern void TRNG_GetRandomData( int *Data, int Size );

.C code (Source file):

#include<OpenDrivers.h>
void main()
{
    int  test[8] ; 
    TRNG_GetRandomData(test,8);
}

and i getting error

undefined reference to TRNG_GetRandomData

Can any one help me solve it?

storaged
  • 1,837
  • 20
  • 34
  • 1
    could you edit with your gcc compile command. Also you compile with gcc under windows (MinGW ?) ? – PilouPili Sep 15 '18 at 09:51
  • I am using GCC fileName.c command at a time and using windows 7 (32 bit) – user2865635 Sep 15 '18 at 09:55
  • 1
    you need to tell compiler to link with the library something like gcc fileName.c -L -l – PilouPili Sep 15 '18 at 09:57
  • Exactly , i am not getting how to link .lib file to header file or source code . could you please suggest how i Will do it ? – user2865635 Sep 15 '18 at 09:58
  • After Using "gcc MyProg.c myLib.lib" Command i found following error C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-min gw32/bin/ld.exe: unknown architecture of input file myLib.lib(trng.o)' is incompatible with i386:x86-64 output C:\Users\admin\AppData\Local\Temp\cc6VqOqa.o:MyProg.c:(.text+0x1a): undefined re ference to `TRNG_GetRandomData' collect2.exe: error: ld returned 1 exit status – user2865635 Sep 15 '18 at 10:08
  • "unknown architecture of input file myLib.lib(trng.o)' is incompatible with i386:x86-64" - is this a Windows build of the library? Can you rebuild it yourself? Or it might be a 32-bit library whereas you're building as 64-bit: can you try compiling with -m32 ? – Rup Sep 17 '18 at 09:12
  • Thank you Rup for your reply , now my first question will be is it possible to call a method from .lib file because when i looking it on internet there are a lot of examples there but with the .O file or .SO file , i did not get any example with .lib file .. – user2865635 Sep 17 '18 at 11:59

2 Answers2

2

You need to add option -l infront of your lib file (without the .lib extension) in your Makefile. I am guessing there is some linking issues with header files, are you sure the header files are properly included?

Btw, you can also refer to this question : What are Header Files and Library Files? Hopefully this helps.

TreesaL
  • 79
  • 6
0

I solved similar problem by extracting all object files from *.lib file and adding these files to linker.

ar x lib3rd_party.lib
gcc <flags> -o my_target my_obj1.o my_obj2.o ... 3rd_party_obj1.o 3rd_party_obj2.o ...

It worked. Then I made some "improvement". I "repacked" these object files to static *.a library.

ar r libmylib.a 3rd_party_obj1.o 3rd_party_obj2.o ...

And linked my project with it:

gcc <flags> -static -L<libmylib.a_location> -o my_target my_obj1.o my_obj2.o ... -lmylib

It worked too. Note -lmylib must go after object files.

LennyB
  • 359
  • 1
  • 5
  • 14