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

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?

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

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

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 .

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

qgis link error because of libgeos.so

When I try to build qgis-2.8.1 for arm on linux with cmake, I get the error below:
Linking CXX shared module ../../../output/lib/qgis/plugins/libspatialiteprovider.so
[ 54%] Built target spatialiteprovider
Scanning dependencies of target crssync
[ 54%] Building CXX object src/crssync/CMakeFiles/crssync.dir/main.cpp.o
Linking CXX executable ../../output/bin/crssync
/home/lib/trunk/qgis/../geos/geos-3.3.9-imx53-shared/lib/libgeos_c.so: undefined reference to `std::auto_ptr<geos::geom::Envelope>::auto_ptr(std::auto_ptr_ref<geos::geom::Envelope>)'
/home/lib/trunk/qgis/../geos/geos-3.3.9-imx53-shared/lib/libgeos_c.so: undefined reference to `_ZNSt8auto_ptrIN4geos4geom8EnvelopeEEcvSt12auto_ptr_refIT_EIS2_EEv'
I tried to build qgis with this configuration:
cmake -G "Unix Makefiles"
-DCMAKE_SYSTEM_NAME=Linux
-DCMAKE_INSTALL_PREFIX=/home/lib/trunk/qgis/qgis-2.8.1-imx53-shared
-DCMAKE_BUILD_TYPE=RELEASE
-DENABLE_TESTS=NO
-DWITH_POSTGRESQL=NO
-DWITH_GRASS=NO
-DWITH_PY_COMPILE=NO
-DWITH_BINDINGS=NO
-DENABLE_QT5=NO
-DWITH_QWTPOLAR=TRUE
-DWITH_QSPATIALITE=FALSE
-DWITH_PYSPATIALITE=FALSE
-DPROJ_INCLUDE_DIR=/home/lib/trunk/qgis/../proj/proj-4.8.0-imx53-shared/include
-DPROJ_LIBRARY=/home/lib/trunk/qgis/../proj/proj-4.8.0-imx53-shared/lib/libproj.so
-DGEOS_CONFIG=/home/lib/trunk/qgis/../geos/geos-3.3.9-imx53-shared/bin/geos-config
-DGEOS_INCLUDE_DIR=/home/lib/trunk/qgis/../geos/geos-3.3.9-imx53-shared/include
-DGEOS_LIBRARY=/home/lib/trunk/qgis/../geos/geos-3.3.9-imx53-shared/lib/libgeos_c.so;/home/lib/trunk/qgis/../geos/geos-3.3.9-imx53-shared/lib/libgeos.so
-DGDAL_CONFIG=/home/lib/trunk/qgis/../gdal/gdal-1.11.2-imx53-shared/bin/gdal-config
-DGDAL_INCLUDE_DIR=/home/lib/trunk/qgis/../gdal/gdal-1.11.2-imx53-shared/include
-DGDAL_LIBRARY=/home/lib/trunk/qgis/../gdal/gdal-1.11.2-imx53-shared/lib/libgdal.so;/home/lib/trunk/qgis/../libpng/libpng-1.6.3-imx53-shared/lib/libpng.so;/home/lib/trunk/qgis/../libjpeg/jpeg-6b-imx53-shared/lib/libjpeg.so;/home/lib/trunk/qgis/../libxml2/libxml2-2.7.8-imx53-shared/lib/libxml2.so
-DGSL_CONFIG=/home/lib/trunk/qgis/../gsl/gsl-1.9-imx53-shared/bin/gsl-config
-DGSL_INCLUDE_DIR=/home/lib/trunk/qgis/../gsl/gsl-1.9-imx53-shared/include
-DGSL_LIBRARY=/home/lib/trunk/qgis/../gsl/gsl-1.9-imx53-shared/lib/libgsl.so;/home/lib/trunk/qgis/../gsl/gsl-1.9-imx53-shared/lib/libgslcblas.so
-DSQLITE3_INCLUDE_DIR=/home/lib/trunk/qgis/../sqlite3/sqlite-3080803-imx53-shared/include
-DSQLITE3_LIBRARY=/home/lib/trunk/qgis/../sqlite3/sqlite-3080803-imx53-shared/lib/libsqlite3.so
-DSPATIALITE_INCLUDE_DIR=/home/lib/trunk/qgis/../libspatialite/libspatialite-4.2.0-imx53-shared/include
-DSPATIALITE_LIBRARY=/home/lib/trunk/qgis/../libspatialite/libspatialite-4.2.0-imx53-shared/lib/libspatialite.so;/home/lib/trunk/qgis/../libspatialite/libspatialite-4.2.0-imx53-shared/lib/mod_spatialite.so;/home/lib/trunk/qgis/../sqlite3/sqlite-3080803-imx53-shared/lib/libsqlite3.so;/home/lib/trunk/qgis/../libiconv/libiconv-1.14-imx53-shared/lib/libcharset.so;/home/lib/trunk/qgis/../libiconv/libiconv-1.14-imx53-shared/lib/libiconv.so
-DSPATIALINDEX_INCLUDE_DIR=/home/lib/trunk/qgis/../spatialindex/spatialindex-1.7.0-imx53-shared/include
-DSPATIALINDEX_LIBRARY=/home/lib/trunk/qgis/../spatialindex/spatialindex-1.7.0-imx53-shared/lib/libspatialindex.so;/home/lib/trunk/qgis/../spatialindex/spatialindex-1.7.0-imx53-shared/lib/libspatialindex_c.so
-DEXPAT_INCLUDE_DIR=/home/lib/trunk/qgis/../expat/expat-2.1.0-imx53-shared/include
-DEXPAT_LIBRARY=/home/lib/trunk/qgis/../expat/expat-2.1.0-imx53-shared/lib/libexpat.so
-DQWT_INCLUDE_DIR=/home/lib/trunk/qgis/../qwt/qwt-6.1.1-emb_imx53-shared/include
-DQWT_LIBRARY=/home/lib/trunk/qgis/../qwt/qwt-6.1.1-emb_imx53-shared/lib/libqwt.so;/home/lib/trunk/qgis/../qwt/qwt-6.1.1-emb_imx53-shared/lib/libqwtmathml.so
-DQWTPOLAR_INCLUDE_DIR=/home/lib/trunk/qgis/../qwtpolar/qwtpolar-1.1.1-imx53-shared/include
-DQWTPOLAR_LIBRARY=/home/lib/trunk/qgis/../qwtpolar/qwtpolar-1.1.1-imx53-shared/lib/libqwtpolar.so
-DCMAKE_SYSTEM_PROCESSOR=/home/toolchain/imx53 -DCMAKE_AR=/home/toolchain/imx53/bin/arm-cortex_a8-linux-gnueabi-ar
-DCMAKE_RANLIB=/home/toolchain/imx53/bin/arm-cortex_a8-linux-gnueabi-ranlib
-DCMAKE_NM=/home/toolchain/imx53/bin/arm-cortex_a8-linux-gnueabi-nm
-DCMAKE_OBJCOPY=/home/toolchain/imx53/bin/arm-cortex_a8-linux-gnueabi-objcopy
-DCMAKE_OBJDUMP=/home/toolchain/imx53/bin/arm-cortex_a8-linux-gnueabi-objdump
-DCMAKE_STRIP=/home/toolchain/imx53/bin/arm-cortex_a8-linux-gnueabi-strip
-DCMAKE_C_COMPILER=/home/toolchain/imx53/bin/arm-cortex_a8-linux-gnueabi-gcc
-DCMAKE_CXX_COMPILER=/home/toolchain/imx53/bin/arm-cortex_a8-linux-gnueabi-g++
Also, I built geos-3.3.9 with this configuration:
cmake -G "Unix Makefiles"
-DCMAKE_SYSTEM_NAME=Linux
-DCMAKE_INSTALL_PREFIX=/home/lib/trunk/geos/geos-3.3.9-imx53-shared
-DCMAKE_BUILD_TYPE=RELEASE
-DGEOS_ENABLE_TESTS=OFF
-DBUILD_TESTING=OFF
-DHAVE_LONG_LONG_INT_64=OFF
-DCMAKE_SYSTEM_PROCESSOR=/home/toolchain/imx53
-DCMAKE_AR=/home/toolchain/imx53/bin/arm-cortex_a8-linux-gnueabi-ar
-DCMAKE_RANLIB=/home/toolchain/imx53/bin/arm-cortex_a8-linux-gnueabi-ranlib
-DCMAKE_NM=/home/toolchain/imx53/bin/arm-cortex_a8-linux-gnueabi-nm
-DCMAKE_OBJCOPY=/home/toolchain/imx53/bin/arm-cortex_a8-linux-gnueabi-objcopy
-DCMAKE_OBJDUMP=/home/toolchain/imx53/bin/arm-cortex_a8-linux-gnueabi-objdump
-DCMAKE_STRIP=/home/toolchain/imx53/bin/arm-cortex_a8-linux-gnueabi-strip
-DCMAKE_C_COMPILER=/home/toolchain/imx53/bin/arm-cortex_a8-linux-gnueabi-gcc
-DCMAKE_CXX_COMPILER=/home/toolchain/imx53/bin/arm-cortex_a8-linux-gnueabi-g++
When I take a look at the build error of qgis, I think that this problem is caused by some configuration error of geos at built. But I do not find anything. Also I tried to assign the geos path to rpath for qgis, nothing changed.
I do not know why I get this error. If somebody has an idea, please guide me to solve this problem.
Thanks.

CMake: satisfying dependencies when building shared object file (.so) from object file (.o)

I am able to do this without CMake using a handwritten Makefile, like so:
g++ $(CXSCINC) -c -fPIC cellComplex_extern.cpp -o cellComplex_extern.o
g++ $(CXSCINC) -shared -Wl -o cellComplex_lib.so cellComplex_extern.o $(CXSCLIB) -lcxsc
This gets me shared library cellComplex_lib.so, which then gets picked up by ctypes as a dynamically linked library (lib = ctypes.cdll.LoadLibrary('./cellComplex_lib.so') for later use.
My project has moved to CMake as a build system and I am looking to emulate the functionality of my Makefile above.
So far I have discovered the add_library() command for CMakeLists.txt, but the link to the CXSC library is never made (as evidenced by horrible complaining when I run make after cmake.
How can I tell CMake to build cellComplex_lib with the third-party library CXSC?
-- non-working CMakeLists.txt --
add_library(include/python/cellComplex_extern OBJECT
include/python/cellComplex_extern.cpp ${all_headers})
add_library(include/python/cellComplex_lib SHARED
include/python/cellComplex_extern)
target_link_libraries(include/python/cellComplex_lib ${CXSC_LIB_DIR}/libcxsc.a)
Result of running cmake followed by make:
.
.
.
[ 75%] Built target include/python/cellComplex_extern
Linking CXX shared library libinclude/python/cellComplex_lib.dylib
ld: can't open output file for writing: libinclude/python/cellComplex_lib.dylib, errno=2 for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [libinclude/python/cellComplex_lib.dylib] Error 1
make[1]: *** [CMakeFiles/include/python/cellComplex_lib.dir/all] Error 2
make: *** [all] Error 2
I think you need to use target_link_libraries
target_link_libraries(include/python/cellComplex_lib ${CXSLIB})
This is what I use during Win32 development:
link_directories(${LIB_ROOT_DIR}/lib ${LIB_ROOT_DIR2}/lib/morelibs)
add_library(MyDll1 SHARED File1.cpp File2.cpp)
add_library(MyDll2 SHARED File3.cpp File4.cpp)
add_dependencies(MyDll2 Dll1)
target_link_libraries(MyDll2 Dll1 some.lib another.lib)
Here you specify that Dll2 requires Dll1 and two other external lib's.