Linker fails building library with CMake - cmake

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)

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_static)
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.
Using liblua_static in the CMakeList.txt for the library glm makes sure, that the resulting glm.so does not need the liblua dynamic library but contains the required code statically. That avoids problems when loading glm.so and the liblua library not being in the library path.
I opened an issue in the GitHub repository for it. So hopefully it will get fixed for everybody soon.

Usually it'll be something like -lYourLibrary not found, if -lSystem for example is not found, try adding it to your CMakeLists.
Are you still having the issue?

Related

make: have built target; make install : *** No rule to make target 'install'

In Ubuntu20.04.
I want to insatll easy3d, but I can not make install it successfully.
make
[ 83%] Building CXX object CMakeFiles/easy3d.dir/surface_mesh_io_obj.cpp.o
[ 87%] Building CXX object CMakeFiles/easy3d.dir/surface_mesh_io_off.cpp.o
[ 91%] Building CXX object CMakeFiles/easy3d.dir/surface_mesh_io_ply.cpp.o
[ 95%] Building CXX object CMakeFiles/easy3d.dir/transform.cpp.o
[100%] Linking CXX static library libeasy3d.a
[100%] Built target easy3d
And the build have files:
CMakeCache.txt CMakeFiles Makefile cmake_install.cmake libeasy3d.a
Then I excute the command make install, I got
make: *** No rule to make target 'install'. Stop.
How could I fix it
I made a big mistake that is nothing to install.

How to statically link PROJ to pybind11

I'm interested in statically linking PROJ to a library created with pybind11.
but, I get error by cmake.
Why am I getting this error and how can I fix it?
My CMakeLists.txt is this.
cmake_minimum_required(VERSION 3.4)
project(myearth LANGUAGES CXX)
add_subdirectory(pybind11)
pybind11_add_module(myearth MyEarth.cpp)
include_directories(${CMAKE_SOURCE_DIR}/PROJ/include)
add_library(proj4 STATIC IMPORTED)
set_target_properties(proj4 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/PROJ/lib/libproj.a)
target_link_libraries(myearth PRIVATE proj4)
CMake Error.
I think fPIC option is for shared library. so, libproj.a is not necessary this option because this is static library.
(venv) ~/Projects/LinkLibTest/MySample/build$ make
[ 50%] Building CXX object CMakeFiles/myearth.dir/MyEarth.cpp.o
[100%] Linking CXX shared module myearth.cpython-36m-x86_64-linux-gnu.so
/usr/bin/ld: ../PROJ/lib/libproj.a(proj_4D_api.o): relocation R_X86_64_PC32 against symbol `pj_errno' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/myearth.dir/build.make:98: myearth.cpython-36m-x86_64-linux-gnu.so] Error 1
make[1]: *** [CMakeFiles/Makefile2:100: CMakeFiles/myearth.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
(venv) ~/Projects/LinkLibTest/MySample/build$
I created libproj.a by the following command.
$ ./configure --prefix=/output --disable-shared
$ sudo make
$ sudo make install
Linux Mint 20.3 64bit
CMake v3.22.2
PROJ v5.2.0 (https://proj.org/)
Python v3.6.8
Thank you.
Shared libraries must have position independent code, but your version of PROJ was built without it; you will have to rebuild PROJ with PIC.
$ ./configure --prefix=/output --with-pic

Difficulty Compiling c2ffi on FreeBSD 12 and Debian 10

I am having problems installing c2ffi (https://github.com/rpav/c2ffi) on both FreeBSD 12 and Debian 10. I need c2ffi in order to use some Common Lisp bindings that rely on c2ffi.
On FreeBSD 12, I have both Clang 6.0 (default) and 10.0 installed; c2ffi requires LLVM 10.0. Because cc and c++ refer to Clang 6.0 in my FreeBSD installation, I set PATH to /usr/local/llvm10/bin:$PATH, and I also set aliases to refer to clang and clang++. I have cmake version 3.17.3 installed. However, when I make it to the make phase of installing c2ffi, I run into the following error:
Scanning dependencies of target c2ffi
[ 7%] Building CXX object CMakeFiles/c2ffi.dir/src/AST.cpp.o
[ 15%] Building CXX object CMakeFiles/c2ffi.dir/src/Decl.cpp.o
[ 23%] Building CXX object CMakeFiles/c2ffi.dir/src/Expr.cpp.o
[ 30%] Building CXX object CMakeFiles/c2ffi.dir/src/OutputDriver.cpp.o
[ 38%] Building CXX object CMakeFiles/c2ffi.dir/src/Template.cpp.o
[ 46%] Building CXX object CMakeFiles/c2ffi.dir/src/Type.cpp.o
[ 53%] Building CXX object CMakeFiles/c2ffi.dir/src/c2ffi.cpp.o
[ 61%] Building CXX object CMakeFiles/c2ffi.dir/src/drivers/JSON.cpp.o
/home/michael/c2ffi/src/drivers/JSON.cpp:36:26: warning: passing an object that
undergoes default argument promotion to 'va_start' has undefined behavior
[-Wvarargs]
va_start(ap, close);
^
/home/michael/c2ffi/src/drivers/JSON.cpp:32:61: note: parameter of type 'bool'
is declared here
void write_object(const char *type, bool open, bool close, ...) {
^
1 warning generated.
[ 69%] Building CXX object CMakeFiles/c2ffi.dir/src/drivers/Null.cpp.o
[ 76%] Building CXX object CMakeFiles/c2ffi.dir/src/drivers/Sexp.cpp.o
[ 84%] Building CXX object CMakeFiles/c2ffi.dir/src/init.cpp.o
[ 92%] Building CXX object CMakeFiles/c2ffi.dir/src/options.cpp.o
[100%] Linking CXX executable bin/c2ffi
/usr/bin/ld: error: unable to find library -lclang-cpp
c++: error: linker command failed with exit code 1 (use -v to see invocation)
*** Error code 1
Stop.
make[2]: stopped in /usr/home/michael/c2ffi/build
*** Error code 1
Stop.
make[1]: stopped in /usr/home/michael/c2ffi/build
*** Error code 1
Stop.
make: stopped in /usr/home/michael/c2ffi/build
The file libclang-cpp.so is present in my /usr/local/llvm10/lib directory, but cmake does not detect it. I tried various other approaches, including setting the LD_LIBRARY_PATH and CMAKE_LIBRARY_PATH environment variables, but to no avail; I received the same error message as above.
I gave up installing c2ffi on FreeBSD and tried installing it on a fresh install of Debian 10 with LLVM 10.0 and cmake 3.18.1. However, when I ran make on Debian, I ended up with the following error message:
[ 7%] Building CXX object CMakeFiles/c2ffi.dir/src/AST.cpp.o
/home/michael/c2ffi/src/AST.cpp:24:10: fatal error: 'clang/AST/ASTConsumer.h' file not found
#include <clang/AST/ASTConsumer.h>
^~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/c2ffi.dir/build.make:82: CMakeFiles/c2ffi.dir/src/AST.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:123: CMakeFiles/c2ffi.dir/all] Error 2
make: *** [Makefile:149: all] Error 2
I am wondering how I can build c2ffi on my FreeBSD and Debian systems?
On my FreeBSD installation, in the CMakeLists.txt I replaced the line
target_link_libraries(c2ffi PUBLIC clang-cpp LLVM)
with
target_link_libraries(c2ffi PUBLIC /usr/local/llvm10/lib/libclang-cpp.so LLVM)
On my Debian installation, it turned out that I did not have one of the libclang development packages installed. After installing that package I was able to run make without any problems.
The project's CMakeLists.txt is broken, so bug upstream. Actualy, there are numerous pull requests already filed:
https://github.com/rpav/c2ffi/pull/70
https://github.com/rpav/c2ffi/pull/68/
https://github.com/rpav/c2ffi/pull/65/
You can try these, or combine them into a working solution.

cmake error with grpc in tensorflow

When trying to build tensorflow using cmake, I got the following error. I'm not sure whether it's an issue related to tensorflow or grpc. Any idea on what's going on?
This is on Mac OS Sierra.
steps to reproduce:
# clone tensorflow repo
# in tensorflow directory
cd tensorflow/contrib/cmake
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DPYTHON_EXECUTABLE=/usr/local/bin/python3
make tf_tutorials_example_trainer
Errors:
# ...
Scanning dependencies of target tf_tutorials_example_trainer
[100%] Building CXX object CMakeFiles/tf_tutorials_example_trainer.dir/Users/kevenwang/VirtualBoxShared/another_tf/tensorflow/cc/tutorials/example_trainer.cc.o
[100%] Linking CXX executable tf_tutorials_example_trainer
Undefined symbols for architecture x86_64:
"_ares_cancel", referenced from:
on_readable_cb(grpc_exec_ctx*, void*, grpc_error*) in libgrpc_unsecure.a(grpc_ares_ev_driver_posix.cc.o)
on_writable_cb(grpc_exec_ctx*, void*, grpc_error*) in libgrpc_unsecure.a(grpc_ares_ev_driver_posix.cc.o)
"_ares_destroy", referenced from:
grpc_ares_ev_driver_unref(grpc_ares_ev_driver*) in libgrpc_unsecure.a(grpc_ares_ev_driver_posix.cc.o)
...
_grpc_ares_ev_driver_create in libgrpc_unsecure.a(grpc_ares_ev_driver_posix.cc.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [tf_tutorials_example_trainer] Error 1
make[2]: *** [CMakeFiles/tf_tutorials_example_trainer.dir/all] Error 2
make[1]: *** [CMakeFiles/tf_tutorials_example_trainer.dir/rule] Error 2
Maybe you install the grpc lib by Homebrew.
In that case , you need to link the c-ares lib.
e.g.
TARGET_LINK_LIBRARIES(demo "/usr/local/lib/libcares.a" ...)

Undefined references while linking test generated by CMake

My CMakeList.txt has the following problematic section:
add_executable(esh_test tests/libtest.c esh.c tests/esh_test.cpp)
add_dependencies(esh_test esh)
target_link_libraries(esh_test
${CMAKE_SOURCE_DIR}/esh-grammar.o
${CMAKE_SOURCE_DIR}/libesh.a
l dl readline curses libgtest gtest_main)
Problematic because it won't link properly:
$> cmake ./; make
-- Configuring done
-- Generating done
-- Build files have been written to: src/
[ 0%] Built target lexpar
[ 0%] Built target libesh
[ 14%] Built target esh
[ 71%] Built target gtest
[ 78%] Linking CXX executable esh_test
CMakeFiles/esh_test.dir/esh_test.cpp.o: In function `testcase_test_test_Test::TestBody()':
esh_test.cpp:(.text+0xe): undefined reference to `mkredir()'
esh_test.cpp:(.text+0x1c): undefined reference to `esh_usage(char*)'
esh_test.cpp:(.text+0x28): undefined reference to `redir_restore_streams(redir*)'
esh_test.cpp:(.text+0x3a): undefined reference to `fd_grep_regex(int, char*)'
esh_test.cpp:(.text+0x107): undefined reference to `redir_cleanup(redir*)'
collect2: error: ld returned 1 exit status
The link.txt for this module is:
/usr/bin/c++ -std=c++11 CMakeFiles/esh_test.dir/esh_test.cpp.o CMakeFiles/esh_test.dir/libtest.c.o CMakeFiles/esh_test.dir/esh.c.o -o esh_test -rdynamic ../esh-grammar.o ../libesh.a -ll -ldl -lreadline -lcurses gtest/src/gtest-build/libgtest.a -lgtest_main -lpthread
Which seems like it would link in the missing functions, declared in libtest.h and defined in libtest.c.
This is my first CMake project, so I'm a little lost. What's going on here?