I'm trying to write a program for class and I can't get my fractional exponent to work. I've tried using 2/3 2.0/3.0 and I've tried entering 2/3 into a separate variable, but nothing seems to work. I don't get any errors while building 2/3 but the logic does not come out correctly. I keep getting "undefined reference to 'pow' as an error which doesn't seem right at all because I'm entering two parameters as required. Here is my code:

//find hydraulicRadius to the power of 2/3
	double twobythree = 2.0/3.0;
	printf("%lf = 2/3\n", twobythree);

	double r = pow (hydraulicRadius, twobythree);

Dani AI

Generated

Short diagnosis and context: the message "undefined reference to 'pow'" is a linker error (not a compile-time type error). As pointed out, the pow prototype must be visible via <math.h>, and as hinted the actual implementation often lives in the math library. On many Unix-like toolchains that means linking libm; the undefined-reference happens when the linker cannot find the pow symbol even though the compiler accepted the source.

Common linker fixes (examples shown for gcc/ld):

gcc main.c -o main -lm
gcc -c main.c
gcc main.o -lm -o main

When linking on the command line, the -lm option (or adding m to the libraries list in an IDE) needs to be present at link time. The build console output is the authoritative place to verify that the final link command contains -lm.

Eclipse CDT specifics: add the math library under Project > Properties > C/C++ Build > Settings > Tool Settings > GCC C Linker > Libraries — click Add and enter m. Alternatively put -lm in the Linker flags (Miscellaneous) for the active toolchain. After changing settings, rebuild and inspect the Console view to confirm the linker invocation actually includes -lm. On Microsoft toolchains (MSVC) the math functions are provided by the runtime and a separate -lm is not required.

Additional correctness tips complementary to earlier replies: integer division is a frequent logic pitfall — 2/3 as integer literals yields 0, so the exponent must be floating point (for example 2.0/3.0 or an explicit cast). pow works in the real domain only for non-negative bases with non-integer exponents (negative base + fractional exponent produces NaN). For the specific 2/3 exponent, alternatives such as the cube-root-of-square approach (when available) can offer better numerical stability or performance than a generic pow call. Checking printed diagnostics (exponent value and final linker command) is a quick way to confirm both arithmetic and build steps are correct — a point already illustrated in 's diagnostic prints.

Recommended Answers

All 6 Replies

post the whole program. In the code you posted the variable hydraulicRadius was not defined. Did you include <math.h> ?

post the whole program. In the code you posted the variable hydraulicRadius was not defined. Did you include <math.h> ?

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

//////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////

//function prototypes
//char* allLower(char*);
//int toppingCost(int, int);
//char* customers(int);
double findFlow(double);

//////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////

//change string to all lowercase
/*char* allLower(char *name){
 int i = 0;
 while(name[i] != '\0')
 {
	 name[i] = tolower(name[i]);
	 ++i;
 }
 //return string in lowercase
 return name;
 }*/

//////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////


static double initialDepth=5.0;
static double channelWidth=15.0;
static double channelDepth=10.0;
static double roughness=0.014;
static double slope=0.0015;


double findFlow(double depth){
	double flow = 0.0;
	double hydraulicRadius = ( (channelDepth * channelWidth) / ( (2.0 * channelDepth) + channelWidth) );

	printf("%lf = radius\n", hydraulicRadius);

	double square = sqrt(1024);
	printf("%lf = sqrt\n", square);

	//find hydraulicRadius to the power of 2/3
	double twobythree = 2.0/3.0;
	printf("%lf = 2/3\n", twobythree);

	double r = pow (hydraulicRadius, twobythree);

	printf("%lf = radius\n", r);

	flow = ((1.486 / roughness) * (channelWidth * depth) * (r) * (slope / 2) );

	printf("%lf is the flow\n", flow);

	return flow;
}


//double calc


int main() {

	//double depth = 5.0;

	double flow = findFlow(initialDepth);

	printf("%lf is the flow", flow);





	return (EXIT_SUCCESS);
}

Are you compiling on *nix? If so you may need the -lm switch to get math.h properly.

Are you compiling on *nix? If so you may need the -lm switch to get math.h properly.

I'm using Eclipse. I completely forgot about that though, is there a way to build it differently in Eclipse?

Edit: nevermind, searched google and figured it out. Thanks.

to get math.h properly

I meant the math library not math.h itself. That may not be an issue any longer anyway, I'm not up on *nix at all anymore I just remember having to do that from a while ago.

Well, anyway glad it worked out for you.

Just a minor thing, but you could save time declaring like variables:

static double one   = 234.34,
              two   = 34520.0303,
              three = 333.33,
              ...;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.