Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

creating a static library under linux

Posted on 2007-10-10
15
Medium Priority
?
218 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:DJ_AM_Juicebox
  • 5
  • 4
  • 4
  • +1
15 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 1600 total points
ID: 20049695
You would first compile your .o files, and then use ar to combine them into a .a file (static library) :

        g++ -c obj1.cpp -o obj1.o
        g++ -c obj2.cpp -o obj2.o
        ar cq libtest.a obj1.o obj2.o

Then you can create an application that links to that library using :

        g++ -o testapp testapp.cpp -ltest
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20049710
>>> 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

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20049719
>>> and then use ar to combine them into a .a file
Sorry, there is someone who knows it better ...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Infinity08
ID: 20049756
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)
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 20049880
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
0
 
LVL 86

Expert Comment

by:jkr
ID: 20049948
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
0
 
LVL 86

Expert Comment

by:jkr
ID: 20049990
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.
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 20050404
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
0
 
LVL 86

Expert Comment

by:jkr
ID: 20050423
The '-l' links to your library when you want to use it with other code.
0
 
LVL 86

Expert Comment

by:jkr
ID: 20050442
BTW, see also http://www.tldp.org/HOWTO/Program-Library-HOWTO/index.html ("Program Library HOWTO")
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 20050624
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
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 400 total points
ID: 20050643
You need to use it as above:

g++ -o myapp myapp.cpp -lmylib.a

No space - and your lib has to be named 'libmylib.a' to satisfy the naming convention. Be sure to chekc out the TLDP link above.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20050715
>> 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)
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 20051003
ah ok I see - that's an interesting way to handle the lib files (the -l parameter)

Thanks
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20051029
In the Unix world, people like shortcuts ... every key press they can save is a bonus ;)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question