Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1K
Views
CMake link a shared library to static libraries

I am porting an AutoTools project to CMake.

What AutoTools does:

  • builds some static libraries
  • builds some shared libraries and links static ones into shared
  • builds an executable, links it to shared libraries

What I've managed to do with CMake:

  • build some static libraries - add_library(staticfoo <src>)
  • build some shared libraries - add_library(sharedfoo SHARED <src>) and link them - target_link_libraries(sharedfoo staticfoo)
  • build an executable, link it to shared libraries - target_link_libraries(exe sharedfoo), but that dragged the static libraries in again, too.

So, the resulting link command for the executable has static libs in addition to shared. Which doesn't correspond to the command generated by AutoTools project.

I've tried target_link_libraries(sharedfoo PRIVATE staticfoo), but that doesn't get the symbols from the static lib into the interface of the shared lib.

How to get the symbols without that 'transitive' behavior?

(in platform-independent way)

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

As I know, CMake doesn't allow to mix STATIC and SHARED libraries.

If your staticfoo library is used solely as part of other libraries/executables, you can define it as

add_library(staticfoo OBJECT <src>)

and use then as some sort of sources when build other library:

add_library(sharedfoo SHARED <src> $<TARGET_OBJECTS:staticfoo>)

For more info see documentation on add_library.

over 4 years ago · Santiago Trujillo Report

0

To resolve this case you need to do few things:

  • first of all make sure you've compiled static libraries w/ -fPIC, so they'll contain a relocatable code (which would be a part of a shared library later)
  • then, you need to control symbols visibility when compiling all libraries, so being a part of shared library, symbols came from the static one would be visible
  • and finally, yes, you need to specify PRIVATE <static libs> when linking your shared library, so the linker command line for your executable wouldn't have any static libs
over 4 years ago · Santiago Trujillo Report

0

See my answer here. Basically add /WHOLEARCHIVE, -all_load, or --whole-archive to the linker flags.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!