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.
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.
undefined reference to `pow' as of #409
c - Why am I getting the error, "undefined reference to `pow' collect2: error: ld returned 1 exit status make: *** [p1] Error 1"? - Stack Overflow
c - Undefined reference to main - collect2: ld returned 1 exit status - Stack Overflow
I always get 'collect2.exe: error: ld returned 1 exit status' error while compiling
It means that es3.c does not define a main function, and you are attempting to create an executable out of it. An executable needs to have an entry point, thereby the linker complains.
To compile only to an object file, use the -c option:
gcc es3.c -c
gcc es3.o main.c -o es3
The above compiles es3.c to an object file, then compiles a file main.c that would contain the main function, and the linker merges es3.o and main.o into an executable called es3.
Perhaps your main function has been commented out because of e.g. preprocessing.
To learn what preprocessing is doing, try gcc -C -E es3.c > es3.i then look with an editor into the generated file es3.i (and search main inside it).
First, you should always (since you are a newbie) compile with
gcc -Wall -g -c es3.c
gcc -Wall -g es3.o -o es3
The -Wall flag is extremely important, and you should always use it. It tells the compiler to give you (almost) all warnings. And you should always listen to the warnings, i.e. correct your source code file es3.C till you got no more warnings.
The -g flag is important also, because it asks gcc to put debugging information in the object file and the executable. Then you are able to use a debugger (like gdb) to debug your program.
To get the list of symbols in an object file or an executable, you can use nm.
Of course, I'm assuming you use a GNU/Linux system (and I invite you to use GNU/Linux if you don't use it already).
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.