LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-10-2019, 06:11 PM   #1
Corbeau
LQ Newbie
 
Registered: Mar 2019
Posts: 4

Rep: Reputation: Disabled
Unhappy 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?

Last edited by Corbeau; 03-11-2019 at 07:12 AM. Reason: typo
 
Old 03-11-2019, 06:04 AM   #2
GazL
LQ Veteran
 
Registered: May 2008
Posts: 5,789

Rep: Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705
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.
 
2 members found this post helpful.
Old 03-11-2019, 06:16 AM   #3
Corbeau
LQ Newbie
 
Registered: Mar 2019
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by GazL View Post
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.
 
1 members found this post helpful.
Old 03-11-2019, 06:20 AM   #4
GazL
LQ Veteran
 
Registered: May 2008
Posts: 5,789

Rep: Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705
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?
 
Old 03-11-2019, 06:22 AM   #5
Corbeau
LQ Newbie
 
Registered: Mar 2019
Posts: 4

Original Poster
Rep: Reputation: Disabled
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.
 
1 members found this post helpful.
Old 03-11-2019, 06:24 AM   #6
Corbeau
LQ Newbie
 
Registered: Mar 2019
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by GazL View Post
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...
 
Old 03-11-2019, 06:39 AM   #7
GazL
LQ Veteran
 
Registered: May 2008
Posts: 5,789

Rep: Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705Reputation: 3705
Changing the thread title to "Undefined reference to `exp'" might help folk find an answer in future.
 
Old 03-11-2019, 07:16 AM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,590

Rep: Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908
Quote:
Originally Posted by GazL View Post
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?
 
2 members found this post helpful.
Old 03-11-2019, 08:13 AM   #9
GazL
LQ Veteran
 
Registered: May 2008
Posts: 5,789

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C program compilation errors pallavibhat89 Programming 7 02-16-2011 12:32 AM
Compilation errors with compiling a program with boost libraries 1.39 Metal_Koola Programming 5 12-04-2009 07:13 PM
Errors, Errors, and more Errors (KDE 3.4.x GUI Errors) Dralnu Linux - Software 2 05-13-2006 08:30 AM
errors,errors,errors!!! randell6564 Mandriva 2 01-15-2006 02:29 AM
Grub errors...grub errors...grub errors smattmac Linux - Newbie 1 06-13-2005 02:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration