How to create a static library with GCC on linux

nietod
nietod used Ask the Experts™
on
I have a large group of .cpp files I want to compile and link to gether into a single static library, which then can be linked into other projects, but I can't find the GCC command-line options to do this.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
On UNIX/Linux, a static library is just a special case of the archive files created by the ar command.  Unless they've changed things in the latest gcc, you can't create them directly with the gcc command.  Instead, you rig your gcc commands to create the .o files, and then issue an ar command to build the library.

e.g.
     rm libmyLib.a
     ar -r libmyLib.a file1.o file2.o ....

There are various approaches to putting this into a makefile.  You can either rebuild the entire library from scratch as shown above (simplest but slowest), you can replace each module after it's been built, or you can do it on a directory by directory basis (in one common case where you build a single .a file from multiple directories of source files).  One gotcha is to make sure you don't leave any old modules in the library (i.e. if you change the name of a file, the old one will still be there and will cause problems).

See the man page for ar for more info.

Gary

Commented:
I think you should use 'ar' utility for this:

You need to compile all files with 'gcc' with flag '-c' - to create obj files.

TARGET = mylib
OBJS = mycpp1.obj mycpp2.obj
$(TARGET): $(OBJS)
      ar ru $@ $?

Later you can link this mylib with your main application with the help of 'ld'.

Commented:
It seems like it takes me too much time to write an answer... Sorry, GaryFx, I didn't see yours...
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Author

Commented:
Thanks.  I programmed in UNIX abotut 15 years ago and thought it was something like that but couldn't remember the command name.   Also I thought maybe things has changed a bit.  I have noticed a few things have changed between SUN OS 5 and RedHat Linux 9! I gues this is one thing that didn't change.

I'm going to leave this question open until Tuesday, just because I want to test this before closing it and I can't test it right now.

thanks.

Author

Commented:
I'm able to build the arcive file okay, but I can't seem to figure out how to link to it.

I've got an object file Test.o and i want to link to an archive library called LibPac.a   three things I've tried are'

l[todd@virims1 TimTst]$ ld -o TimTst  -library=LibPac.a TimTst.o
ld: cannot find -library=LibPac.a

l[todd@virims1 TimTst]$ ld -o TimTst  -library=LibPac TimTst.o
ld: cannot find -library=LibPac

[todd@virims1 TimTst]$ ld -o TimTst  -library=Pac TimTst.o
ld: cannot find -library=Pac

I've tried placing the options in various orders.  The LibPac.a files in in the current directory along with TimTst.o.

Commented:
First,  the file should be libPac.a, not LibPac.a .

Second, you can either include it explicitly (which includes the entire library, whether or not every module is needed):
    ld -o TimTst TimTst.o libPac.a
or you can include it as a library that gets searched
    ld -o TimTst TimTst.o -lPac

Note the funky syntax for libraries:  You omit the "lib" (which is why it must be "lib" and not "Lib"), and you omit the .a.   Also, things get done in order from left to write, so the TimTst.o must appear before the -lPac., otherwise nothing will be loaded from the library because there are no undefined references to resolve at that time.

I don't have my Linux docs handy, and I don't recall whether you need to specify the library directory if the library is in the current directory.  Thus you might need
    ld -o TimTst TimTst.o -L. -lPac
And certainly if it's in a different directory, you'll need it, e.g.
    ld -o TimTst TimTst.o -L../myLibraryDir -lPac

You can use multiple -L options if you have multiple libraries in different directories; they all get searched (hence your library names should be unique).

Gary
most important things about ar and ld have been said,
then make shure that you compile without -fpic or -fPic option

Author

Commented:
Thanks.  I got it working now.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial