ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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
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.
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.
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...
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.