Add pthread.h to nds project makefile

325 Views Asked by At

I've searched and tried to link the library pthread.h for an ARM project with the nds platfrom in my makefile, and I am still unable to link it properly...

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

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

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

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

include $(DEVKITARM)/base_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
# DATA contains .bin files with extra data for the project (e.g. graphic tiles)
# NITRODATA contains the "virtual" file system accessed through filesystem lib
#-------------------------------------------------------------------------------
TARGET      :=  $(shell basename $(CURDIR))
BUILD       :=  build
SOURCES     :=  source
INCLUDES    :=  include
DATA        :=  data
NITRODATA   :=  nitrofiles

#-------------------------------------------------------------------------------
# options for code generation
#-------------------------------------------------------------------------------
ARCH    :=  -march=armv5te -mlittle-endian

CFLAGS  :=  -Wall -g -O2 \
            $(ARCH) -mtune=arm946e-s -fomit-frame-pointer -ffast-math
                # -Wall                     : enable all warnings
                # -g                        : enable debug info generation
                # -O2                       : code optimization level 2
                # $(ARCH) -mtune=arm946e-s  : tune code generation for specific machine
                # -fomit-frame-pointer      : avoid to use a 'frame-pointer' register in functions that do not need it
                # -ffast-math               : optimize math operations

CFLAGS  +=  $(INCLUDE) -DARM9

ASFLAGS :=  -g $(ARCH)
LDFLAGS =   -specs=ds_arm9.specs $(ARCH)

#-------------------------------------------------------------------------------
# any extra libraries we wish to link with the project (order is important)
#-------------------------------------------------------------------------------
LIBS    :=  -lfilesystem -lfat -lnds9

#-------------------------------------------------------------------------------
# list of directories containing libNDS libraries, this must be the top level
# containing include and lib
#-------------------------------------------------------------------------------
LIBNDS  :=  $(DEVKITPRO)/libnds

#---------------------------------------------------------------------------------
# check if the build directory is not created yet
#---------------------------------------------------------------------------------
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)))
AFILES      :=  $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
BINFILES    :=  $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.bin)))

export OFILES   :=  $(BINFILES:.bin=.o) $(CFILES:.c=.o)
export SFILES   :=  $(AFILES:.s=.o)

# GARLIC_API is the directory where the API's include and source code lives
#           (assume a relative structure of the GARLIC project directories)
export GARLICAPI    :=  $(CURDIR)/../GARLIC_API

export INCLUDE  :=  $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
                    $(foreach dir,$(LIBNDS),-I$(dir)/include) \
                    -I$(GARLICAPI)

export LIBPATHS :=  $(foreach dir,$(LIBNDS),-L$(dir)/lib)


#---------------------------------------------------------------------------------
# use CC for linking standard C projects 
#---------------------------------------------------------------------------------
export LD   :=  $(CC)


export _ADDFILES    :=  -d $(CURDIR)/$(NITRODATA)


.PHONY: $(BUILD) clean

#---------------------------------------------------------------------------------
$(BUILD):
    @[ -d $@ ] || mkdir -p $@
    @make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile

#---------------------------------------------------------------------------------
clean:
    @echo "Removing ALL intermediate files... "
    @echo "Por favor, recuerda que habitualmente NO es necesario hacer un 'clean' antes de un 'make'"
    @sleep 3
    @rm -fr $(BUILD) $(TARGET).elf $(TARGET).nds

#---------------------------------------------------------------------------------
run : $(TARGET).nds
    @echo "runing $(TARGET).nds with DesmuME"
    @$(DESMUME)/DeSmuME.exe $(TARGET).nds &

#---------------------------------------------------------------------------------
debug : $(TARGET).nds $(TARGET).elf
    @echo "testing $(TARGET).nds/.elf with DeSmuME_dev/Insight (gdb) through TCP port=1000"
    @$(DESMUME)/DeSmuME_dev.exe --arm9gdb=1000 $(TARGET).nds &
    @$(DEVKITPRO)/insight/bin/arm-eabi-insight $(TARGET).elf &

#---------------------------------------------------------------------------------
else

DEPENDS :=  $(OFILES:.o=.d) $(SFILES:.o=.d)

#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT).nds   :   $(OUTPUT).elf
$(OUTPUT).elf   :   $(OFILES) $(SFILES)

#---------------------------------------------------------------------------------
%.nds: %.elf
    @ndstool -c $@ -9 $< -b $(GAME_ICON) "$(GAME_TITLE);$(GAME_SUBTITLE1);$(GAME_SUBTITLE2)" $(_ADDFILES)
    @echo built ... $(notdir $@)

#---------------------------------------------------------------------------------
%.elf:
    @echo linking $(notdir $@)
    $(LD)  $(LDFLAGS) $(OFILES) $(LIBPATHS) $(LIBS) $(SFILES) -o $@

#---------------------------------------------------------------------------------
%.bin.o :   %.bin
#---------------------------------------------------------------------------------
    @echo $(notdir $<)
    $(bin2o)

-include $(DEPSDIR)/*.d


#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------

As I am not working in a Windows environment, the current thread libraries I found, are the following ones: enter image description here

To conclude, my doubt is... I don't really know how to link the C library of threads in this project, in my makefile... Even I tried to declare the library in the code... like this:

#include <pthread.h>

However type pthread_t was not recognised.

Has this something to do with the linkage in the makefile? In that case what shall I add to it?

1

There are 1 best solutions below

0
On BEST ANSWER

windows DLL will be of no use on an embedded system like the Nintendo DS. You should only use libraries that have been rebuilt for your target platform, and not libraries for the host system.

Additionally, something like pthread relies on an operating system to provide the threading services (typically, a POSIX-compatible one), and the Nintendo DS has no such services: you are running on bare metal, and you only have the services you bring in. That does not mean it is impossible to do multi-threading on a DS, but that means you'll have to do it all by yourself, using the timer interrupt to trigger code that captures stack and program pointers, stores them somewhere and restore those of a previous thread. This is called software multithreading.

Hopefully, since the NDS has dedicated hardware for video and audio multiplexing/compositing as well as a slave processor for slow tasks such as serial devices communication and networking, your ARM9 CPU is typically free for running your game logic uninterrupted and has no real needs for multi-threading.