Questions tagged [static-libraries]

A static library is an archive of object files. Used as a linker input, the linker extracts the object files it needs to carry on the linkage.

The needed object files are those that provide the linker with definitions for symbols that it finds are used, without definition, in other input files. The needed object files, and no others, are extracted from the archive and input to the linkage exactly as if they were individual input files in the linkage command and the static library was not mentioned at all.

Linkers may differ as to whether the position of a static library in the sequence of input files affects its availability to be searched for needed object files. Some linkers (e.g. GNU ld) will search a static library only to obtain definitions for unresolved symbol references used in earlier input files. For such a linker, success requires a static library to be input after all other files that depend on it for symbol definitions. Other linkers (e.g. Microsoft link) will search a static library to obtain a definition for any otherwise unresolved symbol reference.

A linker will normally support an option (GNU ld: --whole-archive, MS link: /WHOLEARCHIVE) to override the default processing of static libraries and instead link all the contained object files, whether they are needed or not.

A static library contributes nothing to a linkage except the object files that are extracted from it, which may be vary in different linkages. It is to be contrasted with a shared library, another kind of file altogether with a very different role in linkage.

4482 questions
40
votes
3 answers

How to compile a static library using the Android NDK?

I'm trying to compile a static library to use on Android but I can't figure out how to compile it. The library uses standard libraries (stdio.h etc...) and libxml2. I am trying to compile using arm-eabi-gcc but I get the following…
Reimund
  • 2,346
  • 1
  • 21
  • 25
40
votes
2 answers

Find out what functions a static C library has

I have a static C library (say mylib.a) and I was wondering if it's possible to find out what functions are implemented inside that file. I don't have a corresponding header file. what I need is like the equivalent of javap for Java.
cd1
  • 15,908
  • 12
  • 46
  • 47
39
votes
5 answers

How to link to a static library in C?

I use code::blocks to compile my static library. The output result is a libstatic.a file. Now, how do I link to my library to use functions that were compiled? (I tried to use #include "libstatic.a" but my project doesn't compile)
user188276
39
votes
5 answers

Is there a way to determine which version of Visual Studio was used to compile a static library?

I have a collection of static libraries (.lib) files one of which may have been built with a different version of Visual Studio. This is causing the code generation of a project that links against all of them to fail. Is there any way to determine…
Bill Carey
  • 1,395
  • 1
  • 11
  • 20
37
votes
2 answers

Why doesn't __attribute__((constructor)) work in a static library?

In the following example, the program should print "foo called\n": // foo.c #include __attribute__((constructor)) void foo() { printf("foo called\n"); } // main.c int main() { return 0; } If the program is compiled like this, it…
Jay Conrod
  • 28,943
  • 19
  • 98
  • 110
36
votes
3 answers

CMake and Static Linking

I'm using CMake in a project, and I'm trying to statically link some libraries. I've set: set(BUILD_SHARED_LIBS OFF) set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++ -static") set_target_properties(icarus PROPERTIES…
Ælex
  • 14,432
  • 20
  • 88
  • 129
35
votes
6 answers

It gives errors when using Swift Static library with Objective-C project

I need to make swift static library for my requirement. I made swift static library which uses swift and Obj-c code. I have included Obj-c files via bridge file. I am able to compile swift static library without any error and get libMySwift.a file.…
Vishal Gabani
  • 421
  • 2
  • 5
  • 12
33
votes
3 answers

Linking a shared library against a static library: must the static library be compiled differently than if an application were linking it?

At least on Linux and Solaris, static libraries are really just a bunch of compiled .o's tossed into one big file. When compiling a static library, usually the -fpic flag is ommited, so the generated code is position dependent. Now say my static…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
32
votes
12 answers

What is a dynamic language, and why doesn't C# qualify?

Listening to a podcast, I heard that C# is not dynamic language while Ruby is. What is a "dynamic language"? Does the existence of dynamic languages imply that there are static languages? Why is C# a dynamic language and what other languages are…
egyamado
  • 1,111
  • 4
  • 23
  • 44
32
votes
3 answers

How to distribute Swift Library without exposing the source code?

The first thing I tried is to create a static library but later I found out that it's not supported yet. Apple Xcode Beta 4 Release Notes: Xcode does not support building static libraries that include Swift code. (17181019) I was hoping that…
Jimmy
  • 10,427
  • 18
  • 67
  • 122
32
votes
2 answers

What is the difference between .LIB and .OBJ files? (Visual Studio C++)

I know .OBJ is the result of compiling a unit of compilation and .LIB is a static library that can be created from several .OBJ, but this difference seems to be only in the number of units of compilation. Is there any other difference? Is it the…
31
votes
6 answers

Can you reference Xib files from static libraries on the iPhone?

In my app, i currently have all my code separated into a static library, to make it easier to set up the xcode project targets for the actual app and for unit tests for my code. The problem with this is that i want to put most of my xib files in…
Kevlar
  • 8,804
  • 9
  • 55
  • 81
31
votes
2 answers

Android NDK: Link using a pre-compiled static library

I'm trying to port Jnetpcap to Android in order to use it for parsing .pcap files. Jnetpcap is a java wrapper for libpcap which uses JNI. I have compiled libpcap as a static library using the android's source code tree. When compiling Jnetpcap as a…
Jimix
  • 1,539
  • 3
  • 15
  • 20
31
votes
3 answers

What does the "rcs" option in ar do?

I did read the man file but it does not help. rcs seems to be the most popular option to pass to ar, but the meaning isn't so clear to me. So c means to create a new archive, but then why use r? which seems to stand for "replace"? What will the s…
user3810155
30
votes
4 answers

"undefined reference" when linking against a static library

g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5 I have the following static library called sdpAPI.a. I am having problems trying to link it with my test application. Just wondering if I am doing something wrong. The static library has been built with…
ant2009
  • 27,094
  • 154
  • 411
  • 609