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

  • SEARCH
  • Home
  • 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 6624005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:36:01+00:00 2026-05-25T21:36:01+00:00

For a Linux/g++ project, I have a helper library (libcommon.a) that I wrote that

  • 0

For a Linux/g++ project, I have a helper library (“libcommon.a”) that I wrote that is used in two different programs (“client” and “server”). One particular source file among several, oshelper.cpp, has a set of unrelated utility functions:

// header file
#ifndef OSHELPER_H
#define OSHELPER_H
    size_t GetConsoleWidth();
    uint32_t GetMillisecondCounter();
#endif

// -----------------------------------------
// Code file
#include "commonincludes.h"
#include "oshelper.h"

size_t GetConsoleWidth()
{
    struct winsize ws = {};
    ioctl(0, TIOCGWINSZ, &ws);
    return ws.ws_col;
}

uint32_t GetMillisecondCounter()
{
    timespec ts={};
    clock_gettime(CLOCK_MONOTONIC, &ts);
    return (uint32_t)(ts.tv_nsec / 1000000 + ts.tv_sec * 1000);
}

Both programs link to the library that contains these functions (libcommon.a or -lcommon).

“client” program calls both the GetConsoleWidth and GetMillisecondCounter function. And since GetMillisecondCounter ultimately depends on a call to “clock_gettime”, -lrt is a required parameter to the linker such that librt is linked in. This is expected.

“server” just calls GetConsoleWidth. It never calls GetMillisecondCounter. But without “-lrt” being passed, the linker complains about the unresolved reference to clock_gettime. Which is obviously fixed by passing -lrt to g++. And then “ldd server” shows that librt.so.1 is still a runtime dependency. So the linkage to clock_gettime clearly did not get optimized away.

But when I separate the implementation of GetConsoleWidth into a seperate source file (but still part of libcommon.a), the linker stops complaining about the unresolved reference to clock_gettime and no longer insists that I pass in -lrt.

It’s as if the g++ linker can only cull out unused object files, but not unused function calls.

What’s going on here?

Update: the compiler and linker command lines are as basic as they can get:

g++ -c oshelper.cpp
g++ -c someotherfile.cpp
etc...
ar -rv libcommon.a oshelper.o someotherfile.o ...

g++ server.cpp -lcommon -lpthread -o server
g++ client.cpp -lcommon -lrt -o client
  • 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-25T21:36:01+00:00Added an answer on May 25, 2026 at 9:36 pm

    Without special commands an .o is linked in its entirety, and thus all he dependencies are required.

    You need to build the compilation units in the library with compiler flags that put all symbols in separate sections, and then call the linker with an option that “garbage collects” sections, so that only code referenced directly or indirect from main (and maybe ctors/dtors) is linked in.

    I don’t know the commands exactly but search for gcc parameters like -ffunction-sections -fdata-sections -fvtable-gc and -gc-section(s)

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

Sidebar

Related Questions

I have a django project that runs on a Linux server, and I've been
I'm compiling a NAnt project on linux with TeamCity Continuous Integration server. I have
I have a large GUI project that I'd like to port to Linux. What
I am making a project that should compile on Windows and Linux. I have
I have a project for Linux that i've been developing for some time. Basically
I have a newbie Linux programming question. Suppose I have a project that uses
We have a smallish C++ Linux project that accompanies our large C++/MFC Windows project.
I'm trying to write a project that will have some autonomous components. One of
I do have a simple cmake project (on linux) that loads some libraries from
I have a project where there are Windows, FreeBSD and Linux servers. I have

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.