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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:56:58+00:00 2026-06-01T05:56:58+00:00

So, I know that in C you need to link the code to the

  • 0

So, I know that in C you need to link the code to the math library, libm, to be able to use its functions. Today, while I was trying to demonstrate this to a friend, and explain why you need to do this, I came across the following situation that I do not understand.

Consider the following code:

#include <math.h>
#include <stdio.h>

/* #define VARIABLE */

int main(void)
{
#ifdef VARIABLE
    double a = 2.0;
    double b = sqrt(a);
    printf("b = %lf\n",b);
#else
    double b = sqrt(2.0);
    printf("b = %lf\n",b);
#endif
    return 0;
}

If VARIABLE is defined, you need to link against libm as you would normally expect; otherwise you get the usual main.c:(.text+0x29): undefined reference to sqrt linking error indicating that the compiler cannot find the definition for the function sqrt. I was surprised to see that if I comment #define VARIABLE, the code runs fine and the result is correct!

Why is it that I need to link to libm when variables are used but I don’t need to do so when literal constants are used? How does the compiler find the definition of sqrt when the library is not linked? I’m using gcc 4.4.5 under linux.

  • 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-01T05:56:59+00:00Added an answer on June 1, 2026 at 5:56 am

    As everyone mentions, yes it has to do with constant folding.

    With optimizations off, GCC only seems to do it when sqrt(2.0) is used. Here’s the evidence:

    Case 1: With the variable.

        .file   "main.c"
        .section    .rodata
    .LC1:
        .string "b = %lf\n"
        .text
    .globl main
        .type   main, @function
    main:
        pushl   %ebp
        movl    %esp, %ebp
        andl    $-16, %esp
        subl    $32, %esp
        fldl    .LC0
        fstpl   24(%esp)
        fldl    24(%esp)
        fsqrt
        fucom   %st(0)
        fnstsw  %ax
        sahf
        jp  .L5
        je  .L2
        fstp    %st(0)
        jmp .L4
    .L5:
        fstp    %st(0)
    .L4:
        fldl    24(%esp)
        fstpl   (%esp)
        call    sqrt
    .L2:
        fstpl   16(%esp)
        movl    $.LC1, %eax
        fldl    16(%esp)
        fstpl   4(%esp)
        movl    %eax, (%esp)
        call    printf
        movl    $0, %eax
        leave
        ret
        .size   main, .-main
        .section    .rodata
        .align 8
    .LC0:
        .long   0
        .long   1073741824
        .ident  "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
        .section    .note.GNU-stack,"",@progbits
    

    You can see that it emits a call to the sqrt function. So you’ll get a linker error if you don’t link the math library.

    Case 2: With the Literal.

        .file   "main.c"
        .section    .rodata
    .LC1:
        .string "b = %lf\n"
        .text
    .globl main
        .type   main, @function
    main:
        pushl   %ebp
        movl    %esp, %ebp
        andl    $-16, %esp
        subl    $32, %esp
        fldl    .LC0
        fstpl   24(%esp)
        movl    $.LC1, %eax
        fldl    24(%esp)
        fstpl   4(%esp)
        movl    %eax, (%esp)
        call    printf
        movl    $0, %eax
        leave
        ret
        .size   main, .-main
        .section    .rodata
        .align 8
    .LC0:
        .long   1719614413
        .long   1073127582
        .ident  "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
        .section    .note.GNU-stack,"",@progbits
    

    There’s no call to sqrt. Hence no linker error.


    With optimizations on, GCC will do constant propagation in both cases. So no linker error in either case.

    $ gcc main.c -save-temps
    main.o: In function `main':
    main.c:(.text+0x30): undefined reference to `sqrt'
    collect2: ld returned 1 exit status
    $ gcc main.c -save-temps -O2
    $ 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that I need to add the tracking code snippet at the bottom
i know that autohotkey is opensource software. but i need 'autohotkeysc.bin' 's source code
I know that the code to get a confirm message while clicking on a
I know enough to know that we need to get our application to deploy
I know that table sources need a data source to hold the data that
HI, i need a XML parser. i want to know that which one is
The example below, is just an example, I know that I don't need an
I have a query that I need to execute that I do not know
I have a subclass of UIImageView that I need to know when is touched.
Need to know this so that i could send DTMF and that is going

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.