Home > database >  Why does linking libm to library require linking libm to executable using my library?
Why does linking libm to library require linking libm to executable using my library?

Time:11-24

I am writing a library. I have the following structure:

src/
├── Logger
│   ├── CMakeLists.txt
│   ├── Logger.c
│   ├── Logger.h
├── Task
│   ├── CMakeLists.txt
│   ├── Task.c
└── ThreadPool
    ├── CMakeLists.txt
    ├── Thread.c
    ├── Thread.h
    ├── ThreadPool.c
    ├── ThreadPool.h

The CMakeLists.txt are very simple. I have a Top-Level CMakeLists.txt where I include these. So I get a static library for everything. Then I search for all object files and combine them to an libsomething.a file. (Not the best way to do it for now but I like cmake but need to learn more about compiling to understand the give functionality of cmake)

This works well an fits my needs BUT I need to link against libm if a compile an executable with my library and I really don't know why. I assume that this happens because I compile a static library without using libm.a or why?

I want the enduser just to link agains my library so he don't has to link agains other non libc parts. And I was not able to find any helpful information about this.

CodePudding user response:

A static library is really nothing more than an archive of object files. Linking with a static library is equivalent to linking with all the individual object files from the library.

As such, the static library have no information about dependencies or other libraries needed. A static library isn't even linked, the object files are just added to the archive.

That's why you need to explicitly link with the dependencies of the static library yourself.

  • Related