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.

DS development > Making a library instead of an image

#62444 - Vich - Wed Nov 30, 2005 11:33 pm

How do you create a static library instead of a .nds file? My guess is that you have to change your makefile and/or ds_rules and/or base_rules file, but I have no clue how.
I've tried many parameters, but I'm partially guessing them from pages like:
- http://forum.gbadev.org/viewtopic.php?t=1513&highlight=library+makefile
- http://lists.gnu.org/archive/html/help-gplusplus/2004-10/msg00015.html

This is the makefile that I use.

What I basically want is:
- build library libA
- build library libB, using libA
- build a program that uses libB

I've googled and searched the gcc webpage, but can't find a clear answer. Anyone?

#62446 - GPFerror - Wed Nov 30, 2005 11:45 pm

here is the makefile that I use for my DS SDL library
look at the main targets section for how the lib is built

Code:
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------

ifeq ($(strip $(DEVKITARM)),)
$(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM)
endif

include $(DEVKITARM)/ds_rules

#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# INCLUDES is a list of directories containing extra header files
#---------------------------------------------------------------------------------
TARGET      :=   libSDL
BUILD      :=   build
SOURCES       :=   source source/video source/video/nds source/timer  source/timer/nds source/thread source/thread/nds source/main source/main/nds source/joystick source/joystick/nds source/file source/events source/endian source/cdrom source/cdrom/nds source/audio source/audio/nds

INCLUDES   :=   include source source/video source/video/nds source/timer  source/timer/nds source/thread source/thread/nds source/main source/main/nds source/joystick source/joystick/nds source/file source/events source/endian source/cdrom source/cdrom/nds source/audio source/audio/nds

NDSLIB_INCLUDE=$(DEVKITPRO)/libnds/include
NDSLIB_LIB=$(DEVKITPRO)/libnds/lib
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH   := -mthumb-interwork

# note: arm9tdmi isn't the correct CPU arch, but anything newer and LD
# *insists* it has a FPU or VFP, and it won't take no for an answer!
CFLAGS   :=   -g -Wall -O2\
          -mcpu=arm9tdmi -mtune=arm9tdmi -fomit-frame-pointer\
         -ffast-math \
         $(ARCH)

CFLAGS   +=   $(INCLUDE) -DARM9 -I$(NDSLIB_INCLUDE) -D__NDS__ -DENABLE_NDS -DDISABLE_THREADS -DPACKAGE=\"SDL\" -DVERSION=\"1.2.3\" -DHAVE_ALLOCA_H=1 -DHAVE_ALLOCA=1

ASFLAGS   :=   -g $(ARCH)
LDFLAGS   =   -specs=ds_arm9.specs -g $(ARCH) -mno-fpu -Wl,-Map,$(notdir $*.map) -L$(NDSLIB_LIB)

#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project
#---------------------------------------------------------------------------------
LIBS   := -lnds9
 
 
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS   :=   $(LIBNDS)
 
#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------
 
export OUTPUT   :=   $(CURDIR)/$(TARGET)
 
export VPATH   :=   $(foreach dir,$(SOURCES),$(CURDIR)/$(dir))
export DEPSDIR   :=   $(CURDIR)/$(BUILD)

CFILES      :=   $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES   :=   $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES      :=   $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
BINFILES   :=   $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.bin)))
 
#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
#---------------------------------------------------------------------------------
   export LD   :=   $(CC)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
   export LD   :=   $(CXX)
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------

export OFILES   :=   $(BINFILES:.bin=.o) \
               $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
 
export INCLUDE   :=   $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
               $(foreach dir,$(LIBDIRS),-I$(dir)/include) \
               $(foreach dir,$(LIBDIRS),-I$(dir)/include) \
               -I$(CURDIR)/$(BUILD)
 
export LIBPATHS   :=   $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
 
.PHONY: $(BUILD) clean
 
#---------------------------------------------------------------------------------
$(BUILD):
   @[ -d $@ ] || mkdir -p $@
   @make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
 
#---------------------------------------------------------------------------------
clean:
   @echo clean ...
   @rm -fr $(BUILD) *.elf *.nds* *.bin
 
 
#---------------------------------------------------------------------------------
else
 
DEPENDS   :=   $(OFILES:.o=.d)
 
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
-include $(DEVKITARM)/base_rules
$(NDSLIB_LIB)/libSDL.a:      $(OFILES)
 
#---------------------------------------------------------------------------------
%.o   :   %.bin
#---------------------------------------------------------------------------------
   @echo $(notdir $<)
   @$(bin2o)
 
 
-include $(DEPENDS)
 
#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------

#62448 - Vich - Wed Nov 30, 2005 11:56 pm

Thanks! That was easy!

I just added:
Code:

-include $(DEVKITARM)/base_rules
$(OUTPUT).a      :   $(OFILES)

:D

#62934 - Vich - Wed Dec 07, 2005 12:37 am

I've got another problem:

I built library called COW.
The library that is called AlterNova uses COW and gets built too.

My program uses library AlterNova and thus also COW. So I link this program with both libraries. When I do that, I get many errors like:
Quote:
AlterNova/CTextureManager.h:145: undefined reference to `cow::CLog::~CLog()'


They're all undefined references to objects from AlterNova(and functions also) to COW.
Any suggestions why this is?

#62938 - GPFerror - Wed Dec 07, 2005 1:09 am

try adding the lib a second time to the link line

ie

-lA -lB -lA

or visa versa depending on the dependancies.

#62939 - Vich - Wed Dec 07, 2005 1:12 am

GPFerror wrote:
try adding the lib a second time to the link line

ie

-lA -lB -lA

or visa versa depending on the dependancies.


Oh my f... God :o
That solved it! It's official now: I hate GCC :P
Thank you v?ry much!

[edit]
-lB -lA does the trick too :o
(-lAlterNova -lCOW)