Link to home
Start Free TrialLog in
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

asked on

creating a static library under linux

Hi,

I am compiling simple applications using g++. When I compile a cpp file, it outputs an executable. How do you output instead a static library? I want to do something like:

     // static library h file
     int MyLibReturnNumber();
     // static library cpp file
     int MyLibReturnNumber()
     {
         return 5;
     }
     //

then in my executable, something like:
     #include "MyLib.h"
     // umm somehow link MyLib.lib
     int main()
     {
         int n = MyLibReturnNumber();
         return 0;
     }

So I guess the first thing is, how do I compile a h/cpp into a static library?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>> how do I compile a h/cpp into a static library?
As far as I know you simply use .a  file extension for the output file when linking.

Regards, Alex

>>> and then use ar to combine them into a .a file
Sorry, there is someone who knows it better ...
oh, and depending on the system you are on, you may need to run ranlib too to generate the index for the library :

        ranlib libtest.a

(I've gotten used to Solaris on which you don't need to do that heh, so I forgot to mention)
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

ASKER

Hi infinity,

Everything looks good up till the -ltest - what is that for? Don't I have to pass it the .a file I want to link to somehow (which is named libtest.a)

Thanks
Or as a makefile:


# Makefile for the library module
#

CFLAGS  = -v -g

.S.s:
    $(CPP) -traditional $< -o $*.s
.c.s:
    $(CC) $(CFLAGS) -S $<
.s.o:
    $(AS) -c -o $*.o $<
.c.o:
    $(CC) $(CFLAGS) -c $<

SUBDIRS =

CC    = g++

SRCS  = sample.cpp getopt.c
OBJS  = sample.o getopt.o
TGT   = sample
LIB   = libSample.a
ARCH  = libSample.a

objs:   $(SRCS)
    $(CC) $(CFLAGS) -c $(SRCS)

lib:    $(SRCS) $(OBJS)
    ar -cr $(LIB) $(OBJS)

expand:
    $(CC) $(CFLAGS) -E $(SRCS) > preprocess.out

clean:
    rm -f $(OBJS)
    rm -f $(ARCH)

man:  $(OBJS)
    c2man $(CFLAGS) $(SRCS)

dep:
    $(CPP) -Iinclude -M *.c > .depend

dummy:

#
# include a dependency file if one exist
#
ifeq (.depend,$(wildcard .depend))
include .depend
endif
If you remove the 'getopt' entries friom the above (sorry, I should have done that), this should suit your task. That's a boilerplate Makefile I used for static libs on various UN*X systems, including Linux.
jkr, I just wanted to try undestanding the basic way on the command line first - this seemed to work:

1) my library is mae up of 3 h/cpp pairs:

      class1.h, class1.cpp
      class2.h, class2.cpp
      class3.h, class3.cpp

2) I compile each one into an .o file.
3) I combine all the .o files into one library .a file like:

    >ar cq mylib.a class1.o class2.o class3.o

4) Now I can use the functions inside the lib file in my application, compile and link it like:

    > g++ -o myapp myapp.cpp mylib.a

It compiled ok and I was able to use the functions inside my class1/2/3 files ok. Is this alright? What was that -l option for Infinity?

Thanks
The '-l' links to your library when you want to use it with other code.
BTW, see also http://www.tldp.org/HOWTO/Program-Library-HOWTO/index.html ("Program Library HOWTO")
jkr, I must be using the -l parameter incorrectly, I thought you'd use it like:

        > g++ -o myapp myapp.cpp -l mylib.a

meaning g++ please link to my library 'mylib.a', but instead I get an error:

    cannot find -lmylib.a

if I just ommit the -l it seems to link ok, as if sticking .a files to the end of the compile string implies g++ please link to that (and it works ok - or at least seems to from my novice perspective

Thanks
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> Everything looks good up till the -ltest - what is that for? Don't I have to pass it the .a file I want to link to somehow (which is named libtest.a)

-ltest includes the libtest.a library. the lib at the beginning and the .a at the end are automatically added ... Isn't that easy ? lol


>>         > ar cq mylib.a class1.o class2.o class3.o
>>         > g++ -o myapp myapp.cpp -l mylib.a

That has to be :

        ar cq libmylib.a class1.o class2.o class3.o
        g++ -o myapp myapp.cpp -lmylib

(see my first post)
ah ok I see - that's an interesting way to handle the lib files (the -l parameter)

Thanks
In the Unix world, people like shortcuts ... every key press they can save is a bonus ;)