gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > Creating a library

#23802 - Boltnews - Wed Jul 21, 2004 11:31 pm

I have looked through the forums, but I just can't figure out how to make a good library with DevKitAdv. I can make a library but I can't resolve the functions in the library.

Can someone tell me the command to AR a library?

Thanks

#23803 - poslundc - Wed Jul 21, 2004 11:41 pm

IIRC:

ar -q archive_name.a bin1.o bin2.o ... binN.o

... will create the archive file for you nice and easy. Then just include it as a parameter when calling gcc to link it to your project (I think you may have to list it before the binaries that use it; it's been a long time since I've done this, though.).

Dan.

#23831 - Boltnews - Thu Jul 22, 2004 4:54 pm

Thanks Dan,

I tried this but it still is having trouble resolving the functions. I'm going to post my sample code here to see if anyone can see what I'm doing wrong.


Code from testlib.h:
Code:

#ifndef _TESTLIB_H_
#define _TESTLIB_H_

#if defined(__cplusplus)
extern "C" {
#endif

int ReturnNum(int* pout);

#if defined(__cplusplus)
}
#endif

#endif /* _TESTLIB_H_ */



Code from testlib.c:
Code:

#include "testlib.h"

int ReturnNum(int* pout)
{
   if (*pout > 10)
      *pout = 15;
   else
      *pout = 5;

   return 10;
}


This is the compile output from the lib:
Quote:

/C/DevKitAdv/devkitadv-r5-beta-3/bin/gcc -c testlib.c -o obj/testlib.o
/C/DevKitAdv/devkitadv-r5-beta-3/bin/ar -q libtestlib.a obj/testlib.o


Code from testapp.c:
Code:

#include "testlib.h"

int main()
{
   int testnum = 11;
   int rv = 0;
   
   rv = ReturnNum(&testnum);
   
   return testnum;
}


Compile output from app:
Quote:

/C/DevKitAdv/devkitadv-r5-beta-3/bin/gcc -c ./testapp.c -I./ -I../testlib -o obj/testapp.o
/C/DevKitAdv/devkitadv-r5-beta-3/bin/gcc -o testapp.elf -L/C/DevKitAdv/devkitadv-r5-beta-3/arm-agb-elf/lib/libm.a -L../libtestlib.a ./obj/testapp.o
./obj/testapp.o: In function `main':
./obj/testapp.o(.text+0x28): undefined reference to `ReturnNum'
collect2: ld returned 1 exit status
make_old: *** [testapp.elf] Error 1


Can anyone see what I'm doing wrong?

#23836 - poslundc - Thu Jul 22, 2004 6:15 pm

Don't use the -L switch, just put the .a file on the command line by itself and it should work.

Dan.

#23839 - Boltnews - Thu Jul 22, 2004 8:17 pm

Oh well. I feel dumb. I figured it out. I had a link order problem. :)

Quote:

/C/DevKitAdv/devkitadv-r5-beta-3/bin/gcc -o testapp.elf -L/C/DevKitAdv/devkitadv-r5-beta-3/arm-agb-elf/lib/libm.a -L../libtestlib.a ./obj/testapp.o


Should be:
Quote:

/C/DevKitAdv/devkitadv-r5-beta-3/bin/gcc -o testapp.elf ./obj/testapp.o -L/C/DevKitAdv/devkitadv-r5-beta-3/arm-agb-elf/lib/libm.a -L../libtestlib.a

#23845 - sajiimori - Fri Jul 23, 2004 12:02 am

I'm suprised it works either way because -L is for adding library search dirs. Like Dan said, just put the .a file on the command line.

#23926 - Cearn - Sat Jul 24, 2004 1:30 pm

As said, `-L' adds the search paths. `-l' (lower case) adds a library. In particular, something like `-lfoo' will add `libfoo.a'. If you haven't already, it might pay to get some of the GNU manuals.