Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6779935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:26:48+00:00 2026-05-26T16:26:48+00:00

Say we have huge static libraries with lots of unneeded features (in the example

  • 0

Say we have huge static libraries with lots of unneeded features (in the example below we have libraries lib1.a and lib2.a with unneeded functions g1() and f2()).

We want to build shared library with a few exported methods which use only a few functions/classes from that huge libraries. See example below: we want to export function foo().

QUESTIONS

  1. Can we tell linker (ld) which functions/methods we want to export (like we do it for DLL in Windows)?
  2. Can linker resolve dependecies and remove unneeded functions/methods? Or is there any other way to solve the problem?
  3. If you have a solution, please, write the fixes for the example below.

EXAMPLE

File 1.h:

int f1( int n );
int g1( int n );

File 2.h:

int f2( int n );

File foo.cpp:

#include "1.h"
#include "2.h"

int foo( int n )
{
    return f1( n );
}

File 1.cpp:

int f1( int n ) { return n; }
int g1( int n ) { return n; }

File 2.cpp:

int f2( int n ) { return n; }

File makefile:

CXXFLAGS = -g -I. -Wall -Wno-sign-compare

all: prepare libFoo.so

clean:
    rm -f obj/*.a obj/*.o res/*.so

prepare:
    mkdir -p src
    mkdir -p obj
    mkdir -p res

lib1.a lib2.a: lib%.a: %.o
    ar r obj/$@ obj/$*.o

1.o 2.o foo.o: %.o:
    g++ $(CXXFLAGS) -c -o obj/$@ src/$*.cpp

libFoo.so: lib1.a lib2.a foo.o
    ld -shared -o res/libFoo.so obj/foo.o -Lobj -l1 -l2

And after making target all we have nm res/libFoo.so:

...
000001d8 T _Z2f1i
0000020e T _Z2g1i
000001c4 T _Z3fooi
...

So ld has removed 2.o object file according to dependencies between object files. But didn’t remove function g1() from 1.o.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:26:49+00:00Added an answer on May 26, 2026 at 4:26 pm

    First, as Basile pointed out, you should add -fPIC flag when building lib{1,2}.a.

    Second, you get all of 1.o linked in because that’s how UNIX linkers work.

    The simplest solution (much simpler than using -flto) is to turn on linker garbage collection by adding -Wl,--gc-sections to libFoo.so link line, and to build lib{1,2}.a with -ffunction-sections -fdata-sections. This will effectively turn each function into its own separate “book”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a dictionary of names (a huge CSV file). I want
I have this huge domain object(say parent) which contains other domain objects. It takes
Say I have three files (template_*.txt): template_x.txt template_y.txt template_z.txt I want to copy them
Say I have a huge amount of code and have different kinds of error
I have a huge .htaccess file, with a lot of rewrites. I want to
Updated I have some huge data, which becomes a large table say table parent
I have a huge database that has lots and lots of poorly documented constraints.
Let's say you have a company running a lot of C/C++, and you want
Suppose that I have a huge SQLite file (say, 500[MB]). Can 10 different python
Suppose that I have a huge SQLite file (say, 500[MB]) stored in Amazon S3

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.