1

How to set the SONAME for a shared library with CMake in Ubuntu?

With help of How to add linker flag for libraries with CMake? I have created a CMakeLists.txt:

project(mylib VERSION 1.2.3)

set(src_files_mylib
    Client.cpp      
    Server.cpp
    )
set(hdr_files_mylib
    Client.h
    Server.h
    )

add_library(mylib SHARED ${src_files_mylib} ${hdr_files_mylib})
set_target_properties(mylib PROPERTIES PREFIX "")
set_target_properties(mylib PROPERTIES SUFFIX "")
set_target_properties(mylib PROPERTIES OUTPUT_NAME "mylib.so.${PROJECT_VERSION}")
add_link_options("LINKER: -l,soname,mylib.so.${PROJECT_VERSION_MAJOR}")

Using CMake 3.16 and out of source build. It produces the library named mylib.so.1.2.3 but it does not seem to have an SONAME in it. When executing ldconfig -n . in the library dir, it does not produce a link.

set(CMAKE_SHARED_LINKER_FLAGS "-Wl,soname,mylib.so.${PROJECT_VERSION_MAJOR}")

instead of

add_link_options("LINKER: -l,soname,mylib.so.${PROJECT_VERSION_MAJOR}")

behaves the same to me.

When writing the Makefile manually, the target is built correctly with this (going ldconfig -n . like above produces a link):

(...)
MYLIB_SO=mylib.so.$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH)
MYLIB_SONAME=mylib.so.$(VERSION_MAJOR)
(...)
$(MYLIB_SO): $(OBJ_MYLIB)
    $(CXX) $(SO_FLAGS) -Wl,-soname,$(MYLIB_SONAME) -o $(MYLIB_SO) $(DEBUGFLAGS) $(OBJ_MYLIB)
  • 1
    Version and soversion of the library is controlled by `VERSION` and `SOVERSION` properties. See [that answer](https://stackoverflow.com/a/45843676/3440745) about their usage. – Tsyvarev Sep 28 '20 at 15:13

0 Answers0