LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   configure with static libs (https://www.linuxquestions.org/questions/linux-newbie-8/configure-with-static-libs-903539/)

gosala 09-17-2011 09:50 AM

configure with static libs
 
Hi,

I am developing an embedded device using arm platform and there I want to install a mad player in the target. for that I want to copile it in my host with static libraries. I already tried using
--enable-static --disable-shared
options in the configuration command. Although the command generates static libs, the executable program still requires shared libs when I try command "arm-linux-readelf -d /location/madplay "

what should I do in order to build the program completely static ?

thank you.

tronayne 09-17-2011 11:03 AM

You make a static library with the ar utility; you have a bunch of object files (.o) and you
Code:

#!/bin/sh
for file in *.o
do
    ar cq libname.a ${file}
done
ranlib libname.a

If you want to see what's getting added add v to cqv. If you've already created the library and are adding or changing functions, use the "replace" directive instead of cq; i.e., use rv.

You can also do this in a Makefile (make "knows" how to execute ar). This is a a small sample of a huge Makefile for building a library of Numerical Recipes functions. It's edited for size and thus isn't actually usable but if you create a Makefile of this form, you'll build static libraries of your object code (and, a bonus, if you edit any of he contained functions and then execute make only the changed object file will be replaced in the library archive).
Code:

#      Define this as the parent directory where your include, lib and bin directories live
#      for example, /home/userid (${HOME}), /usr, /usr/local and so on
INSTALL_BASE =  ${HOME}

BASDIR  =      ${INSTALL_BASE}

MAKEFILE =      Makefile

#      use whichever one of these is for your system (don't add any additional flags, not needed)
#CFLAGS =      -xarch=native -xO3 -DANSI -I$(BASDIR)/include  # Solaris 32-bit
#CFLAGS =      -xarch=native64 -xO3 -DANSI -I$(BASDIR)/include # Solaris 64-bit
#CFLAGS =      -O2 -DANSI -I$(BASDIR)/include                  # Linux 32-bit
CFLAGS  =      -O2 -FPIC -m64 -DANSI -I$(BASDIR)/include      # Linux 64-bit

OWNER  =      ${LOGNAME}

GROUP  =      ${GRPNAME}

LIBDIR  =      $(BASDIR)/lib

LMASK  =      0644

LIBRARY =      libnr.a

#      Do not fiddle with anything below here unless you really, REALLY know what you're doing

#      Do not remove target when quit or delete is hit:
.PRECIOUS:      $(LIBRARY)

OBJECTS =      addint.o airy.o amebsa.o amoeba.o amotry.o amotsa.o anneal.o \
        anorm2.o arcmak.o arcode.o arcsum.o asolve.o atimes.o avevar.o \
        zrhqr.o zriddr.o zroots.o

SOURCES =      addint.c airy.c amebsa.c amoeba.c amotry.c amotsa.c anneal.c \
        anorm2.c arcmak.c arcode.c arcsum.c asolve.c atimes.c avevar.c \
        hundreds missing here
        zrhqr.c zriddr.c zroots.c

ALL:            $(LIBRARY)

# This is what adds object file to your archive file
$(LIBRARY):    $(LIBRARY)(airy.o) $(LIBRARY)(amoeba.o) $(LIBRARY)(amotry.o) \
        $(LIBRARY)(dlinmin.o) $(LIBRARY)(dbrent.o) $(LIBRARY)(df1dim.o) \
        hundreds missing here
        $(LIBRARY)(f1dim.o) $(LIBRARY)(mnbrak.o) $(LIBRARY)(nrutil.o)

# These are the dependencies for every source code file
$(LIBRARY)(addint.o): $(BASDIR)/include/nr.h

$(LIBRARY)(airy.o): $(BASDIR)/include/nr.h

$(LIBRARY)(amebsa.o): $(BASDIR)/include/nr.h \
        $(BASDIR)/include/nrutil.h

$(LIBRARY)(amoeba.o): $(BASDIR)/include/nr.h \
        $(BASDIR)/include/nrutil.h

$(LIBRARY)(amotry.o): $(BASDIR)/include/nr.h \
        $(BASDIR)/include/nrutil.h

$(LIBRARY)(amotsa.o): $(BASDIR)/include/nr.h \
        $(BASDIR)/include/nrutil.h

$(LIBRARY)(anneal.o): $(BASDIR)/include/nr.h
hundreds missing here

install:        $(LIBRARY)
        cp $(LIBRARY) $(LIBDIR)
        cd $(LIBDIR);\
        chown $(OWNER) $(LIBRARY);\
        chgrp $(GROUP) $(LIBRARY);\
        chmod $(LMASK) $(LIBRARY)

remove:
        cd $(LIBDIR);\
        rm -f $(LIBRARY)

clean:
        rm -f $(OBJECTS)

clobber:
        rm -f $(OBJECTS) $(LIBRARY)

You simply type make, wait awhile, make install which will install libname.a in your chosen lib directory (defined a the top of Makefile).

Hope this helps some.

gosala 09-17-2011 01:00 PM

Hi,

thanks for reply.I still don't understand some parts in your post. I will try and let you know the result.

thank you.

tronayne 09-17-2011 02:01 PM

Well, maybe it wasn't all that clear -- so let me back up a little.

A static library is created by the ar (archive) utility and contains compiled object files (.o files). It will be named libsomething.a so that you can link a program with a -lsomething. The math library, for example, is named libm.a and you link it with -lm. If you look in /usr/lib (or /usr/lib64 there are quite a few static libraries named libsomethng.a; that's what they are, they were created with the ar utility, and you link them to a program with -lsomething.

How you create a static library is laid out in the Makefile example above. As I mentioned, make knows how to create a static library using a compiler and the ar utility if the Makefile is put together as in the example. You have source files (say, .c), you want to have object files (.o) and you want those object files put into a searchable archive file (aka "library") that can be linked with -lsomething.

The Makefile syntax that does this is
Code:

$(LIBRARY):    $(LIBRARY)(function_name.o) \
                $(LIBRARY)(function_other.o)

The make utility has built-into it what that syntax means and what to do with it.

The other lines that are important are the dependency lines:
Code:

$(LIBRARY)(function_name.o): $(BASDIR)/include/header_file.h
The tokens that are used, LIBRARY, BASDIR, and others are replaced with their definitions (at the top of the Makefile) for human convenience -- much easier to type LIBRARY that to type libsomethig.a over and over again. The file also takes advantage of make built-in token definitions such as CFLAGS (which we have added to for our particular use).

Tokens enclosed in braces ( {} ) are system environment variables, those enclosed in parens ( () ) are locally defined in the Makefile.

It's worth your time and effort to get to know make if you're going to be doing development work for any length of time -- makes your life much, much easier.

Hope this helps some.


All times are GMT -5. The time now is 04:43 AM.