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:
- Preprocessing:
cpp my_math.cpp > my_math.ii
- Compiling:
g++ -S my_math.ii
(where the output ismy_math.s
by default) - Assembling:
as my_math.s -o my_math.o
- 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!