Setting `IMPORTED_NO_SONAME` causes link failure for irregular shared library file name under Linux using GNU ld
I am using CMake 3.20.3 with gcc 11.1 and GNU ld 2.36.1 under Linux. I have a repository directory which I'll represent its location by <src_dir>
. I have a shared library called a.so
located in <src_dir>
, which doesn't have a soname field. (In this case this library can be created by a simple C program source file and with command gcc -shared -o a.so -O2 -fPIC XXX.c
)
The repository then only contains two CMakeLists.txt
s. One is ./CMakeLists.txt
:
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project("test_proj")
add_subdirectory(src)
The other is ./src/CMakeLists.txt
:
add_library(a SHARED IMPORTED)
set_target_properties(a PROPERTIES IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/a.so" IMPORTED_NO_SONAME ON)
file(WRITE "${CMAKE_BINARY_DIR}/b.c" "int main()\{\}")
add_executable(b "${CMAKE_BINARY_DIR}/b.c")
target_link_libraries(b PRIVATE a)
install(TARGETS b ARCHIVE DESTINATION lib LIBRARY DESTINATION lib RUNTIME DESTINATION bin)
Then in the build dir I build the project by running cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=install <src_dir> && cmake --build . --config Debug
, and the build process reports the following error:
[ 50%] Building C object src/CMakeFiles/b.dir/__/b.c.o
[100%] Linking C executable b
/usr/sbin/ld: cannot find -la
collect2: error: ld returned 1 exit status
make[2]: *** [src/CMakeFiles/b.dir/build.make:98: src/b] Error 1
make[1]: *** [CMakeFiles/Makefile2:98: src/CMakeFiles/b.dir/all] Error 2
make: *** [Makefile:136: all] Error 2
That seems to indicate linker is searching for liba.so
rather than a.so
. If I rename a.so
to liba.so
and replace the corresponding file name in src/CMakeLists.txt
, the build process would succeed.
However, I checked the document for IMPORTED_NO_SONAME
and it doesn't mention any requirements of how name of a library file should be. Is this behavior intended? Should we improve the document or do something more?