How to link ICU4C in cmake via pkg-config on MacOS? [duplicate] - cmake

This question already has answers here:
Cmake cannot find library using "link_directories"
(4 answers)
Closed 3 years ago.
I work on macbook with newest OS. Installed icu4c library via homebrew. But I cannot link libicuuc correctly. In case of find library, I use
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/Cellar/icu4c/64.2/lib/pkgconfig
on my ~/.bashrc, but still got link error.
This is a very simple CMakeLists.txt:
cmake_minimum_required(VERSION 3.4)
project(xeditd CXX)
find_package(PkgConfig REQUIRED)
pkg_search_module(ICU_UC icu-uc)
set(LIB_DIR
${ICU_UC_LIBRARY_DIRS}
)
set(LIB
${ICU_UC_LIBRARIES}
)
set(INC_DIR
${ICU_UC_INCLUDE_DIRS}
)
set(SRC_FILE
xeditd.cpp
)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -o2 ${ICU_UC_CXXFLAGS_OTHER}")
message(${LIB_DIR})
message(${LIB})
message(${INC_DIR})
message(${CMAKE_CXX_FLAGS})
add_executable(xeditd ${SRC_FILE})
link_directories(${LIB_DIR})
target_link_libraries(xeditd ${LIB})
target_include_directories(xeditd PUBLIC ${INC_DIR})
It generate:
/usr/local/Cellar/icu4c/64.2/lib (${LIB_DIR})
icuucicudata (${LIB})
/usr/local/Cellar/icu4c/64.2/include (${INC_DIR})
-std=c++14 -std=c++14 -o2 (${CMAKE_CXX_FLAGS})
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/rolin/workspace/github/xedit/.bin
[ 50%] Linking CXX executable xedit
ld: library not found for -licuuc
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [xedit/xedit] Error 1
make[1]: *** [xedit/CMakeFiles/xedit.dir/all] Error 2
make: *** [all] Error 2
[ 50%] Linking CXX executable xeditd
ld: library not found for -licuuc
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [xeditd/xeditd] Error 1
make[1]: *** [xeditd/CMakeFiles/xeditd.dir/all] Error 2
make: *** [all] Error 2
I don't known why libicuuc.dylib not found.

link_directories only affects targets that come AFTER it. So I move add_executable after the link_directories call.

Related

up dating linker ld on Mac

I am trying to compile an example pytroch c++ project using instructions at:
https://pytorch.org/cppdocs/installing.html
When i reach the point of calling:
cmake -DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch ..
cmake --build . --config Release
I get an error:
ld: unknown option: --no-as-needed
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [BLOCK_READER] Error 1
make[1]: *** [CMakeFiles/BLOCK_READER.dir/all] Error 2
make: *** [all] Error 2
It looks like the issue is that the linker doesnt understand the option --no-as-needed.
After some research it looks like newer version of the linker support this option, but not the version i have.
So my question is how do i update my linker to a version that support this option?
I am on a Apple M1 (BigSur)..
It is my understanding that ld is part of gcc so can i just update gcc?
Btw here is my cmake CMakeLists.txt
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(block_reader)
# Setup Torch
set(CMAKE_CXX_COMPILER "/usr/bin/g++" CACHE STRING "C++ compiler" FORCE)
set(CMAKE_LINKER "/usr/bin/ld" CACHE STRING "" FORCE)
set(CMAKE_PREFIX_PATH /Users/username/Downloads/libtorch)
find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
# Setup Torch
add_executable(BLOCK_READER main.cpp)
target_include_directories(BLOCK_READER PRIVATE /src)
set_property(TARGET BLOCK_READER PROPERTY CXX_STANDARD 14)
target_link_libraries(BLOCK_READER "${TORCH_LIBRARIES}")

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

Clion, cmake DSO missing when building simple2d

I have started with a project that are using the simple2d library (https://github.com/simple2d/simple2d). But when I try to build my small example project I get a DSO missing error. I think a have manage to link the dependencies for simple2d, but apparently I'm still some thing.
Here is the output:
> ====================[ Build | c_game | Debug ]==================================
/home/fredrik/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/192.5728.100/bin/cmake/linux/bin/cmake --build /home/fredrik/Repos/c_game/cmake-build-debug --target c_game -- -j 2 -m
[ 50%] Linking C executable c_game
/usr/bin/ld: /usr/local/lib/libsimple2d.a(gl.o): undefined reference to symbol 'fmin##GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libm.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
CMakeFiles/c_game.dir/build.make:85: recipe for target 'c_game' failed
make[3]: *** [c_game] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/c_game.dir/all' failed
make[2]: *** [CMakeFiles/c_game.dir/all] Error 2
CMakeFiles/Makefile2:84: recipe for target 'CMakeFiles/c_game.dir/rule' failed
make[1]: *** [CMakeFiles/c_game.dir/rule] Error 2
Makefile:118: recipe for target 'c_game' failed
make: *** [c_game] Error 2
And here is my CMakeList.txt, I hope someone can help me.
cmake_minimum_required(VERSION 3.14)
project(c_game C)
set(CMAKE_C_STANDARD 11)
find_package(SDL2 REQUIRED)
find_package(GLEW REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})
add_executable(c_game main.c)
find_library(SIMPLE2D_LIB libsimple2d.a /usr/local/lib)
message(${SIMPLE2D_LIB})
message(${SDL2_INCLUDE_DIRS})
message(${GLEW_INCLUDE_DIRS})
target_include_directories(c_game PUBLIC ${SDL2_INCLUDE_DIRS} ${GLEW_INCLUDE_DIRS})
target_link_libraries(c_game ${SIMPLE2D_LIB} ${SDL2_LIBRARIES} ${GLEW_LIBRARIES})

cmake 3.10 cant internally test the blas library

I cant get working my blas library with cmake. I have arch-linxux, both blas and cmake are installed with pacman. Cant understand where is problem :-(. I made cmake to print out the output of macro CHECK_FORTRAN_FUNCTION_EXISTS, because it fails there. Maybe I dont have proper configuration of ld?
My simple testing CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(Test C Fortran)
find_package(BLAS)
Error part from output:
-- Looking for Fortran sgemm
-- Change Dir: /home/jiri/test_lapack/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_486fc/fast"
/usr/bin/make -f CMakeFiles/cmTC_486fc.dir/build.make CMakeFiles/cmTC_486fc.dir/build
make[1]: Entering directory '/home/jiri/test_lapack/CMakeFiles/CMakeTmp'
Building Fortran object CMakeFiles/cmTC_486fc.dir/testFortranCompiler.f.o
/usr/bin/gfortran -c /home/jiri/test_lapack/CMakeFiles/CMakeTmp/testFortranCompiler.f -o CMakeFiles/cmTC_486fc.dir/testFortranCompiler.f.o
Linking Fortran executable cmTC_486fc
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_486fc.dir/link.txt --verbose=1
/usr/bin/gfortran CMakeFiles/cmTC_486fc.dir/testFortranCompiler.f.o -o cmTC_486fc /usr/local/lib64/libblas.a
/usr/bin/ld: /usr/local/lib64/libblas.a(sgemm.f.o): relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: /usr/local/lib64/libblas.a(xerbla.f.o): relocation R_X86_64_32S against `.rodata' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
make[1]: *** [CMakeFiles/cmTC_486fc.dir/build.make:99: cmTC_486fc] Error 1
make[1]: Leaving directory '/home/jiri/test_lapack/CMakeFiles/CMakeTmp'
make: *** [Makefile:126: cmTC_486fc/fast] Error 2
-- Looking for Fortran sgemm - not found

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.