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).
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