CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2009
    Location
    Boston
    Posts
    364

    [RESOLVED] sudden undefined reference to `acosl' linker error

    Hello,

    I am rewriting a tool that I use and have run into this linker error. The only change I am making at the moment is to go to long double for some of the calculations. When I re-ran the makefile, I get this error.

    Code:
    : undefined reference to `acosl'
    collect2: error: ld returned 1 exit status
    I have math included,

    Code:
    #include <cmath>
    and I am also linking to stdc++ in the makefile. This code has worked for a while now so I'm not sure what the issue is. I call acos in two places in one of the functions.

    Code:
    // determine radians
    long double result_radians = acos(ABC_cosine);
    
    // evaluate units for return value 
    if(degrees) {
       long double PI = 2*acos(0.0);
       result_degrees = result_radians * 180.0 / PI;
       return result_degrees;
    }
    else { return result_radians; }
    There is nothing mysterious about what I am doing here, so I have no idea why it suddenly won't work.

    I am using g++ 5.3.0 under 32-bit cygwin.

    Thanks,

    LMHmedchem
    Last edited by LMHmedchem; October 16th, 2017 at 11:41 PM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: sudden undefined reference to `acosl' linker error

    is to go to long double...so I have no idea why it suddenly won't work
    For acos() with long double as an argument, it calls ::acosl() - that'll be why it has suddenly reported a problem. You may need to link with a different library under gcc or use a different option, but as I don't use gcc I can't be more specific. Sorry.

    As a 'temp fix' you could try

    Code:
    long double result_radians = acos((double)ABC_cosine);
    which should compile/link OK.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    May 2009
    Location
    Boston
    Posts
    364

    Re: sudden undefined reference to `acosl' linker error

    Quote Originally Posted by 2kaud View Post
    For acos() with long double as an argument, it calls ::acosl() - that'll be why it has suddenly reported a problem.
    According to this page,
    http://en.cppreference.com/w/c/numeric/math/acos

    acosl() is defined in <tgmath.h> so I have included that instead of <cmath>. That doesn't seem to fix the issue as I get the same error. I should get a different linker error if g++ can't find the tgmath header file, right?

    I suppose I may not be using the c++ standard I think I am.

    LMHmedchem

  4. #4
    Join Date
    May 2009
    Location
    Boston
    Posts
    364

    Re: sudden undefined reference to `acosl' linker error

    I looked in the tgmath.h header file and there is no function defined there as acosl(). I guess this is just a macro that allows for more types to be passed as function arguments but doesn't re-define the functions. I thought there would be a separate acosl() function that accepts long double.

    It turns out that if you just include tgmath.h instead of math.h or cmath you can just use the normal acos() function and pass it whatever type you want to.

    So I have it linking again, thanks for the help.

    LMHmedchem

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: [RESOLVED] sudden undefined reference to `acosl' linker error

    For info, that's different to VS. On VS, tgmath.h doesn't exist and all you need to include is cmath! (There is a xtgmath.h though as an internal include).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured