ADISC  
Rules Answers Wiki Galleries Live Chat Links
Go Back   ADISC > Public Forums > Off-topic > Computers and Games
Register Blogs FAQ Members List Social Groups Mark Forums Read

Closed Thread Post New Thread
 
LinkBack Thread Tools Display Modes
Old 12-08-2009   #1 (permalink)
Cnsnnts - Stt f Mnd
Staff Member
 
mzkkbprmt's Avatar
 
Join Date: Apr 2008
Location: Jarvis Cocker Land
Age: 18
Posts: 1,817
Threads: 54
Gallery Uploads: 40
Blog Entries: 31
Reputation: 58
Default Help with programming in C

Hi guys. I've recently started learning C. I have borrowed a good book from the library, and, as an exercise, I had to make a program to show the relative mass of an object at speeds close to c, for a rest mass of 1 kg. This is my program:

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

#define START 0.9*3e8
#define END 0.99*3e8
#define INC 0.01*3e8

double convert(double v)
{
	double m, m0, c;
	m0 = 1.0;
	c = 3.0e8;

	m = m0 / sqrt(1 - (v / c) * (v / c));
	return m;
}

int main(void)
{
	double v;
	double mass;
	
	for (v = START; v <= END; v += INC)
	{
		mass = convert(v);
		printf("At velocity %f%% of c, an object of rest mass 1 kg weighs %f kg", v, mass);
	}
	return 0;
}
However, when trying to compile I get this error.

Code:
andrew@andrew-laptop:~/Documents/C$ gcc relmass.c -o relmass
/tmp/cc4LgTLT.o: In function `convert':
relmass.c:(.text+0x50): undefined reference to `sqrt'
collect2: ld returned 1 exit status
As far as I understanding it it is saying that sqrt() is undefined. However I know that sqrt() is defined in math.h which I did include. So can anyone tell me what is going wrong.

If it helps I am running ubuntu 9.04 with gcc version 4.3.3 (Ubuntu 4.3.3-5ubuntu4).

Can anyone tell me why this program won't compile, or spot any mistakes?

Thanks for your help.
mzkkbprmt is offline  
Old 12-08-2009   #2 (permalink)
Regular
 
element's Avatar
 
Join Date: Mar 2008
Location: UT/NY
Age: 22
Posts: 88
Threads: 2
Reputation: 3
Send a message via AIM to element Send a message via MSN to element Send a message via Yahoo to element
Default

You need to link the math shared library in when you compile. Try:
Code:
gcc relmass.c -lm -o relmass
The -lm flag tells GCC to link in the libm math library which has the sqrt (and other assorted math functions defined in math.h) function defined in it.
element is offline  
Old 12-08-2009   #3 (permalink)
Cnsnnts - Stt f Mnd
Staff Member
 
mzkkbprmt's Avatar
 
Join Date: Apr 2008
Location: Jarvis Cocker Land
Age: 18
Posts: 1,817
Threads: 54
Gallery Uploads: 40
Blog Entries: 31
Reputation: 58
Default

Thanks for that. Solved it.

Note to self - find time to read through man gcc
mzkkbprmt is offline  
Closed Thread

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Java] (Bi)Monthly Programming Challenge, anyone interested? Mink Computers and Games 5 13-02-2009 04:05 PM
Programming Language James Computers and Games 36 27-06-2008 09:44 PM
Favorite Programming Language Dreamaker Computers and Games 16 30-04-2008 05:36 AM
Programming Incomplete Dude Computers and Games 20 25-03-2008 05:17 AM


All times are GMT. The time now is 08:23 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
Site content is Copyright ADISC.org 2008.
Content from this site may not be reproduced anywhere else
without the advance written permission of the webmaster, or author.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16