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 22, 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 22, 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.

🌐
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.
Find elsewhere
🌐
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.
🌐
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
🌐
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
🌐
Reddit
reddit.com › r/cpp_questions › collect2.exe: error: ld returned 1 exit status | undefined reference to...
r/cpp_questions on Reddit: collect2.exe: error: ld returned 1 exit status | undefined reference to...
October 22, 2022 -

Howdy!

I'm creating a project for an ESP8266 with the Arduino framework using PlatformIO. I'm having trouble building the project, however.

I have a main.cpp that looks something like this:

#include <Arduino.h>
#include <deque>
#include <variant>

#include "communication.h"
#include "components.h"
#include "helpers.h"

// ...

constexpr size_t serial_buffer_size = 10;
std::array<char, serial_buffer_size> serial_buffer{};

void setup() {
    // ...
}

void loop() {
    // ...
    auto color = receive_color(serial_buffer);
}

Additionally, I have communication.h and communication.cpp, which look like this: communication.h:

#pragma once
#include <Arduino.h>
#include <variant>

#include "components.h"

template <size_t buffer_size>
std::optional<Color> receive_color(std::array<char, buffer_size>& buffer);
// ...

communication.cpp:

#include <Arduino.h>
#include <charconv>
#include <variant>

#include "communication.h"
#include "components.h"
#include "helpers.h"

template <size_t buffer_size>
std::optional<Color> receive_color(std::array<char, buffer_size>& buffer) {
    // ...
}

When I attempt to build my project, I run into

c:/users/USER/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld.exe: .pio\build\d1_mini_lite\src\main.cpp.o:(.text.loop+0x1c): undefined reference to `_Z13receive_colorILj10EESt8optionalI5ColorERSt5arrayIcXT_EE'
c:/users/USER/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld.exe: .pio\build\d1_mini_lite\src\main.cpp.o: in function `loop':
C:\Users\USER\ProjectPath/src/main.cpp:61: undefined reference to `_Z13receive_colorILj10EESt8optionalI5ColorERSt5arrayIcXT_EE'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\d1_mini_lite\firmware.elf] Error 1

I've been looking at this for a while now, but I can't spot the mistake. I think I need another pair of eyes.

The project builds without errors when I comment out the line auto color = receive_color(serial_buffer);

For debugging, I have tried making a dummy function that just prints something. Placing the function prototype in the header and writing the implementation in the corresponding source file, allows me to call the function from main without problems. Thus, I think the compiler is able to find the necessary files without problem.

I have also tried reinstalling the toolchain, as mentioned here: https://stackoverflow.com/questions/64778211/platformio-on-vscode-not-compiling-collect2-exe-error-ld-returned-1-exit-stat

That didn't help either. I suspect that I'm just doing something silly with the array that I'm trying to pass to receive_color, but I can't tell what's wrong.

🌐
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
🌐
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
🌐
Edureka Community
edureka.co › home › community › categories › others › what does collect2 error ld returned 1 exit...
What does collect2 error ld returned 1 exit status mean | Edureka Community
February 22, 2022 - I see the error collect2: error: ld returned 1 exit status very often. For example, when I ... status Could anyone help me understand what it means?