How to solve cmake linking error `lld: error: undefined symbol: llvm::ConstantInt::get(llvm::IntegerType*, unsigned long, bool) ...` - cmake

I am trying to compile from source ypsilon, but couldn't get it installed on Ubunut 22.04 LTS after multiple trial-and-errors.
I followed the official instructions linked above but process fails at cmake --build ., complaining that it couldn't find llvm. Then, I used cmake -DLLVM_DIR=/usr/lib/llvm-14/cmake .... Now, the compilation is OK, but there are many link errors even after trying out -DCMAKE_EXE_LINKER_FLAGS_RELEASE=:
$ cmake -DLLVM_DIR=/usr/lib/llvm-14/cmake -DCMAKE_EXE_LINKER_FLAGS_RELEASE=-L/usr/lib/llvm-14/lib -DCMAKE_BUILD_TYPE=Release .. && cmake --build . |& tee ~/a.txt
-- Build type: Release
-- Found LLVM 14.0.0
-- Using LLVMConfig.cmake in: /usr/lib/llvm-14/cmake
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/tmp/ypsilon/build
[ 2%] Building CXX object CMakeFiles/ypsilon.dir/src/arith.cpp.o
...
[100%] Linking CXX executable ypsilon
ld.lld: error: undefined symbol: llvm::Type::getInt32Ty(llvm::LLVMContext&)
>>> referenced by ld-temp.o
>>> lto.tmp:(digamma_t::reg_cache_t<8>::copy(llvm::Value*))
>>> referenced by ld-temp.o
>>> lto.tmp:(digamma_t::reg_cache_t<24>::copy(llvm::Value*))
>>> referenced by ld-temp.o
>>> lto.tmp:(digamma_t::reg_cache_t<16>::copy(llvm::Value*))
>>> referenced 96 more times
ld.lld: error: undefined symbol: llvm::ConstantInt::get(llvm::IntegerType*, unsigned long, bool)
...
ld.lld: 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)
gmake[2]: *** [CMakeFiles/ypsilon.dir/build.make:817: ypsilon] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:164: CMakeFiles/ypsilon.dir/all] Error 2
gmake: *** [Makefile:146: all] Error 2
I am not familiar with cmake or its linking flags on the command line.
Does any one know how to fix the cmake linking issue in the above commands?
The steps I tried are as follows:
sudo apt install clang lld
git clone https://github.com/fujita-y/ypsilon.git
cd ypsilon/ ; mkdir -p build ; cd build
cmake -DLLVM_DIR=/usr/lib/llvm-14/cmake -DCMAKE_EXE_LINKER_FLAGS_RELEASE=-L/usr/lib/llvm-14/lib -DCMAKE_BUILD_TYPE=Release .. && cmake --build .

Related

x265 - Undefined symbols for architecture arm64 (Apple Silicon)

any ideas for this?
I try to build SDRangel on my M1 Mac but at 77% I get this error ...
-- GIT_EXECUTABLE /opt/local/bin/git
-- GIT LIVE REPO VERSION RETRIEVED
-- X265 RELEASE VERSION 3.5+68-40e37bce9
-- Detected CXX compiler using -O3 optimization level
-- Configuring done
-- Generating done
-- Build files have been written to: /opt/build/sdrangel/build/external/x265/src/x265-build
[ 27%] Performing build step for 'x265'
Consolidate compiler generated dependencies of target common
[ 45%] Built target common
Consolidate compiler generated dependencies of target encoder
[ 65%] Built target encoder
[ 76%] Built target x265-static
[ 77%] Linking CXX shared library libx265.dylib
Undefined symbols for architecture arm64:
"x265_entropyStateBits", referenced from:
_x265_costCoeffNxN_neon in pixel-util.S.o
(maybe you meant: _x265_entropyStateBits)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[5]: *** [libx265.206.dylib] Error 1
make[4]: *** [CMakeFiles/x265-shared.dir/all] Error 2
make[3]: *** [all] Error 2
make[2]: *** [external/x265/src/x265-stamp/x265-build] Error 2
make[1]: *** [external/CMakeFiles/x265.dir/all] Error 2
make: *** [all] Error 2
I am stuck. Don't know what to try to solve this issue.
I did not find anything anywhere ...
Working on it for most of 2 days, I did find this site that presents a working method of building libx265 stuff on Apple Silicon and works for me on macOS 13.0.1. https://www.osxexperts.net/ Problem is, it only builds the libraries for building it into ffmpeg, no executable binary.
When I tried to get the executable also, by removing -DENABLE_CLI=OFF from the last cmake command, the error comes back. Put it back in, everything completes without error.
Here is the full process that works for me (no executable). It builds a multilibrary that can encode 8, 10, or 12-bit video:
cd x265/source
#### 12 bit
cmake -DCMAKE_INSTALL_PREFIX:PATH=${TARGET} -DHIGH_BIT_DEPTH=ON -DMAIN12=ON -DENABLE_SHARED=NO -DEXPORT_C_API=NO -DENABLE_CLI=OFF .
make
mv libx265.a libx265_main12.a
make clean-generated
rm CMakeCache.txt
#### 10bit
cmake -DCMAKE_INSTALL_PREFIX:PATH=${TARGET} -DHIGH_BIT_DEPTH=ON -DMAIN10=ON -DENABLE_SHARED=NO -DEXPORT_C_API=NO -DENABLE_CLI=OFF .
make clean
make
mv libx265.a libx265_main10.a
make clean-generated && rm CMakeCache.txt
#### 8 bit and all together
# removed -DENABLE_CLI=OFF from last one so we get the x265 binary
cmake -DCMAKE_INSTALL_PREFIX:PATH=${TARGET} -DEXTRA_LIB="x265_main10.a;x265_main12.a" -DEXTRA_LINK_FLAGS=-L. -DLINKED_10BIT=ON -DLINKED_12BIT=ON -DENABLE_SHARED=OFF -DENABLE_CLI=OFF .
make clean
make
mv libx265.a libx265_main.a
# this is Apple's libtool
libtool -static -o libx265.a libx265_main.a libx265_main10.a libx265_main12.a
make install
When I remove the last -DENABLE_CLI=OFF, here's the error:
[100%] Linking CXX executable x265
Undefined symbols for architecture arm64:
"x265_entropyStateBits", referenced from:
_x265_costCoeffNxN_neon in libx265.a(pixel-util.S.o)
_x265_10bit_costCoeffNxN_neon in libx265_main10.a(pixel-util.S.o)
_x265_12bit_costCoeffNxN_neon in libx265_main12.a(pixel-util.S.o)
(maybe you meant: _x265_entropyStateBits)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [x265] Error 1
make[1]: *** [CMakeFiles/cli.dir/all] Error 2
make: *** [all] Error 2
JimsMBPro:source jim$
EDIT: #Peter Betz, see the issue report. Until the developers correct this error, it will compile if you edit x265/source/common/aarch64/pixel-util.S, line 2410. Add an underscore as follows. Change:
movrel x1, x265_entropyStateBits
to
movrel x1, _x265_entropyStateBits

Cannot find -l<nameOfTheLibrary>, even after setting LD_LIBRARY_PATH

I'm trying to build OpenMVG, and can't resolve some dependency errors. I'm in an anaconda environment and using conda installed versions of g++ and some of the libs (I do not have root priveleges).
$ cmake --build . --target install
... (lots of output from seemingly successful steps) ...
[ 77%] Built target openMVG_sample_main_features_repeatability_dataset
[ 77%] Linking CXX executable ../../Linux-x86_64-RELEASE/openMVG_sample_features_affine
/home/user/anaconda3/envs/sfm/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/bin/ld: cannot find -ljpeg
/home/user/anaconda3/envs/sfm/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/bin/ld: cannot find -lpng
/home/user/anaconda3/envs/sfm/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/bin/ld: cannot find -lz
/home/user/anaconda3/envs/sfm/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/bin/ld: cannot find -ltiff
collect2: error: ld returned 1 exit status
gmake[2]: *** [Linux-x86_64-RELEASE/openMVG_sample_features_affine] Error 1
gmake[1]: *** [openMVG_Samples/features_affine_demo/CMakeFiles/openMVG_sample_features_affine.dir/all] Error 2
gmake: *** [all] Error 2
But libjpeg, libpng etc. are all /home/user/anaconda3/envs/sfm/lib/. For example:
(sfm) [openMVG]$ find /home/user/anaconda3/envs/sfm/ -name 'libjpeg*'
/home/user/anaconda3/envs/sfm/conda-meta/libjpeg-turbo-cos6-x86_64-1.2.1-h24432d5_0.json
/home/user/anaconda3/envs/sfm/lib/libjpeg.a
/home/user/anaconda3/envs/sfm/lib/libjpeg.so
...
So I've tried both of the following:
export LD_PRELOAD=/home/user/anaconda3/envs/sfm/lib/libjpeg.so
and
export LD_LIBRARY_PATH=/home/user/anaconda3/envs/sfm/lib/:/home/user/anaconda3/lib/:/usr/lib64/:/usr/lib/
before rerunning$ cmake --build . --target install, but get the same result.
It seems to me that the compiler couldn't find some libs. So I told it exactly where to look, and even tried preoloading them, and it still can't find them. Adding the paths to LD_LIBRARY_PATH was necessary to get this far - so it does seem to have worked for other things. Just not this problem. How do I troubleshoot from here?

Install libssh fail due to cmake error

So I was trying to install libssh base on instruction from https://github.com/substack/libssh/blob/master/INSTALL
Then when I was trying to build using cmake I have run into problems. I have cmake module on my linux server "cmake/3.7.2-1"
When I have executed command
cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Debug ..
I got following errors:
CMake Error at /afs/.../RHEL6-x86_64/cmake/3.7.2-1/share/cmake-3.7/Modules/CMakeTestCXXCompiler.cmake:44 (message):
The C++ compiler ".../4.9.1/Linux_x86_64_compiled/bin/gcc"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: /local/efrimaz/libSSH/v0-6/build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/gmake" "cmTC_e97bb/fast"
/usr/bin/gmake -f CMakeFiles/cmTC_e97bb.dir/build.make
CMakeFiles/cmTC_e97bb.dir/build
gmake[1]: Entering directory
`/local/efrimaz/libSSH/v0-6/build/CMakeFiles/CMakeTmp'
Building CXX object CMakeFiles/cmTC_e97bb.dir/testCXXCompiler.cxx.o
/.../4.9.1/Linux_x86_64_compiled/bin/gcc -o
CMakeFiles/cmTC_e97bb.dir/testCXXCompiler.cxx.o -c
/local/efrimaz/libSSH/v0-6/build/CMakeFiles/CMakeTmp/testCXXCompiler.cxx
Linking CXX executable cmTC_e97bb
/afs/.../cmake/3.7.2-1/bin/cmake
-E cmake_link_script CMakeFiles/cmTC_e97bb.dir/link.txt --verbose=1
/.../4.9.1/Linux_x86_64_compiled/bin/gcc
CMakeFiles/cmTC_e97bb.dir/testCXXCompiler.cxx.o -o cmTC_e97bb -rdynamic
/usr/bin/ld: unrecognized option '-plugin'
/usr/bin/ld: use the --help option for usage information
collect2: error: ld returned 1 exit status
gmake[1]: *** [cmTC_e97bb] Error 1
gmake[1]: Leaving directory
`/local/efrimaz/libSSH/v0-6/build/CMakeFiles/CMakeTmp'
gmake: *** [cmTC_e97bb/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
examples/CMakeLists.txt:1 (project)
-- Configuring incomplete, errors occurred!
Google didnt help. Any adivse appreciate.

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" ...)

Clang and CMake on Windows

I am building a C++ project with Clang 3.8 and CMake 3.7 on Windows, but Clang emits an error in configure like below.
Determining if the CXX compiler works failed with the following output:
Change Dir: C:/tools/k.build/CMakeFiles/CMakeTmp
Run Build Command:"C:/tools/ninja.exe" "cmTC_c0004"
[1/2] Building CXX object CMakeFiles/cmTC_c0004.dir/testCXXCompiler.cxx.obj
[2/2] Linking CXX executable cmTC_c0004.exe
FAILED: cmTC_c0004.exe
cmd.exe /C "cd . && C:\tools\llvm-3.8.1.build\Release\bin\clang++.exe CMakeFiles/cmTC_c0004.dir/testCXXCompiler.cxx.obj -o cmTC_c0004.exe -Wl,--out-implib,libcmTC_c0004.dll.a -Wl,--major-image-version,0,--minor-image-version,0 && cd ."
LINK : warning LNK4044: unrecognized option '/-out-implib'; ignored
LINK : warning LNK4044: unrecognized option '/-major-image-version'; ignored
LINK : warning LNK4044: unrecognized option '/-minor-image-version'; ignored
LINK : fatal error LNK1181: cannot open input file 'libcmTC_c0004.dll.a'
clang++.exe: error: linker command failed with exit code 1181 (use -v to see invocation)
ninja: build stopped: subcommand failed.
Strange thing is that no file exists in CMakeFiles/CMakeTmp/. How can I get it to work?
Simply put, you are defining your toolchain wrong. CMake thinks you are using the MSVC compiler with a different path. See this question for a detailed answer. Note that you will still need MSVC or Cygwin as a build environment.