floooh / fips

High-level build system for distributed, multi-platform C/C++ projects.
MIT License
468 stars 82 forks source link

Missing standard math library when build in Ubuntu. #247

Closed Ron2014 closed 4 years ago

Ron2014 commented 4 years ago

Hi, master.

When I use fips to manage lua source code I get a problem when building in Ubuntu 18.04.2.

./fips build

tell me a lot of "undefined reference to 'xxx' " and I find that all the reference come from same place: math library for C standard.

...
liblua-5.3.5-lib.a(lmathlib.c.o): In function `math_sin':
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lmathlib.c:51: undefined reference to `sin'
liblua-5.3.5-lib.a(lmathlib.c.o): In function `math_cos':
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lmathlib.c:56: undefined reference to `cos'
liblua-5.3.5-lib.a(lmathlib.c.o): In function `math_tan':
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lmathlib.c:61: undefined reference to `tan'
liblua-5.3.5-lib.a(lmathlib.c.o): In function `math_asin':
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lmathlib.c:66: undefined reference to `asin'
liblua-5.3.5-lib.a(lmathlib.c.o): In function `math_acos':
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lmathlib.c:71: undefined reference to `acos'
liblua-5.3.5-lib.a(lmathlib.c.o): In function `math_atan':
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lmathlib.c:78: undefined reference to `atan2'
liblua-5.3.5-lib.a(lmathlib.c.o): In function `math_floor':
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lmathlib.c:109: undefined reference to `floor'
liblua-5.3.5-lib.a(lmathlib.c.o): In function `math_ceil':
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lmathlib.c:120: undefined reference to `ceil'
liblua-5.3.5-lib.a(lmathlib.c.o): In function `math_fmod':
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lmathlib.c:138: undefined reference to `fmod'
liblua-5.3.5-lib.a(lmathlib.c.o): In function `math_modf':
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lmathlib.c:157: undefined reference to `ceil'
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lmathlib.c:157: undefined reference to `floor'
liblua-5.3.5-lib.a(lmathlib.c.o): In function `math_sqrt':
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lmathlib.c:167: undefined reference to `sqrt'
liblua-5.3.5-lib.a(lmathlib.c.o): In function `math_log':
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lmathlib.c:183: undefined reference to `log'
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lmathlib.c:188: undefined reference to `log2'
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lmathlib.c:191: undefined reference to `log10'
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lmathlib.c:193: undefined reference to `log'
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lmathlib.c:193: undefined reference to `log'
liblua-5.3.5-lib.a(lmathlib.c.o): In function `math_exp':
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lmathlib.c:200: undefined reference to `exp'
liblua-5.3.5-lib.a(lobject.c.o): In function `numarith':
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lobject.c:110: undefined reference to `pow'
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lobject.c:111: undefined reference to `floor'
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lobject.c:115: undefined reference to `fmod'
liblua-5.3.5-lib.a(lvm.c.o): In function `luaV_tointeger':
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lvm.c:99: undefined reference to `floor'
liblua-5.3.5-lib.a(lvm.c.o): In function `luaV_execute':
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lvm.c:1001: undefined reference to `fmod'
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lvm.c:1016: undefined reference to `floor'
/home/ron/fips-workplace/fips-lua/lua-5.3.5/src/lvm.c:1026: undefined reference to `pow'
collect2: error: ld returned 1 exit status
lua-5.3.5/CMakeFiles/lua-5.3.5-interpreter.dir/build.make:95: recipe for target '/home/ron/fips-workplace/fips-deploy/fips-lua/linux-make-debug/lua-5.3.5-interpreter' failed
make[2]: *** [/home/ron/fips-workplace/fips-deploy/fips-lua/linux-make-debug/lua-5.3.5-interpreter] Error 1
CMakeFiles/Makefile2:296: recipe for target 'lua-5.3.5/CMakeFiles/lua-5.3.5-interpreter.dir/all' failed
make[1]: *** [lua-5.3.5/CMakeFiles/lua-5.3.5-interpreter.dir/all] Error 2
[ 98%] Linking C executable /home/ron/fips-workplace/fips-deploy/fips-lua/linux-make-debug/lua-5.1.4-compiler
[100%] Linking C executable /home/ron/fips-workplace/fips-deploy/fips-lua/linux-make-debug/lua-5.1.4-interpreter
liblua-5.1.4-lib.a(loslib.c.o): In function `os_tmpname':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/loslib.c:60: warning: the use of `tmpnam' is dangerous, better use `mkstemp'
liblua-5.1.4-lib.a(lmathlib.c.o): In function `math_sin':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lmathlib.c:32: undefined reference to `sin'
liblua-5.1.4-lib.a(lmathlib.c.o): In function `math_sinh':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lmathlib.c:37: undefined reference to `sinh'
liblua-5.1.4-lib.a(lmathlib.c.o): In function `math_cos':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lmathlib.c:42: undefined reference to `cos'
liblua-5.1.4-lib.a(lmathlib.c.o): In function `math_cosh':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lmathlib.c:47: undefined reference to `cosh'
liblua-5.1.4-lib.a(lmathlib.c.o): In function `math_tan':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lmathlib.c:52: undefined reference to `tan'
liblua-5.1.4-lib.a(lmathlib.c.o): In function `math_tanh':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lmathlib.c:57: undefined reference to `tanh'
liblua-5.1.4-lib.a(lmathlib.c.o): In function `math_asin':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lmathlib.c:62: undefined reference to `asin'
liblua-5.1.4-lib.a(lmathlib.c.o): In function `math_acos':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lmathlib.c:67: undefined reference to `acos'
liblua-5.1.4-lib.a(lmathlib.c.o): In function `math_atan':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lmathlib.c:72: undefined reference to `atan'
liblua-5.1.4-lib.a(lmathlib.c.o): In function `math_atan2':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lmathlib.c:77: undefined reference to `atan2'
liblua-5.1.4-lib.a(lmathlib.c.o): In function `math_ceil':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lmathlib.c:82: undefined reference to `ceil'
liblua-5.1.4-lib.a(lmathlib.c.o): In function `math_floor':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lmathlib.c:87: undefined reference to `floor'
liblua-5.1.4-lib.a(lmathlib.c.o): In function `math_fmod':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lmathlib.c:92: undefined reference to `fmod'
liblua-5.1.4-lib.a(lmathlib.c.o): In function `math_sqrt':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lmathlib.c:105: undefined reference to `sqrt'
liblua-5.1.4-lib.a(lmathlib.c.o): In function `math_pow':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lmathlib.c:110: undefined reference to `pow'
liblua-5.1.4-lib.a(lmathlib.c.o): In function `math_log':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lmathlib.c:115: undefined reference to `log'
liblua-5.1.4-liblua-5.1.4-lib.a(lvm.c.o): In function `Arith':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lvm.c:333: undefined reference to `floor'
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lvm.c:334: undefined reference to `pow'
liblua-5.1.4-lib.a(lvm.c.o): In function `luaV_execute':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lvm.c:495: undefined reference to `floor'
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lvm.c:499: undefined reference to `pow'
liblua-5.1.4-lib.a(lcode.c.o): In function `constfolding':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lcode.c:711: undefined reference to `floor'
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lcode.c:712: undefined reference to `pow'
lib.acollect2: error: ld returned 1 exit status
(lmathlib.c.o): In function `math_log10':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lmathlib.c:120: undefined reference to `log10'
liblua-5.1.4-lib.a(lmathlib.c.o): In function `math_exp':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lmathlib.c:125: undefined reference to `exp'lua-5.1.4/CMakeFiles/lua-5.1.4-compiler.dir/build.make:95: recipe for target '/home/ron/fips-workplace/fips-deploy/fips-lua/linux-make-debug/lua-5.1.4-compiler' failed
make[2]: *** [/home/ron/fips-workplace/fips-deploy/fips-lua/linux-make-debug/lua-5.1.4-compiler] Error 1
CMakeFiles/Makefile2:89: recipe for target 'lua-5.1.4/CMakeFiles/lua-5.1.4-compiler.dir/all' failed
make[1]: *** [lua-5.1.4/CMakeFiles/lua-5.1.4-compiler.dir/all] Error 2

liblua-5.1.4-lib.a(lmathlib.c.o): In function `math_random':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lmathlib.c:193: undefined reference to `floor'
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lmathlib.c:200: undefined reference to `floor'
liblua-5.1.4-lib.a(lvm.c.o): In function `Arith':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lvm.c:333: undefined reference to `floor'
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lvm.c:334: undefined reference to `pow'
liblua-5.1.4-lib.a(lvm.c.o): In function `luaV_execute':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lvm.c:495: undefined reference to `floor'
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lvm.c:499: undefined reference to `pow'
liblua-5.1.4-lib.a(lcode.c.o): In function `constfolding':
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lcode.c:711: undefined reference to `floor'
/home/ron/fips-workplace/fips-lua/lua-5.1.4/src/lcode.c:712: undefined reference to `pow'
...

so I try to add some code to solve this problem.

if(UNIX)
fips_deps(m)
endif()

which will add -lm option when linking. But, what I'm exterested in is "Why fips don't add -lm for linux project automatically", so I read the cmake-toolchain/linux.cmake and found this:

check_library_exists(rt clock_gettime "time.h" FOUND_CLOCK_GETTIME)
if (FOUND_CLOCK_GETTIME)
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lrt")
endif()

Does it meant that I should add -llibrary when I found the library function in the project? So I imitated the code and add more check in the linux.cmake:

check_library_exists(m sin "math.h" FOUND_MATH_SIN)
if (FOUND_MATH_SIN)
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lm")
endif()

But it didn't work and I'm stucked and I'm really confused about that: need I check and add linking library one by one in linux.cmake?

Ron2014 commented 4 years ago

wait, I found your fips-lua project and mgerhardy's, let me learn it first...

floooh commented 4 years ago

Hi, I usually add the 'm' lib to the project dependencies on Linux as needed, for instance:

https://github.com/floooh/sokol-samples/blob/6ac972e1f4c703132df6e515fdd6a1f1bbe827a5/libs/sokol/CMakeLists.txt#L24

(fips_libs() and fips_deps() are equivalent, so fips_deps(m) should work too, for system libraries, fips_libs() is preferred though).

PS: the automatic handling of 'rt' and 'atomic' are artefacts from the past, I don't want to add any "magic dependencies" like this in the future.