0

I want to create a trivial static library my_math.lib. Its header and source files are given as follows.

// my_math.h
#ifndef MY_MATH_H
#define MY_MATH_H

double reciprocal(double d);
#endif

// my_math.cpp
#include "my_math.h"
#include <iostream>
double reciprocal(double d)
{
    std::cout << __func__ << std::endl;

    return 1.0 / d;
}

For the sake of learning purposes, I compile it in discreate steps as follows:

  1. Preprocessing: cpp my_math.cpp > my_math.ii
  2. Compiling: g++ -S my_math.ii (where the output is my_math.s by default)
  3. Assembling: as my_math.s -o my_math.o
  4. Archiving: ar rvs my_math.a my_math.o

Question

As you can see, my library above uses cout defined in c/c++ standard libraries.

For academic purposes, is it possible to create a static library my_math.lib that statically links against c/c++ libraries? I did not find any articles that show how to do so.

Can we extend the archiving step to also include static linking against c/c++ standard libraries? In other words, is it possible to use -static option provided by g++ when creating static libraries?

Edit:

As the expected my_math.lib contains both my own code and c/c++ standard libraries, someone who uses my_math.lib will only need to have both my_math.h and iostream, and statically links against my_math.lib. The resulting .exe binary does not need c/c++ runtime anymore. This is the scenario I want to achieve for academic purposes!

Second Person Shooter
  • 14,188
  • 21
  • 90
  • 165
  • Old link but still good [Static, Shared Dynamic and Loadable Linux Libraries](http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html) (seriously, can do dial back the ICBM tests a bit?...) – David C. Rankin May 09 '21 at 04:51
  • There is. It shows you how to create and use both types of libraries, static and dynamic. (remember, a static library is basically an archive of the functions, the command `ar` is used to interact with them in a number of ways) – David C. Rankin May 09 '21 at 06:01
  • Strike that, Yes, the executable loading will be provided by the OS, so on windows the windows executable is loaded or on Linux the ELF is loaded. You are creating a library (static or dynamic) that contain the symbols (and functions attached to those symbol) so when your code executes the machine code for them is there. – David C. Rankin May 09 '21 at 06:08
  • Think of it this way. You have your basic `"Hello World"` program. You compile and link it. It is a self-contained executable for the OS on which you built it. It contains all the C run time code in the executable format. Now you create another source file that has a function that outputs `"Goodbye World"` and compile it into a library. You include a header for that in your Hello program. You then have to link Hello against that library. It doesn't matter if it is static or dynamic. It is what has the Goodbye code. Either form of library can provide that code. – David C. Rankin May 09 '21 at 06:17
  • I think where you are getting hung up is the library is something *You Will Provide*. So it doesn't matter what type of library you use. You will still have to provide *a library* (unless you compile and link all the functions with your original program). Static libraries are not used much because they are huge compared to dynamic shared object libraries because they include all the overhead needed for the functionality they provide. In either case, your library isn't something that will already be on the target machine. – David C. Rankin May 09 '21 at 06:23
  • Yes, that is 100% the correct way to think about. – David C. Rankin May 09 '21 at 06:24
  • It has been ages since I messed with static libraries (KDE3 timeframe). Several good articles should be all you need. I would search `"C++ static dynamic library difference"` and you can probably find all you need in the first page of links `:)` Good luck with your project! – David C. Rankin May 09 '21 at 06:31
  • Ohhh... We have been talking about different things. If you just have a separate source file with functions and you are calling that a "library", there is nothing special required. You can simply compile and link that at the same time, e.g. `gcc -o my_program hello.c goodbye.c` done. Or you can compile `goodbye.c` to object with `gcc -c -o goodbye.o goodbye.c` and then link that will hello as `gcc -o my_prog hello.c goodbye.o` – David C. Rankin May 09 '21 at 07:00
  • "is it possible to create a static library `my_math.lib` that statically links against c/c++ libraries?" Now I'm confused. If you don't want to create a `-static` library from `my_math.o` and you don't want to create a shared-object library. Are you wanting to dynamically load the static library at runtime? If so, that's a no-go, and the answer is here [dynamically loading static library?](https://stackoverflow.com/q/3626062/3422102) – David C. Rankin May 09 '21 at 07:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232139/discussion-between-david-c-rankin-and-kim-jong-un). – David C. Rankin May 09 '21 at 07:48

1 Answers1

0

Thank you PeterT enter image description here for informing me a link about how to merge two archives or static libraries.

I hope this answer is also useful for others in the future. Here I want to merge my_math.a with libstdc++.a.

  1. Extract my_math.o from my_math.a with ar x my_math.a.
  2. Also extract all object .o files from libstdc++.a with ar x libstdc++.a.
  3. Optionally remove both archives with del my_math.a and del libstdc++.a.
  4. Archive all object files with ar c libmerged.a *.o.

Done.

Second Person Shooter
  • 14,188
  • 21
  • 90
  • 165