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 7857731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:07:49+00:00 2026-06-02T21:07:49+00:00

Possible Duplicate: gcc: why the -lm flag is needed to link the math library?

  • 0

Possible Duplicate:
gcc: why the -lm flag is needed to link the math library?

Generally speaking, in order to use any of the math functions, apart from including the header file math.h, you have to link with the linker option -lm. -l here would mean the linker option to search of the specific library libm.o.

Why does GCC does include this library by default? Is it because the library heavily uses a math-coprocessor and it requires to add the extra bit of code to initialize the floating point initialization (I may use the wrong terminology here)?

Note

I just reviewed all the answers mentioned in question gcc: why is the -lm flag needed to link the math library?. This doesn’t makes much sense to me. There are three basic reasons attributed:

  1. The standard libraries are guaranteed to be available. Linking other POSIX libraries like pthread explicitly makes sense, but why do we have to do an explicit link for a standard library? Even the historical reason is not very clear.

  2. Why was libm separated from libc?

  3. Why are we still inheriting these behaviors in the recent GCC compilers? What simplicity does it achieve? Here is what I tested, without libm and with libm. For the one without libm, I have written my own version of Pow().

Here is the example:

cd ~/Projects/GIPL6_2
$ ls -1 Test_*|xargs -I{} sh -c "echo {} && echo "-----------------" && cat {}"

Test_withlibm.c
-----------------
#include<stdio.h>
#include<math.h>
int main() {
    int i=20;
    double output1=pow(2.618033988749895,i);
    return 0;
    }
Test_withoutlibm.c
-----------------
#include<stdio.h>
#include<math.h>
double Pow(double _X, int _Y)  {
    double _Z = 1;
    for (; _Y; _X *= _X) {
    if (_Y & 1) _Z *= _X;
    _Y >>= 1;
    }
    return _Z;
    }
int main() {
    int i=20;
    double output1=Pow(2.618033988749895,i);
    return 0;
    }

$ gcc Test_withlibm.c -lm -o Main_withlibm.o
$ gcc Test_withoutlibm.c -o Main_withoutlibm.o
$ objdump -d Main_withoutlibm.o|wc -l

261

$ objdump -d Main_withlibm.o|wc -l

241
  • 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-06-02T21:07:51+00:00Added an answer on June 2, 2026 at 9:07 pm

    It is to accomodate systems (mainly embedded) where floating point math is not possible or necessary. It’s kind of historical indeed but don’t forget that gcc and most other C compilers were written in a time where a 386SX was considered a high performance processor.

    To give an example, when I still worked in embedded computing, we used standard compilers (Microsoft and Borland) to generate code for our processors (Z80, 80186 and 68030). If the compilers had by default linked to the math library we would have been in trouble as none of our systems had floating point capabilities or even needed them.

    It’s true that 30 years afterwards it seems silly but the reason was sound at that time.

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

Sidebar

Related Questions

Possible Duplicate: Why do you have to link the math library in C? When
Possible Duplicate: Creating ELF instead of a.out When I use the command gcc hello.cpp
Possible Duplicate: Can I list-initialize a vector of move-only type? I use gcc 4.6.1
Possible Duplicate: How to: URL re-writing in PHP? How can a website use an
Possible Duplicate: How to minify php page html output? Any reason not to strip
Possible Duplicate: problem with template inheritance This code doesn't compile in GCC: template <typename
Possible Duplicate: How do you get assembler output from C/C++ source in gcc? Hello
Possible Duplicate: Should a function have only one return statement? Hello, gcc 4.4.4 c89
Possible Duplicate: Does there exist a static_warning? I often use #warning scattered through my
Possible Duplicate: Why does gcc have “â” in all its error messages? New to

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.