# problem with math.h



## First_Law_of_Unix (Oct 12, 2022)

[_Mod: Split off from a 10(!) year old thread; Thread problem-with-math-h.33320_]

I'm getting the same issues as to op when compiling a `C/C++` project:


```
[100%] Built target darknet_l
[100%] Linking CXX executable darknet
ld: error: undefined symbol: expf
>>> referenced by activations.h:36 (/home/user/darknet/src/activations.h:36)
>>>               CMakeFiles/darknet.dir/src/activations.c.o:(activate)
>>> referenced by activations.h:44 (/home/user/darknet/src/activations.h:44)
>>>               CMakeFiles/darknet.dir/src/activations.c.o:(activate)
>>> referenced by activations.h:37 (/home/user/darknet/src/activations.h:37)
>>>               CMakeFiles/darknet.dir/src/activations.c.o:(activate)
>>> referenced 10 more times

ld: error: undefined symbol: log1pf
>>> referenced by activations.h:56 (/home/user/darknet/src/activations.h:56)
>>>               CMakeFiles/darknet.dir/src/activations.c.o:(activate)
>>> referenced by activations.h:56 (/home/user/darknet/src/activations.h:56)
>>>               CMakeFiles/darknet.dir/src/activations.c.o:(gradient)

ld: error: undefined symbol: tanhf
>>> referenced by activations.h:64 (/home/user/darknet/src/activations.h:64)
>>>               CMakeFiles/darknet.dir/src/activations.c.o:(activate)
>>> referenced by activations.h:79 (/home/user/darknet/src/activations.h:79)
>>>               CMakeFiles/darknet.dir/src/activations.c.o:(gradient)

ld: error: undefined symbol: floorf
>>> referenced by activations.h:24 (/home/user/darknet/src/activations.h:24)
>>>               CMakeFiles/darknet.dir/src/activations.c.o:(activate)
>>> referenced by activations.h:111 (/home/user/darknet/src/activations.h:111)
>>>               CMakeFiles/darknet.dir/src/activations.c.o:(gradient)
>>> referenced by image.c:0 (/home/user/darknet/src/image.c:0)
>>>               CMakeFiles/darknet.dir/src/image.c.o:(place_image)
>>> referenced 7 more times

ld: error: undefined symbol: floor
>>> referenced by activations.h:25 (/home/user/darknet/src/activations.h:25)
>>>               CMakeFiles/darknet.dir/src/activations.c.o:(activate)
>>> referenced by activations.h:26 (/home/user/darknet/src/activations.h:26)
>>>               CMakeFiles/darknet.dir/src/activations.c.o:(activate)
>>> referenced by image.c:21 (/home/user/darknet/src/image.c:21)
>>>               CMakeFiles/darknet.dir/src/image.c.o:(get_color)
>>> referenced 6 more times

ld: error: undefined symbol: expm1f
>>> referenced by activations.h:78 (/home/user/darknet/src/activations.h:78)
>>>               CMakeFiles/darknet.dir/src/activations.c.o:(gradient)

ld: error: undefined symbol: pow
>>> referenced by batchnorm_layer.c:118 (/home/user/darknet/src/batchnorm_layer.c:118)
>>>               CMakeFiles/darknet.dir/src/batchnorm_layer.c.o:(backward_batchnorm_layer)
>>> referenced by batchnorm_layer.c:118 (/home/user/darknet/src/batchnorm_layer.c:118)
>>>               CMakeFiles/darknet.dir/src/batchnorm_layer.c.o:(backward_batchnorm_layer)
>>> referenced by batchnorm_layer.c:118 (/home/user/darknet/src/batchnorm_layer.c:118)
>>>               CMakeFiles/darknet.dir/src/batchnorm_layer.c.o:(backward_batchnorm_layer)
>>> referenced 85 more times

ld: error: undefined symbol: log
>>> referenced by blas.c:279 (/home/user/darknet/src/blas.c:279)
>>>               CMakeFiles/darknet.dir/src/blas.c.o:(softmax_x_ent_cpu)
>>> referenced by blas.c:290 (/home/user/darknet/src/blas.c:290)
>>>               CMakeFiles/darknet.dir/src/blas.c.o:(logistic_x_ent_cpu)
>>> referenced by blas.c:290 (/home/user/darknet/src/blas.c:290)
>>>               CMakeFiles/darknet.dir/src/blas.c.o:(logistic_x_ent_cpu)
>>> referenced 26 more times

ld: error: undefined symbol: exp
>>> referenced by blas.c:322 (/home/user/darknet/src/blas.c:322)
>>>               CMakeFiles/darknet.dir/src/blas.c.o:(softmax)
>>> referenced by blas.c:322 (/home/user/darknet/src/blas.c:322)
>>>               CMakeFiles/darknet.dir/src/blas.c.o:(softmax_cpu)
>>> referenced by network.c:110 (/home/user/darknet/src/network.c:110)
>>>               CMakeFiles/darknet.dir/src/network.c.o:(get_current_rate)
>>> referenced 40 more times

ld: error: undefined symbol: atan
>>> referenced by box.c:324 (/home/user/darknet/src/box.c:324)
>>>               CMakeFiles/darknet.dir/src/box.c.o:(box_ciou)
>>> referenced by box.c:324 (/home/user/darknet/src/box.c:324)
>>>               CMakeFiles/darknet.dir/src/box.c.o:(box_ciou)
>>> referenced by box.c:508 (/home/user/darknet/src/box.c:508)
>>>               CMakeFiles/darknet.dir/src/box.c.o:(dx_box_iou)
>>> referenced 5 more times

ld: error: undefined symbol: log2
>>> referenced by box.c:807 (/home/user/darknet/src/box.c:807)
>>>               CMakeFiles/darknet.dir/src/box.c.o:(encode_box)
>>> referenced by box.c:807 (/home/user/darknet/src/box.c:807)
>>>               CMakeFiles/darknet.dir/src/box.c.o:(encode_box)
>>> referenced by box.c:1709 (/home/user/darknet/src/box.c:1709)
>>>               CMakeFiles/darknet.dir/src/box.c.o:(encode_box_y4)
>>> referenced 1 more times

ld: error: undefined symbol: exp2
>>> referenced by box.c:817 (/home/user/darknet/src/box.c:817)
>>>               CMakeFiles/darknet.dir/src/box.c.o:(decode_box)
>>> referenced by box.c:817 (/home/user/darknet/src/box.c:817)
>>>               CMakeFiles/darknet.dir/src/box.c.o:(decode_box)
>>> referenced by box.c:1719 (/home/user/darknet/src/box.c:1719)
>>>               CMakeFiles/darknet.dir/src/box.c.o:(decode_box_y4)
>>> referenced 5 more times

ld: error: undefined symbol: ceil
>>> referenced by image.c:22 (/home/user/darknet/src/image.c:22)
>>>               CMakeFiles/darknet.dir/src/image.c.o:(get_color)
>>> referenced by image.c:22 (/home/user/darknet/src/image.c:22)
>>>               CMakeFiles/darknet.dir/src/image.c.o:(mask_to_rgb)
>>> referenced by image.c:22 (/home/user/darknet/src/image.c:22)
>>>               CMakeFiles/darknet.dir/src/image.c.o:(draw_detections_v3)
>>> referenced 3 more times

ld: error: undefined symbol: roundf
>>> referenced by image.c:392 (/home/user/darknet/src/image.c:392)
>>>               CMakeFiles/darknet.dir/src/image.c.o:(draw_detections_v3)
>>> referenced by image.c:393 (/home/user/darknet/src/image.c:393)
>>>               CMakeFiles/darknet.dir/src/image.c.o:(draw_detections_v3)
>>> referenced by image.c:394 (/home/user/darknet/src/image.c:394)
>>>               CMakeFiles/darknet.dir/src/image.c.o:(draw_detections_v3)
>>> referenced 5 more times

ld: error: undefined symbol: cos
>>> referenced by image.c:0 (/home/user/darknet/src/image.c:0)
>>>               CMakeFiles/darknet.dir/src/image.c.o:(rotate_crop_image)
>>> referenced by image.c:0 (/home/user/darknet/src/image.c:0)
>>>               CMakeFiles/darknet.dir/src/image.c.o:(rotate_image)
>>> referenced by network.c:122 (/home/user/darknet/src/network.c:122)
>>>               CMakeFiles/darknet.dir/src/network.c.o:(get_current_rate)
>>> referenced 1 more times

ld: error: undefined symbol: sin
>>> referenced by image.c:0 (/home/user/darknet/src/image.c:0)
>>>               CMakeFiles/darknet.dir/src/image.c.o:(rotate_crop_image)
>>> referenced by image.c:0 (/home/user/darknet/src/image.c:0)
>>>               CMakeFiles/darknet.dir/src/image.c.o:(rotate_image)
>>> referenced by lsd.c:392 (/home/user/darknet/examples/lsd.c:392)
>>>               CMakeFiles/darknet.dir/examples/lsd.c.o:(slerp)
>>> referenced 6 more times

ld: error: undefined symbol: acos
>>> referenced by lsd.c:391 (/home/user/darknet/examples/lsd.c:391)
>>>               CMakeFiles/darknet.dir/examples/lsd.c.o:(slerp)
>>> referenced by lsd.c:391 (/home/user/darknet/examples/lsd.c:391)
>>>               CMakeFiles/darknet.dir/examples/lsd.c.o:(inter_dcgan)

ld: error: undefined symbol: nan
>>> referenced by utils.c:615 (/home/user/darknet/src/utils.c:615)
>>>               CMakeFiles/darknet.dir/src/utils.c.o:(parse_fields)
>>> referenced by utils.c:614 (/home/user/darknet/src/utils.c:614)
>>>               CMakeFiles/darknet.dir/src/utils.c.o:(parse_fields)

ld: error: undefined symbol: operator new(unsigned long)
>>> referenced by image_opencv.cpp:0 (/home/user/darknet/src/image_opencv.cpp:0)
>>>               CMakeFiles/darknet.dir/src/image_opencv.cpp.o:(open_video_stream)
>>> referenced by new:235 (/usr/include/c++/v1/new:235)
>>>               CMakeFiles/darknet.dir/src/image_opencv.cpp.o:(open_video_stream)
>>> referenced by new:235 (/usr/include/c++/v1/new:235)
>>>               CMakeFiles/darknet.dir/src/image_opencv.cpp.o:(show_image_frame_cv)
>>> referenced 13 more times

ld: error: undefined symbol: operator delete(void*)
>>> referenced by new:245 (/usr/include/c++/v1/new:245)
>>>               CMakeFiles/darknet.dir/src/image_opencv.cpp.o:(open_video_stream)
>>> referenced by new:245 (/usr/include/c++/v1/new:245)
>>>               CMakeFiles/darknet.dir/src/image_opencv.cpp.o:(open_video_stream)
>>> referenced by new:0 (/usr/include/c++/v1/new:0)
>>>               CMakeFiles/darknet.dir/src/image_opencv.cpp.o:(open_video_stream)
>>> referenced 23 more times

ld: error: too many errors emitted, stopping now (use -error-limit=0 to see all errors)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
--- darknet ---
*** [darknet] Error code 1

make[2]: stopped in /usr/home/user/darknet/build
1 error
```

Many errors related to <math.h>

Thanks.


----------



## gpw928 (Oct 12, 2022)

plichel said:


> #include <math.h>
> 
> int main(void)
> {
> ...


It always helps to know the FreeBSD release and compiler version.

On FreeBSD 13.1-RELEASE-p2, that code compiles for me without errors using both `cc` and `gcc11`:
	
	



```
[strand.277] $ cc --version
FreeBSD clang version 13.0.0 (git@github.com:llvm/llvm-project.git llvmorg-13.0.0-0-gd7b669b3a303)
Target: x86_64-unknown-freebsd13.1
Thread model: posix
InstalledDir: /usr/bin

[strand.278] $ gcc11 --version
gcc11 (FreeBSD Ports Collection) 11.3.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[strand.279] sum /usr/include/math.h
60456 15 /usr/include/math.h

[strand.280] $ grep include /usr/include/math.h
#include <sys/cdefs.h>
#include <sys/_types.h>
#include <machine/_limits.h>

[strand.285] $ cc -E t.c >t1.c
[strand.286] $ sum t1.c
40608 12 t1.c
```
Your sum(1)s should be the same if you have the same version of FreeBSD and cc.


----------



## First_Law_of_Unix (Oct 12, 2022)

Thank for the reply. It turns out that I needed to add some compiler flags which did the trick:

`-lstdc++` and `-lm`


----------



## gpw928 (Oct 12, 2022)

First_Law_of_Unix said:


> Thank for the reply. It turns out that I needed to add some compiler flags which did the trick:
> 
> `-lstdc++` and `-lm`


The OP had a problem with the compiler.  Technically, your problem was not with the compiler.  It was with the loader, ld.lld(1) (undefined symbols when linking the object files to create an executable because of missing libraries).


----------



## Alain De Vos (Oct 12, 2022)

For the record, On Gentoo-Linux it compiles fine just "cc".

```
EXP(3)                                            Linux Programmer's Manual                                           EXP(3)
NAME
       exp, expf, expl - base-e exponential function
SYNOPSIS
       #include <math.h>
       double exp(double x);
       float expf(float x);
       long double expl(long double x);
       Link with -lm.
```

FreeBSD asks explicit to link math : "cc -lm".


----------



## Paul Floyd (Oct 12, 2022)

First_Law_of_Unix said:


> Thank for the reply. It turns out that I needed to add some compiler flags which did the trick:
> 
> `-lstdc++` and `-lm`



You are using clang and libstdc++?

Normally you would use clang++ as the linker driver and that just does the right thing (links with libc++).

You may need to use -rpath so that your exe knows where to load libstdc++ (or else use LD_LIBRARY_PATH, but that is a really bad idea).


----------



## Alain De Vos (Oct 12, 2022)

FreeBSD,

```
man exp
EXP(3)                 FreeBSD Library Functions Manual                 EXP(3)

NAME
     exp, expf, expl, exp2, exp2f, exp2l, expm1, expm1f, expm1l, pow, powf,
     powl – exponential and power functions

LIBRARY
     Math Library (libm, -lm)
```


----------



## First_Law_of_Unix (Oct 13, 2022)

Paul Floyd said:


> You are using clang and libstdc++?
> 
> Normally you would use clang++ as the linker driver and that just does the right thing (links with libc++).
> 
> You may need to use -rpath so that your exe knows where to load libstdc++ (or else use LD_LIBRARY_PATH, but that is a really bad idea).



I am using Cmake and set it to use clang on the CmakeList.txt file, not too sure how it is using clang exactly. The project contains both C and C++ files:








						GitHub - sowson/darknet: Darknet on OpenCL Convolutional Neural Networks on OpenCL on Intel & NVidia & AMD & Mali GPUs for macOS & GNU/Linux & Windows & FreeBSD
					

Darknet on OpenCL Convolutional Neural Networks on OpenCL on Intel & NVidia & AMD & Mali GPUs for macOS & GNU/Linux & Windows & FreeBSD - GitHub - sowson/darknet: Darknet on...




					github.com


----------



## Paul Floyd (Oct 13, 2022)

I'm not much of an expert on cmake, but I'd expect that simply setting CC=clang and CXX=clang++ should be enough.

The project might have hard coded dependencies on gcc/g++/libstdc++ though.


----------

