# Gracefully create symbolic link for static libs like for shared libs

**URL:** <https://discourse.cmake.org/t/gracefully-create-symbolic-link-for-static-libs-like-for-shared-libs/10458>\
**Category:** Code\
**Tags:** os:linux\
**Created:** [March 23, 2024, 9:58am UTC](https://discourse.cmake.org/t/gracefully-create-symbolic-link-for-static-libs-like-for-shared-libs/10458 "2024-03-23T09:58:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![evilroach](https://discourse.cmake.org/user_avatar/discourse.cmake.org/evilroach/32/2805_2.png) [@evilroach](https://discourse.cmake.org/u/evilroach)\
**Post date:** [March 23, 2024, 9:58am UTC](https://discourse.cmake.org/t/gracefully-create-symbolic-link-for-static-libs-like-for-shared-libs/10458/1 "2024-03-23T09:58:07Z")

</div>

As we know, only for Shared Libs, a symbolic link is created automatically, not for Static Libs.  
For example:  
If we have a target `libLeonLog.so.1.2.3`, we will get a symbolic link `libLeonlog.so` and `libLeonLog.so.1` that is pointing to the real target `libLeonLog.so.1.2.3`.

Now I want to do the same for my static lib.  
At first, I defined my lib like this:

```auto
add_library( target_static STATIC LogToFile.cpp )
set_target_properties( target_static PROPERTIES
	OUTPUT_NAME_RELEASE LeonLog
	OUTPUT_NAME_DEBUG LeonLog
	PUBLIC_HEADER LeonLog.hpp
	VERSION ${PROJECT_VERSION}
	SUFFIX ".a.${PROJECT_VERSION}"
#[[ Here I want keep the property "PREFIX" in default value('lib'), so I removed this line:
        PREFIX "lib"
]]
)

install( TARGETS target_static
	ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
	PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

```

And then, I’m trying to add a symbolic link by means of running a shell command when installing.  
Like this:

```auto
get_target_property( MY_NAME target_static ARCHIVE_OUTPUT_NAME_RELEASE )
message( STATUS "MY_NAME:\"${MY_NAME}\"" )
install( CODE "execute_process(
	COMMAND ln -fs ${MY_NAME}.${PROJECT_VERSION} ${MY_NAME}
	WORKING_DIRECTORY ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR} )" )

```

But I can **NOT** get the value of PROPERTY “ARCHIVE\_OUTPUT\_NAME\_RELEASE”, I got a empty string.  
I have also tried many other properties, but all failed.  
like this:

```auto
get_target_property( MY_NAME target_static LIBRARY_OUTPUT_NAME_RELEASE )
message( STATUS "LIBRARY_OUTPUT_NAME_RELEASE:\"${MY_NAME}\"" )
get_property( MY_NAME TARGET target_static PROPERTY OUTPUT_NAME )
message( STATUS "OUTPUT_NAME:\"${MY_NAME}\"" )

```

Finally, I have to compose it by myself, like this:

```auto
get_target_property( MY_NAME target_static OUTPUT_NAME_RELEASE )
# compose it with adding a prefix, but it's hard coding!
set( MY_NAME "lib${MY_NAME}.a" )
message( STATUS "MY_NAME:\"${MY_NAME}\"" )
install( CODE "execute_process(
	COMMAND ln -fs ${MY_NAME}.${PROJECT_VERSION} ${MY_NAME}
	WORKING_DIRECTORY ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR} )" )

```

But I think it is ugly, and not the correct way.

Is there an “Official” way to do this?  
Sorry for my English!

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [April 11, 2024, 12:09am UTC](https://discourse.cmake.org/t/gracefully-create-symbolic-link-for-static-libs-like-for-shared-libs/10458/2 "2024-04-11T00:09:24Z")

</div>

> [@evilroach](#):
>
> Now I want to do the same for my static lib.

I’m going to start here…why? There’s no purpose to SONAME or SOVERSION filenames for static libraries as they have no runtime existence.

---

<div class="post-metadata">

**Author:** ![evilroach](https://discourse.cmake.org/user_avatar/discourse.cmake.org/evilroach/32/2805_2.png) [@evilroach](https://discourse.cmake.org/u/evilroach)\
**Post date:** [April 11, 2024, 2:36pm UTC](https://discourse.cmake.org/t/gracefully-create-symbolic-link-for-static-libs-like-for-shared-libs/10458/3 "2024-04-11T14:36:06Z")

</div>

For example:  
I’m developing the new version of my project(let’s call it Octopus- **2.0** ), meanwhile I have to maintain/bugfix the producing version of the same project(Octopus- **1.0** ).

The project Octopus depends on another project LeonLog that has two versions too, and Octopus- **2.0** should be linked against the static libs of LeonLog- **5.0** , and Octopus- **1.0** should keep to use LeonLog- **3.0** still.

Hence I keep multiple versions of LeonLog’s static libs existing at `/usr/local/lib`, and need to frequently switch between those two.  
Like this:

```auto
leon@galvatron:~$ ls /usr/local/lib
lrwxrwxrwx 1 leon leon 18 2024-03-24 23:29 libLeonLog.a -> libLeonLog.a.5.0.5
-rw-r--r-- 1 leon leon 207K 2023-11-03 15:23 libLeonLog.a.3.7.2
-rw-r--r-- 1 leon leon 214K 2024-03-24 23:29 libLeonLog.a.5.0.5

```

Is there any better method?

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [April 11, 2024, 3:40pm UTC](https://discourse.cmake.org/t/gracefully-create-symbolic-link-for-static-libs-like-for-shared-libs/10458/4 "2024-04-11T15:40:58Z")

</div>

Put the version number in the library name itself.

```cmake
set_property(TARGET ${tgt}
  PROPERTY
    OUTPUT_NAME "${tgt}-${major_version}")

```
