Thursday, July 28, 2022

[SOLVED] Linker fails building library with CMake

Issue

I'm trying to build LUAGLM (from https://github.com/gottfriedleibniz/lua) to bind GLM library to give access in Lua using cmake. When i build, the building completes ok but the linker does not giving error:

ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [glm.so] Error 1
make[1]: *** [CMakeFiles/glm.dir/all] Error 2
make: *** [all] Error 2

Above these lines there is a very long list starting with:

[ 10%] Building CXX object CMakeFiles/liblua.dir/onelua.c.o
[ 20%] Linking CXX shared library liblua54.dylib
[ 20%] Built target liblua
[ 30%] Building CXX object CMakeFiles/liblua_static.dir/onelua.c.o
[ 40%] Linking CXX static library liblua54_static.a
[ 40%] Built target liblua_static
[ 50%] Building C object CMakeFiles/lua.dir/lua.c.o
[ 60%] Linking CXX executable lua
[ 60%] Built target lua
[ 70%] Building CXX object CMakeFiles/luac.dir/onelua.c.o
[ 80%] Linking CXX executable luac
[ 80%] Built target luac
[ 90%] Building CXX object CMakeFiles/glm.dir/libs/glm_binding/lglmlib.cpp.o
[100%] Linking CXX shared module glm.so
Undefined symbols for architecture x86_64:
  "glm_pushmat(lua_State*, glmMatrix const&)", referenced from:
      glm_mat_add(lua_State*) in lglmlib.cpp.o
      glm_mat_sub(lua_State*) in lglmlib.cpp.o
      glm_mat_mul(lua_State*) in lglmlib.cpp.o
      glm_mat_negate(lua_State*) in lglmlib.cpp.o
      glm_mix(lua_State*) in lglmlib.cpp.o
      glm_saturation(lua_State*) in lglmlib.cpp.o
      glm_orthonormalize(lua_State*) in lglmlib.cpp.o
      ...

I'm on MacOS Intel (i386-apple-darwin11.3.0)

Not sure where to go next, do I need to edit the CMakeList somehow?

Any help much appreciated

EDIT:

The cmake error log contains the following :

Compiling the C compiler identification source file "CMakeCCompilerId.c" failed. Compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc Build flags: Id flags:

The output was: 1 ld: library not found for -lSystem clang: error: linker command failed with exit code 1 (use -v to see invocation)

Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" failed. Compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ Build flags: Id flags:

The output was: 1 ld: library not found for -lc++ clang: error: linker command failed with exit code 1 (use -v to see invocation)

 

Solution

This is actually a bug in the CMakeList.txt because the library-list for the glm.so module does not contain the liblua.

If you apply the following patch

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8cf8a1a6..6eda2a17 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -940,7 +940,7 @@ ENDIF()
 
 ADD_LIBRARY(glm MODULE ${SRC_LIBGLM})
 TARGET_INCLUDE_DIRECTORIES(glm PRIVATE ${INCLUDE_DIRECTORIES})
-TARGET_LINK_LIBRARIES(glm PRIVATE ${LIBS})
+TARGET_LINK_LIBRARIES(glm PRIVATE ${LIBS} liblua)
 IF( LUA_BUILD_AS_DLL )
   TARGET_LINK_LIBRARIES(glm PUBLIC ${interpretor_target})
   TARGET_COMPILE_DEFINITIONS(glm PRIVATE LUA_BUILD_AS_DLL)

to the freshly cloned repository and then do (inside the cloned repository):

git submodule update --init
mkdir build
cd build
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DONE_LUA=ON ..
make

you should find the files glm.so, lua and luac in your build directory. At least that worked on my Intel MacBook.

I opened an issue in the GitHub repository for it. So hopefully it will get fixed for everybody soon.



Answered By - Frank
Answer Checked By - Gilberto Lyons (WPSolving Admin)