Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
21 years ago

Building libraries with Eclipse

Has anybody tried building a simple library for use with a NIOS IDE project? -

I have a bunch of application code that I'd prefer to use as a library rather than have it included in every application project which uses it, but I haven't worked out how best to build a makefile to compile/build the library using the gnu tools.

- Roddy

3 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Others have. Scan previous topics. People discuss things they have run into, tips and tricks.

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    <div class='quotetop'>QUOTE </div>

    --- Quote Start ---

    Others have. Scan previous topics. People discuss things they have run into, tips and tricks.[/b]

    --- Quote End ---

    Yes, this is a newsgroup. http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/wink.gif

    But, I haven&#39;t found anything useful about how to build a makefile to link a library for NIOS. One useful comment about the required bizarre name prefix for the mangled make manager, and that&#39;s it.

    My options seem to be:

    a: a library built outside the NIOS IDE.

    b: An &#39;advanced c/c++ project&#39; built within the IDE. This seems to be deprecated.

    c: Some kind of additional "HAL" module, which just pushes my code into the system library.

    &#39;a&#39; seems the only sensible one, because at least it stops the IDE make system from getting in the way of everything.

    If I succeed with what I&#39;m doing, I&#39;ll post an example makefile...

    - Roddy
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Here is a makefile I use with a modified Rules.mak

    Apparently the forum post code is ripping the tabs out of the posted text even though it is in a code tagset so these will need modifications to properly indent the rule commands.

    Also I don&#39;t use the IDE much anymore and use the NIOS shell instead, so I haven&#39;t checked these in the IDE recently, but it should get you most of the way there.

    note the LDLIBS change which links in the static lib d.a and the rule for d.a

    Makefile:

    include Rules.mak
    #  macros and variables
    LDLIBS += d.a
    EXEC = d.a mysoftware.flt
    ifdef DEBUG
    GDB := $(patsubst %.flt, %.gdb, $(EXEC))
    EXEC += $(GDB)
    endif
    #  targets
    all: $(EXEC) 
    d.a: d.o a.o b.o
        $(AR) cru $@ $^
        $(RANLIB) $@
    clean:
        -rm -f $(EXEC) $(OBJ) *.elf *.gdb *.o
    .PHONY: clean all

    Rules.mak

    ###########################################################################  Variables and Rules for building Nios II Linux applications#  #  This file is a "template" that is copied over from the Nios II Linux#  Application plug-in.  This file should be included within your top-level#  Makefile.# 
    ifeq "$(origin TOPDIR)" "undefined"
    TOPDIR := $(shell pwd)
    endif
    # #  The Settings.mak file is automatically generated by Eclipse.  It#  contains values for all external variables used by this Makefile#  fragment.# #  The "dash" in front of the include statement indicates that the Make#  process will not terminate if it cannot find the Settings.mak file.# 
    include $(TOPDIR)/Settings.mak
    # #  Warn users if external variables have not yet been defined.# 
    ifeq "$(origin UTILSDIR)" "undefined"
    $(warning The location of the utilties directory is undefined)
    endif
    ifeq "$(origin LIBCDIR)" "undefined"
    $(warning The location of uClibc is undefined)
    endif
    ifeq "$(origin LINUX_NE_ROOT)" "undefined"
    $(warning The location of libraries is undefined)
    endif
    # #  Path + Name of necessary executables# 
    EXECSUFFIX := .exe
    CROSS       := nios2-elf-
    CC          := $(CROSS)gcc
    AR          := $(CROSS)ar
    LD          := $(CROSS)ld
    NM          := $(CROSS)nm
    RANLIB      := $(CROSS)ranlib
    STRIPTOOL   := $(CROSS)strip
    ELF2FLT     := $(UTILSDIR)/bin/elf2flt
    # #  Location of important files# 
    CRT0  := $(LIBCDIR)/lib/crt0.o
    LIBC  := $(LIBCDIR)/lib/libc.a
    LIBGCC  := $(shell $(CC) -print-file-name=libgcc.a)
    LIBM     := $(shell $(CC) -print-file-name=libm.a)
    LIBGCCDIR   := $(dir $(LIBGCC))
    LINKSCRIPT  := $(UTILSDIR)/scripts/elf2flt.ld
    # #  Various flags, modifications are not recommended and not necessarily#   supported.# #  To debug your app, you should use "-O0 -g" in your CFLAGS variable.#  #   DEPFLAGS:#    -MM : ignore system includes, #     -M : include system includes in the dependency# 
    WARNING_FLAGS := -Wall
    INCS = -I$(LIBCDIR)/include -I$(LIBGCCDIR)/include -I$(ULIBDIR)/include
    DEFS := -D__linux__
    ifdef DEBUG
    FLAGS := -O0 -g -nostdinc -c
    else
    FLAGS := -O2 -g -nostdinc -c
    endif
    CFLAGS = $(FLAGS) $(INCS) $(DEFS)
    #  Auto-dependency flags: -MM : ignore system includes, -M : include system includes in the dependency
    DEPFLAGS = -E -MM
    LDFLAGS   := -msys-crt0=$(CRT0) -r -d -L$(LIBCDIR)/lib -L$(ULIBDIR)
    LDLIBS    := $(LIBM) $(LIBGCC) $(LIBC)
    #  default stack size is 4K
    FLTFLAGS:=
    %.exe : %.flt
        cp -f $< $@
        # #  Make a relocatable flat file from an ELF file.#  STACKSIZE is defined by individual makefiles.# 
    %.flt : %.elf
        $(ELF2FLT) $(FLTFLAGS) -o $@ $<
    # #  Make a bound-address ELF file required by GDB for debugging the application.# 
    %.gdb : %.bin
        $(LD) -T $(LINKSCRIPT) -o $@ $<
    # #  Make a relocatable ELF file.# 
    %.elf : %.bin
        $(LD) -T $(LINKSCRIPT) -Ur -o $@ $<
        # #  Standard stuff: The object file is dependent on the C source and Makefile timestamps# 
    %.o : %.c Makefile
        $(CC) $(CFLAGS) $(WARNING_FLAGS) -o $@ $<
    %.o : %.cpp Makefile
        $(CC) $(CFLAGS) $(WARNING_FLAGS) -o $@ $<
    %.bin : %.o
        $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
        # #  Automatically generate dependencies for the .c source files.#  Be sure to omitt debug-level flags as this can produce macro def&#39;n output.#  The sed "script" makes the .d (in addition to the .c) file a#  target of the dependencies so that the .d file is rebuild if required.#  e.g. "X.o: <dependency-list>" gets transformed into "X.o X.d: <dependency-list>"# 
    %.d: %.c
        $(SHELL) -ec &#39;$(CC) $(DEPFLAGS) $(CFLAGS) -c $< | sed &#39;\&#39;&#39;s/\($*\)\.o*/.o $@ : /g&#39;\&#39;&#39; >$@;  || rm -f $@&#39;