You need to link with the math library:

gcc -o sphere sphere.c -lm

The error you are seeing: error: ld returned 1 exit status is from the linker ld (part of gcc that combines the object files) because it is unable to find where the function pow is defined.

Including math.h brings in the declaration of the various functions and not their definition. The def is present in the math library libm.a. You need to link your program with this library so that the calls to functions like pow() are resolved.

Answer from codaddict on Stack Overflow
🌐
Reddit
reddit.com › r/c_programming › "undefined reference to pow" error after including and the gcc option -lm
r/C_Programming on Reddit: "Undefined reference to pow" error after including <math.h> and the GCC option -lm
December 16, 2021 -

Hey y'all, as the title says, I'm a wee bit stuck trying to use the math.h library function "pow". Here's my code -

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

int main(int argc, char *argv[]) {

//OMITTED CODE

size_t i;
double j;
int k;
for(i = argLen - 1, k = 0; k < argLen; i--, k++) {
	j = pow(16, i);
	deciVals[k] = deciVals[k] * j;
}

//OMITTED CODE	

return 0;

}

Despite having math.h included, and using the -lm option for the GCC, I'm still getting this error -

>gcc -lm -o output hexConverter.c
/usr/bin/ld: /tmp/ccghjuxO.o: in function `main':
hexConverter.c:(.text+0x237): undefined reference to `pow'
collect2: error: ld returned 1 exit status

Any help would be appreciated, thanks guys.

Discussions

undefined reference to `pow' as of #409
As of #409, I am getting the following error when calling rpi_ws281x after installing it via scons: # github.com/rpi-ws281x/rpi-ws281x-go /usr/bin/ld: //usr/local/lib/libws2811.a(ws2811.o): in func... More on github.com
🌐 github.com
7
July 24, 2020
c - Why am I getting the error, "undefined reference to `pow' collect2: error: ld returned 1 exit status make: *** [p1] Error 1"? - Stack Overflow
Here is my makefile: CC=gcc CFLAGS=-g LDFLAGS=-lm EXECS= p1 all: $(EXECS) clean: rm -f *.o $(EXECS) 14:32:16 **** Build of configuration Default for project CH3-Programs **** make p1 ... More on stackoverflow.com
🌐 stackoverflow.com
January 8, 2017
c - Undefined reference to main - collect2: ld returned 1 exit status - Stack Overflow
I'm trying to compile a program (called es3), but, when I write from terminal: gcc es3.c -o es3 it appears this message: /usr/lib/gcc/i686-linux-gnu/4.4.5/../../../../lib/crt1.o: In function `_s... More on stackoverflow.com
🌐 stackoverflow.com
I always get 'collect2.exe: error: ld returned 1 exit status' error while compiling
If I try to run this program, I always get collect2.exe: error: ld returned 1 exit status error. Here is my terminal output - g++ .\main.cpp -o ray.exe In file included from .\main.cpp:3: .\vector.... More on github.com
🌐 github.com
11
March 9, 2021
🌐
GeeksforGeeks
geeksforgeeks.org › c language › how-to-solve-undefined-reference-to-pow-in-c-language
How to solve undefined reference to `pow' in C language? - GeeksforGeeks
July 23, 2025 - #include <stdio.h> #include <math.h> // Function to count the number of digits in a number int countDigits(int num) { int count = 0; while (num != 0) { num /= 10; ++count; } return count; } // Function to check if a number is an Armstrong number int isArmstrong(int num) { int originalNum = num; int n = countDigits(num); int result = 0; while (num != 0) { int remainder = num % 10; result += pow(remainder, n); num /= 10; } return (result == originalNum); } int main() { int low = 100, high = 500; // Define the range in the driver code printf("Armstrong numbers between %d and %d are: ", low, high); // Iterate through each number in the interval for (int i = low + 1; i < high; ++i) { if (isArmstrong(i)) { printf("%d ", i); } } return 0; } ... /tmp/ccGJHZ8f.o: In function `isArmstrong': Solution.c:(.text+0xa6): undefined reference to `pow' collect2: error: ld returned 1 exit status
🌐
GitHub
github.com › jgarff › rpi_ws281x › issues › 424
undefined reference to `pow' as of #409 · Issue #424 · jgarff/rpi_ws281x
July 24, 2020 - # github.com/rpi-ws281x/rpi-ws281x-go /usr/bin/ld: //usr/local/lib/libws2811.a(ws2811.o): in function `ws2811_set_custom_gamma_factor': /home/pi/workspace/rpi_ws281x/ws2811.c:1298: undefined reference to `pow' collect2: error: ld returned 1 exit status
Author   TwiN
🌐
LinuxQuestions.org
linuxquestions.org › questions › programming-9 › c-program-error-undefined-reference-to-pow-4175698172
C Program error : undefined reference to pow
July 22, 2021 - I typed a C program from the Internet that calculates Compound Interest. One of the lines in the program reads CIFuture = PAmount*(pow((1+ROI/100),
Top answer
1 of 1
2

The problem here is the order you link with the math library (the -lm option). Libraries should be after the sources or object files on the command line when building.

So if you ran the command to build manually, it should look like

gcc p1.c -o p1 -lm

The problem is that your makefile doesn't really do anything, and it lives on implicit rules alone. The implicit rules use certain variables in a certain order which do not place the library in the right position in your makefile.

Try instead something like this makefile:

# The C compiler to use.
CC = gcc

# The C compiler flags to use.
# The -g flag is for adding debug information.
# The -Wall flag is to enable more warnings from the compiler
CFLAGS = -g -Wall

# The linker flags to use, none is okay.
LDFLAGS = 

# The libraries to link with.
LDLIBS = -lm

# Define the name of the executable you want to build.
EXEC = p1

# List the object files needed to create the executable above.
OBJECTS = p1.o

# Since this is the first rule, it's also the default rule to make
# when no target is specified. It depends only on the executable
# being built.
all: $(EXEC)

# This rule tells make that the executable being built depends on
# certain object files. This will link using $(LDFLAGS) and $(LDLIBS).
$(EXEC): $(OBJECTS)

# No rule needed for the object files. The implicit rules used
# make together with the variable defined above will make sure
# they are built with the expected flags.

# Target to clean up. Removes the executable and object files.
# This target is not really necessary but is common, and can be
# useful if special handling is needed or there are many targets
# to clean up.
clean:
    -rm -f *.o $(EXEC)

If you run make using the above makefile, the make program should first build the object file p1.o from the source file p1.c. Then is should use the p1.o object file to link the executable p1 together with the standard math library.

🌐
Google Groups
groups.google.com › g › ns-3-users › c › IeUlrNnhiMs
Request for assistance on build failed with "Undefined reference to 'main' collect2: error: ld returned 1 exit status" error
undefined reference to 'main' , collect2: error: ld returned 1 exist status) for both algorithms whenever I try to run the algorithms on NS-3.29
Find elsewhere
🌐
Quora
quora.com › How-can-I-fix-collect2-exe-error-ld-returned-1-exit-status-in-C-programming
How to fix 'collect2.exe: error: ld returned 1 exit status' in C++ programming - Quora
Rebuild with verbose/linker command printed: add -v or run make VERBOSE=1 to see exact g++/ld invocation. Compile and link in one command to avoid missing link step: g++ main.cpp other.cpp -o prog · Isolate the error by creating minimal reproducer: remove files until the undefined reference disappears.
🌐
GitHub
github.com › DinoZ1729 › Ray › issues › 30
I always get 'collect2.exe: error: ld returned 1 exit status' error while compiling · Issue #30 · DinoZ1729/Ray
March 9, 2021 - If I try to run this program, I always get collect2.exe: error: ld returned 1 exit status error. Here is my terminal output - g++ .\main.cpp -o ray.exe In file included from .\main.cpp:3: .\vector.h: In member function 'double Vector::se...
Author   MahmudX
🌐
Blogger
cprogrammingcodesacademy.blogspot.com › 2010 › 07 › undefined-reference-to-pow-collect2-ld.html
Programming In C: undefined reference to `pow' collect2: ld returned 1 exit status
In Linux GCC compiler when we compile a c program may get error message: Error message: undefined reference to `pow' collect2: ld returned 1 exit status Compiler: Linux gcc Solution: Cause of this error is by default math.h library has not included.
🌐
GitHub
github.com › jqlang › jq › issues › 1659
Make fails on Ubuntu 18.04 · Issue #1659 · jqlang/jq
April 27, 2018 - ./.libs/libjq.a(builtin.o): In function f_pow10': /home/daadmin/StdUtils/jq/src/libm.h:253: undefined reference to pow10' collect2: error: ld returned 1 exit status Makefile:984: recipe for target ...
Author   gharris999
🌐
Reddit
reddit.com › r/cpp_questions › collect2.exe: error: ld returned 1 exit status error
r/cpp_questions on Reddit: collect2.exe: error: ld returned 1 exit status error
March 29, 2024 -

Hello there dear Redditors, i am a super newbie that trying to learn programming and i started my journey with cpp (or i tried to start). Problem is i am getting error everytime i try to write basic of basics just hello-there. I am sure that i followed the steps perfectly from video that i watched during installing process (Installing MinGW to build C++ Code on Windows). I am in need of help ( ╥ω╥ )

:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../lib/libmingw32.a(lib64_libmingw32_a-crtexewin.o): in function `main':
C:/M/B/src/mingw-w64/mingw-w64-crt/crt/crtexewin.c:67:(.text.startup+0xbd): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../lib/libmingw32.a(lib64_libmingw32_a-crtexewin.o): in function `main':
C:/M/B/src/mingw-w64/mingw-w64-crt/crt/crtexewin.c:67:(.text.startup+0xbd): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
🌐
Intel Community
community.intel.com › t5 › FPGA-SoC-And-CPLD-Boards-And › collect2-exe-error-ld-returned-1-exit-status-Shows-up-during › td-p › 647467
collect2.exe: error: ld returned 1 exit status Shows up during 'make' - Intel Community
May 11, 2020 - Hi, I am using DE1-SoC. I have a C code which uses pow and sqrt from math.h library. But when i try to make file is shows this error, and says: undefined reference to 'pow' undefined reference to 'sqrt' here is the make file content:# TARGET = prognostic2 # ALT_DEVICE_FAMILY ?= soc_cv_av SOCEDS_...
🌐
Stack Overflow
stackoverflow.com › questions › 68359935 › undefined-reference-to-pow-when-compiled-using-gcc
c - Undefined reference to pow when compiled using gcc - Stack Overflow
/usr/bin/ld: /tmp/ccUkOL31.o: in function `main': a1B.c:(.text+0xf3): undefined reference to 'pow' collect2: error: ld returned 1 exit status