1

I have written some code in Lib_file.h and Lib_file.cpp. I wish to convert this code to a static library. I am able to compile the code (using the command g++ -I <necessary include files> -o Lib_file.o Lib_file.cpp)to get Lib_file.o. I am also able to add it to an archive using the ar rvs Lib_file.a Lib_file.o command. Now when I try to use this library in some other code using the -L option, I get undefined reference errors. This errors point to the code in my Lib_file.o . So my question is how do I get the code in my Lib_file.cpp to link to the libraries that it uses.

I have tried the following options so far

I. After creating the Lib_file.o, I tried the following command g++ -L<include path> -l<.a files> Lib_file.o . On executing this command, I get the following error

/usr/lib/../lib64/crt1.o: In function `_start':
init.c:(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status

II. I tried to include all the necessary .a files in a new archive along with my Lib_file.o using the ar command. Still I get the undefined reference error when I try to use the Lib_file.a library with my application

Please help me out here

2
  • 1
    I strongly suggest making shared libraries, not static ones. See stackoverflow.com/a/12197285/841108 Commented Sep 2, 2012 at 12:06
  • 1
    On Linux a .a file is just a convenience archive containing a bunch of .o files (compiled but not linked). It has no meta-data. Linking with a .a file is basically the same as linking with the .o files individually. As for .o files, .a files have no way to indicate their externally dependent libraries. You need to track that separately in your build system and give the information to the linker at link-time. Commented Sep 2, 2012 at 12:21

1 Answer 1

4

First of all, all libraries are normally named something like libxyz.a where xyz is the name of the library.

Secondly, you try to create a program using only the object file you used for the library, and also linking it with itself. This will of course not work, since the library have no main function which is needed for normal programs. You have to create another program, and link that one with the library.

Like

gcc myotherprogram.c -o myotherprogram -L/some/path -lxyz

As you can see in my command line above, I placed the library last on the command line. It's needed because the linker look for dependencies in kind of reversed order.

Edit: Linking your static library with other libraries: You don't. A static library is completely standalone, and if it needs other libraries itself to work then they have to be present on the command line when compiling the actual program.

For example, lets say that library xyz depends on the standard math library (i.e. the m library). You can't "link" with it when creating the xyz library as you don't actually link static libraries, you just put a collection of object files together in an archive (ar and the .a extension is for archive). When you build the actual application that needs the xyz library you also needs to link with whatever libraries that xyz needs:

gcc myotherprogram.c -o myotherprogram -L/some/path -lxyz -lm
Sign up to request clarification or add additional context in comments.

2 Comments

I think I may have been unclear in my question. The problem that I am facing is that when I execute the command line that you have given, I get undefined reference errors for the code in the library that I have linked using -l. My question is that for the library code libxyz.cpp, if it is making use of some other libraries, then how do I link those libraries to this library.
@MananShah You need to add all libraries that you library need when compiling the finished program. Updated the answer to reflect this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.