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
26
votes
1 answer

MSVC list symbols inside a static .lib file

Is there a command line tool that I can use (or comes with) Visual Studio that will print the names of the symbols inside a C++ static lib file in a simple and easy to parse format?
kvanbere
  • 3,289
  • 3
  • 27
  • 52
25
votes
5 answers

g++: In what order should static and dynamic libraries be linked?

Let's say we got a main executable called "my_app" and it uses several other libraries: 3 libraries are linked statically, and other 3 are linked dynamically. In which order should they be linked against "my_app"? But in which order should these be…
25
votes
7 answers

How do I tell cmake I want my project to link libraries statically?

I'm trying to build an OpenCV-based project using CMake, running on Linux. So far my CMakeLists.txt files looks something like FIND_PACKAGE (OpenCV REQUIRED) ... TARGET_LINK_LIBRARIES (my-executable ${OpenCV_LIBS}) but this results in dynamically…
agnul
  • 12,608
  • 14
  • 63
  • 85
25
votes
3 answers

Can Clang compile code with GCC compiled .a libs?

I have my project currently compiling under gcc. It uses Boost, ZeroMQ as static .a libraries and some .so libraries like SDL. I want to go clang all the way but not right now. I wonder if it is possible to compile code that uses .a and .so…
DuckQueen
  • 772
  • 10
  • 62
  • 134
25
votes
1 answer

Static initialization and destruction of a static library's globals not happening with g++

Until some time ago, I thought a .a static library was just a collection of .o object files, just archiving them and not making them handled differently. But linking with a .o object and linking with a .a static library containing this .o object are…
moala
  • 5,094
  • 9
  • 45
  • 66
24
votes
4 answers

Proper way to link a static library using GCC

Why is it that some static libraries (lib*.a) can be linked in the same way that shared libraries (lib*.so) are linked (ld -l switch), but some can not? I had always been taught that all libraries, static or not, can be linked with -l..., however…
Nairou
  • 3,585
  • 5
  • 29
  • 47
24
votes
3 answers

How do static libraries do linking to dependencies?

Say I have libA. It depends on, for example, libSomething for the simple fact that a non-inline method of libA makes a call to a method in libSomething.h. How does the dependency link up in this case? Does libA have to statically link to…
jmasterx
  • 52,639
  • 96
  • 311
  • 557
24
votes
6 answers

How to force inclusion of an object file in a static library when linking into executable?

I have a C++ project that due to its directory structure is set up as a static library A, which is linked into shared library B, which is linked into executable C. (This is a cross-platform project using CMake, so on Windows we get A.lib, B.dll,…
Brian Bassett
  • 661
  • 1
  • 6
  • 18
24
votes
2 answers

How can I statically link standard library to my C++ program?

I'm using Code::Blocks IDE(v13.12) with GNU GCC Compiler. I want to the linker to link static versions of required runtime libraries for my programs, how may I do this? I already know that my executable size will increase. Would you please tell me…
AmRCPP
  • 241
  • 1
  • 2
  • 4
24
votes
4 answers

How to use libraries compiled with MingW in MSVC?

I have compiled several libraries with MingW/MSYS... the generated static libraries are always .a files. When I try to link the library with a MSVC project, Visual Studio throws 'unresolved external symbols' ... It means that the .a static library…
NumberFour
  • 3,551
  • 8
  • 48
  • 72
24
votes
10 answers

Test bundle could not be loaded because an unanticipated error

Recently I have started writing test case for one old static library. I have loaded the library to Xcode 5,Since Static Library is old , I have to manually add TestProject with Test Target. When I am trying "Product-->Test" , It launches emulator…
Brijesh Thakur
  • 6,768
  • 4
  • 24
  • 29
23
votes
1 answer

Boost libs building - difference between runtime-link and link options

I'm trying to build boost libraries in Windows 7 with MSVC (VS 2010). I have come across the options runtime-link and link in the bjam command line options. I would like to know how they are used and what is the exact difference between them. I…
Anvesh
  • 395
  • 1
  • 2
  • 10
23
votes
1 answer

Is a static variable in a library (DLL) shared by all processes referencing that library?

I know that a static variable used in a web application is shared for all users across the web application. If I have a library (DLL) that uses some static private variable, do all applications using that library share the value of that…
Devin Burke
  • 13,642
  • 12
  • 55
  • 82
23
votes
2 answers

C: Creating static library and linking using a Makefile

I am trying to understand static and shared Libraries. I want to do the following to create a makefile that does separate compilation and linking such that a static library is created and linked in forming the final static executable. I have the…
user3337714
  • 663
  • 1
  • 7
  • 25
23
votes
1 answer

Including header file from static library

I am making a test setup of a C static library and program. The library code, located in a subdirectory 'foo' of my project, contains the following files: foo/foo.c: #include void foo(void) { printf("something"); } foo/foo.h: #ifndef…
Ethan McTague
  • 2,236
  • 3
  • 21
  • 53