Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,493 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,705 people online right now. Registration is fast and FREE... Join Now!




Compile Error - Math Library

 
Reply to this topicStart new topic

Compile Error - Math Library, Question from Kira89

Amadeus
7 Mar, 2008 - 05:57 AM
Post #1

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 39 times
Dream Kudos: 25
My Contributions
PLEASE NOTE: THIS POST IS FROM KIRA89. ALL REFERENCES TO GCC HAVE BEEN CHANGED TO GC


Hello,

I am new to C and I'm getting an error I have no idea how to fix.

For my assignment I am making a program that finds the area under a curve
using trapezoidal approximation.

Here's my code:

CODE

# Makefile for Lab7.c
# CS201
# Makefile for Multiple Files
# Lab7.o: Lab7.c Proto.h

Lab7:   Lab7.o Trap.o XSquaredSinx.o HalfCircle.o TestFunction.o
               gc -o Lab7 Lab7.o Trap.o XSquaredSinx.o HalfCircle.o TestFunction.o

Lab7.o: Lab7.c Proto.h
       gc -c Lab7.c

XSquaredSinx.o: XSquaredSinx.c
       gc -c XSquaredSinx.c

HalfCircle.o: HalfCircle.c
       gc -c HalfCircle.c

TestFunction.o: TestFunction.c
       gc -c TestFunction.c


CODE

/* Proto.h - Function Prototypes */

#ifndef _PROTO_H
#define _PROTO_H
void Trap(double f(double f_arg), double a, double b, double n, double *h,
double *area);
double XSquaredSinx(double x);
double HalfCircle(double x);
double TestFunction(double x);
#endif


CODE

/* Lindsey Hanna
* lab6 section3
* February 36, 2008
* Pg. 321: Create a program to assist with the calculation of intravenous
rates.
*/

#include <stdio.h>                                      // printf, scanf, fprint, fscanf, fopen, fclose
definitions
#include <stdlib.h>                                     // exit definition
#include <math.h>                                       // math library
#include "Proto.h"                                      // prototypes of methods for calculation


int i, j;                                                       // variables for 'for' loops

int main(void)
{
       double h = 0;
       double area = 0;

       printf("CS201 - Lab 7 - Area Under The Curve\n");
       printf("g(x) = XSquaredSinX\n");
       double a = 0.00000;
       double b = 3.14159;
       int n = 2;
       printf("a = %.5d, b = %.5d", a, b);
       for (i = 1; i < 8; i++)
       {
               n = pow(n, i);
               h = (b-a)/n;
               for (j = 0; j < n;j++)
               {
                       b = a + h;
                       Trap(XSquaredSinx, a, b, n, &h, &area);
                       a = b;
               }
               printf("n = %3d, h = %7.5d, area = %12.5d", n, h, area);
       }


       printf("h(x) = HalfCircle\n");
       a = -2.00000;
       b = 2.00000;
       n = 0;
       printf("a = %.5d, b = %.5d", a, b);
       for (i = 1; i < 8; i++)
       {
               n = pow(n, i);
               h = (b-a)/n;
               for (j = 0; j < n;j++)
               {
                       b = a + h;
                       Trap(HalfCircle, a, b, n, &h, &area);
                       a = b;
               }
               printf("n = %3d, h = %7.5d, area = %12.5d", n, h, area);
       }



       printf("f(x) = TestFunctopm\n");
       a = -0.00000;
       b = 12.00000;
       n = 0;
       printf("a = %.5d, b = %.5d", a, b);
       for (i = 1; i < 8; i++)
       {
               n = pow(n, i);
               h = (b-a)/n;
               for (j = 0; j < n;j++)
               {
                       b = a + h;
                       Trap(TestFunction, a, b, n, &h, &area);
                       a = b;
               }
               printf("n = %3d, h = %7.5d, area = %12.5d", n, h, area);
       }
       return(0);
}


CODE

/* Function designed to calculate area under a curve using Trapezoidal Rule
  Trapezoidal rule: T = (h/2)(f(a) + f(b) + 2(summation from i = 1, to
n-1 of f(xi)
  Pre: f(x) function, a, b, n defined.*/
#include <stdio.h>
void Trap(double f(double f_arg),                                                               // input: function under which
to find area
                 double a, double b,                                                                   // input: interval for area calculation
                 double n,                                                                                     // input: number of trapezoids
                 double *h,                                                                                    // output:
                 double *area)                                                                                 // output: area under function on [a, b]
{
       double heightA;
       double heightB;
       double heightAverage;

       heightA = f(a);
       heightB = f(b);
       heightAverage = (heightA + heightB) / 2;
       *area = (*area + (heightAverage * n));
}


CODE

/* Function designed to calculate value of (x^2)(six(x))
  Pre: x defined*/

#include <math.h>
#include <stdio.h>
double XSquaredSinx(double x)
{
       double X_2;                                                                     // local variable for calculation of x squared
       double sinX;                                                            // local variable for calculation of Sin(x)
       double value;                                                           // local variable for ending calculation of
(x^2)(six(x))
       X_2 = x * x;                                                            // calculation of x squared
       sinX = sin(x);                                                          // calculation of Sin(x)
       value = X_2 * sinX;                                             // calculates (x^2)(six(x)) and assigns to
'value' variable
       return value;                                                           // returns value
}

/* Post: (x^2)(six(x) calculated, returned*/


CODE

/* Function designed to calculate half-circle with radius 2
  pre: x defined */

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

double HalfCircle(double x)
{
       double h;
       h = 4 - (x*x);
       h = sqrt(h);
       return h;
}


CODE

/* Function stub for TA use */

double TestFunction(double x)
{
       return x;
}

// This function is not supposed to do anything, it's a stub method for
the use of my TA


When I compile normally I get the following error messages:

[lhanna@cslab10 lab7]$ make
gc -c Lab7.c
gc -o Lab7 Lab7.o Trap.o XSquaredSinx.o HalfCircle.o TestFunction.o
Lab7.o: In function `main':
Lab7.c:(.text+0x8b): undefined reference to `pow'
Lab7.c:(.text+0x1bc): undefined reference to `pow'
Lab7.c:(.text+0x2ed): undefined reference to `pow'
XSquaredSinx.o: In function `XSquaredSinx':
XSquaredSinx.c:(.text+0x22): undefined reference to `sin'
HalfCircle.o: In function `HalfCircle':
HalfCircle.c:(.text+0x3e): undefined reference to `sqrt'
collect2: ld returned 1 exit status
make: *** [Lab7] Error 1


When I compile using a method my C teacher suggested for problems with
connecting the math.h library, I get the follow error message:

[lhanna@cslab10 lab7]$ gc -lm -o Lab7 Lab7.c
/tmp/ccK7l5Nf.o: In function `main':
Lab7.c:(.text+0xf0): undefined reference to `XSquaredSinx'
Lab7.c:(.text+0xf5): undefined reference to `Trap'
Lab7.c:(.text+0x221): undefined reference to `HalfCircle'
Lab7.c:(.text+0x226): undefined reference to `Trap'
Lab7.c:(.text+0x352): undefined reference to `TestFunction'
Lab7.c:(.text+0x357): undefined reference to `Trap'
collect2: ld returned 1 exit status


I have no idea how to fix this.

Any help would be awesome.

Thanks,
Kira89
User is offlineProfile CardPM
+Quote Post

luke.guildner
RE: Compile Error - Math Library
7 Mar, 2008 - 08:06 AM
Post #2

New D.I.C Head
*

Joined: 7 Mar, 2008
Posts: 1


My Contributions
Kira89,
Since your teacher corrected the errors with the math library, these are the only errors you need to worry about:

[lhanna@cslab10 lab7]$ gc -lm -o Lab7 Lab7.c
/tmp/ccK7l5Nf.o: In function `main':
Lab7.c:(.text+0xf0): undefined reference to `XSquaredSinx'
Lab7.c:(.text+0xf5): undefined reference to `Trap'
Lab7.c:(.text+0x221): undefined reference to `HalfCircle'
Lab7.c:(.text+0x226): undefined reference to `Trap'
Lab7.c:(.text+0x352): undefined reference to `TestFunction'
Lab7.c:(.text+0x357): undefined reference to `Trap'
collect2: ld returned 1 exit status

Each of theses errors is occurring where you call the Trap function, with the name of your other function inside the arguments to the Trap function. It looks like what you are trying to do is pass a function to the Trap function, but this is not what you have specified in the Trap function prototype in the header. Without variable names, this is the current signature for your Trap function:
CODE

void Trap(double, double, double, double, double *, double*);

The prototype signature should be:
CODE

void Trap(double (*)(double), double, double, double, double *, double *);


Here is a very simple example of how to pass a pointer of a function:
CODE

void sum(int one, int two)
{
  printf("the sum of 1 and 2 is %d.\n", one+two);

  return;
}

trap(void (*f)(int, int), int one, int two)
{
  f(one, two);

  return;
}

int main()
{
  trap(sum, 1, 2);

  return 0;
}


I hope this helps you... smile.gif

~Luke

This post has been edited by luke.guildner: 7 Mar, 2008 - 08:21 AM
User is offlineProfile CardPM
+Quote Post

kira89
RE: Compile Error - Math Library
18 Mar, 2008 - 11:29 AM
Post #3

D.I.C Head
**

Joined: 27 Nov, 2007
Posts: 54



Thanked: 1 times
My Contributions
QUOTE(luke.guildner @ 7 Mar, 2008 - 09:06 AM) *

Kira89,
Since your teacher corrected the errors with the math library, these are the only errors you need to worry about:

[lhanna@cslab10 lab7]$ gc -lm -o Lab7 Lab7.c
/tmp/ccK7l5Nf.o: In function `main':
Lab7.c:(.text+0xf0): undefined reference to `XSquaredSinx'
Lab7.c:(.text+0xf5): undefined reference to `Trap'
Lab7.c:(.text+0x221): undefined reference to `HalfCircle'
Lab7.c:(.text+0x226): undefined reference to `Trap'
Lab7.c:(.text+0x352): undefined reference to `TestFunction'
Lab7.c:(.text+0x357): undefined reference to `Trap'
collect2: ld returned 1 exit status

Each of theses errors is occurring where you call the Trap function, with the name of your other function inside the arguments to the Trap function. It looks like what you are trying to do is pass a function to the Trap function, but this is not what you have specified in the Trap function prototype in the header. Without variable names, this is the current signature for your Trap function:
CODE

void Trap(double, double, double, double, double *, double*);

The prototype signature should be:
CODE

void Trap(double (*)(double), double, double, double, double *, double *);


Here is a very simple example of how to pass a pointer of a function:
CODE

void sum(int one, int two)
{
  printf("the sum of 1 and 2 is %d.\n", one+two);

  return;
}

trap(void (*f)(int, int), int one, int two)
{
  f(one, two);

  return;
}

int main()
{
  trap(sum, 1, 2);

  return 0;
}


I hope this helps you... :)

~Luke



So I fixed the prototype for my Trap function to what you posted but the compiling still won't work. I get:

[lhanna@cslab6 lab7]$ gc -lm -o Lab7 Lab7.c
/tmp/cc63kPYA.o: In function `main':
Lab7.c:(.text+0xf0): undefined reference to `XSquaredSinx'
Lab7.c:(.text+0xf5): undefined reference to `Trap'
Lab7.c:(.text+0x221): undefined reference to `HalfCircle'
Lab7.c:(.text+0x226): undefined reference to `Trap'
Lab7.c:(.text+0x352): undefined reference to `TestFunction'
Lab7.c:(.text+0x357): undefined reference to `Trap'
collect2: ld returned 1 exit status

again...

Anymore ideas? =S

-Kira
User is offlineProfile CardPM
+Quote Post

Datalus
RE: Compile Error - Math Library
22 Apr, 2008 - 12:04 AM
Post #4

D.I.C Head
Group Icon

Joined: 18 Dec, 2005
Posts: 235


Dream Kudos: 35
My Contributions
I have a similar problem. Here is a very simple example I pulled from here.

CODE

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

int main()
{
     double param, result;
     param = 1024.0;

     result = sqrt (param);

     printf ("sqrt(%lf) = %lf\n", param, result );
     return 0;
}


During compile-time, I get the following errors.

CODE

datalus@evo3:~> gcc test.c -o test
/tmp/cc6o8xhz.o: In function `main':
test.c:(.text+0x35): undefined reference to `sqrt'
collect2: ld returned 1 exit status


Is it possible that the GCC syntax has changed? I'm using GCC 4.2.1. I may as well show my compile-params:

CODE

datalus@evo3:~> gcc -v
Using built-in specs.
Target: i586-suse-linux
Configured with: ../configure --enable-threads=posix --prefix=/usr --with-local-prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib --libexecdir=/usr/lib --enable-languages=c,c++,objc,fortran,obj-c++,java,ada --enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.2.1 --enable-ssp --disable-libssp --disable-libgcj --with-slibdir=/lib --with-system-zlib --enable-shared --enable-__cxa_atexit --enable-libstdcxx-allocator=new --disable-libstdcxx-pch --program-suffix=-4.2 --enable-version-specific-runtime-libs --without-system-libunwind --with-cpu=generic --host=i586-suse-linux
Thread model: posix
gcc version 4.2.1 (SUSE Linux)


:Datalus

This post has been edited by Datalus: 22 Apr, 2008 - 12:05 AM
User is offlineProfile CardPM
+Quote Post

Datalus
RE: Compile Error - Math Library
22 Apr, 2008 - 02:26 AM
Post #5

D.I.C Head
Group Icon

Joined: 18 Dec, 2005
Posts: 235


Dream Kudos: 35
My Contributions
I forgot the "-lm" argument for gcc. Duhm.

This post has been edited by Datalus: 22 Apr, 2008 - 05:17 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 07:56PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month