Skip to content

Commit d4aab3c

Browse files
authored
add -lm as needed (#55)
* hack add -lm to linux build * proper lm detection * better -lm detection
1 parent 46012f8 commit d4aab3c

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

CMakeLists.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
22

33
project(gau2grid
4-
VERSION 2.0.1
4+
VERSION 2.0.3
55
LANGUAGES C)
66
set(gau2grid_AUTHORS "Daniel G. A. Smith")
77
set(gau2grid_DESCRIPTION "Fast computation of a gaussian and its derivative on a grid")
@@ -88,6 +88,9 @@ endif()
8888
set_target_properties(gg PROPERTIES POSITION_INDEPENDENT_CODE ${BUILD_FPIC}
8989
SOVERSION 1) # bump whenever interface has changes or removals
9090

91+
find_package(StandardMathLibraryC)
92+
target_link_libraries(gg PRIVATE ${STANDARD_MATH_LIBRARY})
93+
9194
if(${BUILD_SHARED_LIBS})
9295
target_link_libraries(gg PRIVATE ${LIBC_INTERJECT})
9396
endif()

cmake/FindStandardMathLibraryC.cmake

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# * downloaded Nov 2016 from https://android.googlesource.com/platform/external/eigen/+/master/cmake/FindStandardMathLibrary.cmake
2+
# * changed CXX to C
3+
# * note that full path to libm *not* detected
4+
5+
# - Try to find how to link to the standard math library, if anything at all is needed to do.
6+
# On most platforms this is automatic, but for example it's not automatic on QNX.
7+
#
8+
# Once done this will define
9+
#
10+
# STANDARD_MATH_LIBRARY_FOUND - we found how to successfully link to the standard math library
11+
# STANDARD_MATH_LIBRARY - the name of the standard library that one has to link to.
12+
# -- this will be left empty if it's automatic (most platforms).
13+
# -- this will be set to "m" on platforms where one must explicitly
14+
# pass the "-lm" linker flag.
15+
#
16+
# Copyright (c) 2010 Benoit Jacob <[email protected]>
17+
# Redistribution and use is allowed according to the terms of the 2-clause BSD license.
18+
include(CheckCSourceCompiles)
19+
# a little test program for c++ math functions.
20+
# notice the std:: is required on some platforms such as QNX
21+
set(find_standard_math_library_test_program
22+
"#include<math.h>
23+
int main() { sin(0.0); log(0.0f); }")
24+
# C++ test program
25+
# "#include<cmath>
26+
# int main() { std::sin(0.0); std::log(0.0f); }")
27+
# first try compiling/linking the test program without any linker flags
28+
set(CMAKE_REQUIRED_FLAGS "")
29+
set(CMAKE_REQUIRED_LIBRARIES "")
30+
CHECK_C_SOURCE_COMPILES(
31+
"${find_standard_math_library_test_program}"
32+
standard_math_library_linked_to_automatically
33+
)
34+
if(standard_math_library_linked_to_automatically)
35+
# the test program linked successfully without any linker flag.
36+
set(STANDARD_MATH_LIBRARY "")
37+
set(STANDARD_MATH_LIBRARY_FOUND TRUE)
38+
else()
39+
# the test program did not link successfully without any linker flag.
40+
# This is a very uncommon case that so far we only saw on QNX. The next try is the
41+
# standard name 'm' for the standard math library.
42+
set(CMAKE_REQUIRED_LIBRARIES "m")
43+
CHECK_C_SOURCE_COMPILES(
44+
"${find_standard_math_library_test_program}"
45+
standard_math_library_linked_to_as_m)
46+
if(standard_math_library_linked_to_as_m)
47+
# the test program linked successfully when linking to the 'm' library
48+
set(STANDARD_MATH_LIBRARY "m")
49+
set(STANDARD_MATH_LIBRARY_FOUND TRUE)
50+
else()
51+
# the test program still doesn't link successfully
52+
set(STANDARD_MATH_LIBRARY_FOUND FALSE)
53+
endif()
54+
endif()

0 commit comments

Comments
 (0)