. Advertisement .
..3..
. Advertisement .
..4..
Hi developer experts, I have a small but frustrating use case, and so far, I couldn’t get my head around this problem & ideal solution. I am running my command and facing one problem with the clang: error: linker command failed with exit code 1. Below is the command I used:
#include <stdio.h>
int power(int m, int n);
int main()
{
int i;
for (i=0; 1<10; ++i)
printf("%d %d %d\n", i, power(2,i), power(-3,i));
return 0;
}
When I run it, I get the following error:
Undefined symbols for architecture x86_64:
"_power", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I am looking forward to gaining some knowledge from all experts. Thank you, guys!
The cause:
There is no definition in code in the function which you has just declared. Compiler(here clang), cannot link
power
function with its definition at the time of linking so linker throws an error in this type of situation and the error happens.Solution:
If you define
Your
power
function declaration will be linked to its definition by the linker. You won’t get an error. I have created function for integer number as below:This
gcc file.c
compilation is required.The definition of function
int power (int base,int n)
was not included in your book’s main pages.You must define the function that you are declaring prototype of.
The following definition will ensure that your code compiles exactly as you want it to.
PRE-EDIT NOW THIS ISN’T RELEVANT, BUT USEFULL
You may want to use the
math.h
functionpow()
, I believe.C library function
pow(double a, double b)
returnsa
, which is twice as powerful asb
. This function returns double values so that"%lf"
will print the correct specifier.You will only need to include the header file in this instance
Include it in your program.
It is not necessary to provide function declaration
int power(int m, int n);
Your error is caused by I being given as the parameter to
pow()
. This is becausepow()
will compile your source code after you have includedmath.h
and usedpow()
to replacei
with any other integer numbers.You will get a good result if you combine it with
It throws the same error, so
pow()
should only accept constants input.You can also declare if
math.h
is not required.This will allow you to link with the library code directly without the use of the
math.h
include files. Here is an example.You can find more information on
pow()
on the man page for Linux, i.e.man pow
.