LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Undefined reference to `exp' (https://www.linuxquestions.org/questions/programming-9/undefined-reference-to-%60exp-4175649934/)

Corbeau 03-10-2019 06:11 PM

Undefined reference to `exp'
 
Hello. Sorry for resurrecting an old thread, but I have exactly the same problem and the solution proposed here didn't help. To make matters more interesting, I had similar programs working maybe a year ago and then I don't really know what happened. There was no reinstalling involved or anything.

Anyway, this is the problem.

After the problem started, I wrote a simple test program to make things more simple.
Code:

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

main()
{
double x, y;
x = 1.;
y = exp(x);
}

Then:

Code:

gcc -lm test.c
And this happens:
Code:

test.c:(.text+0x17): undefined reference to `exp'
So, any ideas?

GazL 03-11-2019 06:04 AM

Usually one puts the libraries after the file: linking is a fill in the gaps kind of deal, so you want the gap to exist first, before you fill it,

Having said that, it appears to work both ways round here on my Slackware64 box. Not sure why it works. I've certainly encountered undefined reference failures owing to incorrect order before.

Anyway, give the following a go and see if it makes any difference.
gcc test.c -lm


Failing that, there's likely something faulty with your toolchain.

Corbeau 03-11-2019 06:16 AM

Quote:

Originally Posted by GazL (Post 5972601)
gcc test.c -lm

*headdesk*

I should write memoires with the list of all the st00pid errors I encountered through life. It would be a bestseller.

Thank you, this fixed it.

GazL 03-11-2019 06:20 AM

:) You're welcome. I had to ask someone the first time I encountered this issue too.

I'm curious to understand why my Slackware64 box works when they're listed the wrong way around though. Anyone have any ideas on that?

Corbeau 03-11-2019 06:22 AM

BTW, I see that someone (admin, mod...) removed these posts from the thread titled "Undefined reference to `exp' ". I understand some people's annoyance by resurrecting old threads, but woldn't it be much more functional if all the questions and answers regarding this problem were placed under the same name? I found the old thread by searching exactly "undefined reference to `exp'". Now, someone else with the same problem as me will find that old thread, but not this particular solution so he may have to ask the same question again.

Corbeau 03-11-2019 06:24 AM

Quote:

Originally Posted by GazL (Post 5972605)
:) You're welcome. I had to ask someone the first time I encountered this issue too.

Yeah, well, like I said, it worked many moons ago, then I had a break from C and imagine my shock, disbelief and frustration when it "stopped" working...

GazL 03-11-2019 06:39 AM

Changing the thread title to "Undefined reference to `exp'" might help folk find an answer in future.

ntubski 03-11-2019 07:16 AM

Quote:

Originally Posted by GazL (Post 5972605)
I'm curious to understand why my Slackware64 box works when they're listed the wrong way around though. Anyone have any ideas on that?

https://stackoverflow.com/questions/...-errors-in-gcc says:
Quote:

Some recent distributions apparently default to using the --as-needed linker flag, which enforces that the program's object files come before the dynamic libraries. If that flag is passed, the linker will not link to libraries that are not actually needed by the executable (and it detects this from left to right). My recent archlinux distribution doesn't use this flag by default, so it didn't give an error for not following the correct order.
So maybe Slackware doesn't add this flag to the defaults?

GazL 03-11-2019 08:13 AM

Thanks ntubski. Adding -Wl,--as-needed certainly breaks it when they're in the wrong order, so that sounds very plausible.


All times are GMT -5. The time now is 02:25 AM.