# cmake multi sub projects : problem with path of lib

**URL:** https://discourse.cmake.org/t/cmake-multi-sub-projects-problem-with-path-of-lib/6021
**Category:** Usage
**Created:** [July 7, 2022, 10:03am UTC](https://discourse.cmake.org/t/cmake-multi-sub-projects-problem-with-path-of-lib/6021 "2022-07-07T10:03:47Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![mickae1](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mickae1/32/2479_2.png) [@mickae1](https://discourse.cmake.org/u/mickae1)
#### Post date: [July 7, 2022, 10:03am UTC](https://discourse.cmake.org/t/cmake-multi-sub-projects-problem-with-path-of-lib/6021/1 "2022-07-07T10:03:47Z")

</div>

Hi,

I have a project with sub projects :

```auto
    ├── CMakeLists.txt
    ├── projects
        ├── libA
        │ ├── CMakeLists.txt
        │ ├── include
        │ │ └── libA
        │ │ └── my_liba.h
        │ ├── src
        │ │ └── my_liba.cpp
        │ └── test
        ├── libB
        │ ├── CMakeLists.txt
        │ ├── include
        │ │ └── libB
        │ │ └── my_libb.h
        │ ├── src
        │ │ └── my_libb.cpp
        │ └── test
        └── runner
            ├── CMakeLists.txt
            └── src
                └── main.cpp

```

My problem is that the runner is compiled with the path to the libA and libB :

gcc-linaro-…-mingw32\_arm-linux-gnueabihf\bin\arm-linux-gnueabihf-gcc.exe -LC:/openssl/lib -ldl -llzma -lz -lrt -lpthread projects/libA/libA.so projects/libB/libB.so

In the runner CmakeList file I have:

target\_link\_libraries (runner  
libA  
libB  
)  
On My raspberry pi device, I don’t have a folder projects …

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [July 7, 2022, 10:55am UTC](https://discourse.cmake.org/t/cmake-multi-sub-projects-problem-with-path-of-lib/6021/2 "2022-07-07T10:55:50Z")

</div>

What is produced is planned to be used in situ.

If you want to distribute the artifacts, you have to install then (see [install()](https://cmake.org/cmake/help/latest/command/install.html) command). The installation process handle to update binaries (or relink them) to enable portable usage…

See also various `RPATH` variables and properties to control the way libraries are linked.

---

<div class="post-metadata">

### Author: ![mickae1](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mickae1/32/2479_2.png) [@mickae1](https://discourse.cmake.org/u/mickae1)
#### Post date: [July 7, 2022, 12:49pm UTC](https://discourse.cmake.org/t/cmake-multi-sub-projects-problem-with-path-of-lib/6021/3 "2022-07-07T12:49:50Z")

</div>

Thanks for the answer.

I added:  
install(TARGETS ${project\_name} DESTINATION Debug)

and I tried:

set(CMAKE\_BUILD\_RPATH\_USE\_ORIGIN TRUE)  
set(CMAKE\_SKIP\_RPATH TRUE)  
set(CMAKE\_SKIP\_BUILD\_RPATH TRUE)  
SET(CMAKE\_SKIP\_BUILD\_RPATH TRUE)

But it didn’t work ☹

I still have the path of the lib in my executable.

What is the correct way to edit the executable and remove the path of the lib inside ?

---

<div class="post-metadata">

### Author: ![mickae1](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mickae1/32/2479_2.png) [@mickae1](https://discourse.cmake.org/u/mickae1)
#### Post date: [July 7, 2022, 4:00pm UTC](https://discourse.cmake.org/t/cmake-multi-sub-projects-problem-with-path-of-lib/6021/4 "2022-07-07T16:00:13Z")

</div>

I would like cmake use this :

```auto
gcc ... -L /home/projects/my_files -lwinter_fresh

```

and not:

```auto
gcc ... /home/projects/my_files/libwinter_fresh.so

```

I have used :

```auto
target_link_directories( ${project_name} PRIVATE
   ${project_lib_path}
)
target_link_libraries (${project_name}
libA
libB
)

```

---

<div class="post-metadata">

### Author: ![mickae1](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mickae1/32/2479_2.png) [@mickae1](https://discourse.cmake.org/u/mickae1)
#### Post date: [July 7, 2022, 4:39pm UTC](https://discourse.cmake.org/t/cmake-multi-sub-projects-problem-with-path-of-lib/6021/5 "2022-07-07T16:39:48Z")

</div>

I found a way to fix my problem, but it’s not the best one … it’s not clean.

```auto
target_link_libraries (${project_name}
-llibA
-llibB
)

```

I added -l before each lib

---

<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: [July 9, 2022, 1:20pm UTC](https://discourse.cmake.org/t/cmake-multi-sub-projects-problem-with-path-of-lib/6021/6 "2022-07-09T13:20:27Z")

</div>

CMake vastly prefers to say “use this file” rather than (re-)construct a set of flags to lead the linker to the same conclusion. What’s the problem that using full paths causes?

---

<div class="post-metadata">

### Author: ![mickae1](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mickae1/32/2479_2.png) [@mickae1](https://discourse.cmake.org/u/mickae1)
#### Post date: [July 9, 2022, 11:52pm UTC](https://discourse.cmake.org/t/cmake-multi-sub-projects-problem-with-path-of-lib/6021/7 "2022-07-09T23:52:03Z")

</div>

My problem is that the path of the lib is built into my executable. And in my remote machine this path doesn’t exist.

And cmake doesn’t remove the path with install…

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [July 10, 2022, 10:52am UTC](https://discourse.cmake.org/t/cmake-multi-sub-projects-problem-with-path-of-lib/6021/8 "2022-07-10T10:52:48Z")

</div>

Is the library to be linked with maybe missing an SONAME?

---

<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: [July 10, 2022, 6:56pm UTC](https://discourse.cmake.org/t/cmake-multi-sub-projects-problem-with-path-of-lib/6021/9 "2022-07-10T18:56:51Z")

</div>

> [@mickae1](#):
>
> My problem is that the path of the lib is built into my executable

In what way? Can you provide the `readelf -d $lib` output of the file that fails when using full paths? A missing SONAME could cause something like this (though I’m not familiar with embedded idiosyncrasies).

---

<div class="post-metadata">

### Author: ![mickae1](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mickae1/32/2479_2.png) [@mickae1](https://discourse.cmake.org/u/mickae1)
#### Post date: [July 11, 2022, 3:17pm UTC](https://discourse.cmake.org/t/cmake-multi-sub-projects-problem-with-path-of-lib/6021/10 "2022-07-11T15:17:25Z")

</div>

Thanks,

My project has:

0x00000001 (NEEDED) Shared library: [libdl.so.2]  
0x00000001 (NEEDED) Shared library: [liblzma.so.5]  
0x00000001 (NEEDED) Shared library: [libz.so.1]  
0x00000001 (NEEDED) Shared library: [librt.so.1]  
0x00000001 (NEEDED) Shared library: [libpthread.so.0]  
0x00000001 (NEEDED) Shared library: [libxml2.so.2]  
0x00000001 (NEEDED) Shared library: [libsqlite3.so.0]  
0x00000001 (NEEDED) Shared library: [projects/LibA/libA.so]  
0x00000001 (NEEDED) Shared library: [projects/LibBt/libB.so]  
0x00000001 (NEEDED) Shared library: [projects/LibC/libC.so]  
0x00000001 (NEEDED) Shared library: [libcrypto.so.1.1]  
0x00000001 (NEEDED) Shared library: [libc.so.6]

I have edited /etc/[ld.so.conf.to](http://ld.so.conf.to) add libA.so in the LD\_LIBRARY\_PATH .  
But it doesn’t work, That’s why I would like to delete the path projects/LibA projects/LibB projects/LibC

---

<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: [July 11, 2022, 3:31pm UTC](https://discourse.cmake.org/t/cmake-multi-sub-projects-problem-with-path-of-lib/6021/11 "2022-07-11T15:31:31Z")

</div>

I would suggest instead setting a SONAME for those libraries. Alternatively, you can change their `LIBRARY_OUTPUT_DIRECTORY` property to place them in a better location in the build tree to avoid these paths. But the SONAME is the better solution here.

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [July 12, 2022, 7:32am UTC](https://discourse.cmake.org/t/cmake-multi-sub-projects-problem-with-path-of-lib/6021/12 "2022-07-12T07:32:08Z")

</div>

Are you installing the project to a local directory prior to distributing it to the remote system? That should be preferred (instead of taking the binaries directly from the build tree).

---

<div class="post-metadata">

### Author: ![mickae1](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mickae1/32/2479_2.png) [@mickae1](https://discourse.cmake.org/u/mickae1)
#### Post date: [July 12, 2022, 7:34am UTC](https://discourse.cmake.org/t/cmake-multi-sub-projects-problem-with-path-of-lib/6021/13 "2022-07-12T07:34:25Z")

</div>

Thanks,

Ok I will try with the SONAME.

Is it enough to make a lib with SONAME =\>

project(${project\_name} VERSION 1.0.0)  
SET\_PROPERTY(GLOBAL PROPERTY TARGET\_SUPPORTS\_SHARED\_LIBS TRUE)

option(BUILD\_SHARED\_LIBS “Build using shared libraries” ON)

add\_library(${project\_name} ${files})

---

<div class="post-metadata">

### Author: ![mickae1](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mickae1/32/2479_2.png) [@mickae1](https://discourse.cmake.org/u/mickae1)
#### Post date: [July 12, 2022, 8:46am UTC](https://discourse.cmake.org/t/cmake-multi-sub-projects-problem-with-path-of-lib/6021/14 "2022-07-12T08:46:07Z")

</div>

I found out what was my problem =\>  
this was needed in my toolchain file :  
SET(CMAKE\_SHARED\_LIBRARY\_SONAME\_C\_FLAG “-Wl,-soname,”)

now I have after install on my WINDOWS machine :  
libA.so  
libA.so.1  
libA.so.1.0.0

When I copy it into my linux machine, I have with ldconfig :

libA.so.1 is not a symbolic link

I should copy files with extension so.1.0.0 and make a symbolic link to libA.so and libA.so.1 ?

Thanks,

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [July 12, 2022, 9:50am UTC](https://discourse.cmake.org/t/cmake-multi-sub-projects-problem-with-path-of-lib/6021/15 "2022-07-12T09:50:36Z")

</div>

> [@mickae1](#):
>
> I found out what was my problem =\>  
> this was needed in my toolchain file :  
> SET(CMAKE\_SHARED\_LIBRARY\_SONAME\_C\_FLAG “-Wl,-soname,”)

That’s weird as it’s set in uncoditional in Platform/Linux.cmake. Does your toolchain file properly setup cross-compiling to Linux, e.g. sets CMAKE\_SYSTEM\_NAME?

---

<div class="post-metadata">

### Author: ![mickae1](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mickae1/32/2479_2.png) [@mickae1](https://discourse.cmake.org/u/mickae1)
#### Post date: [July 12, 2022, 10:21am UTC](https://discourse.cmake.org/t/cmake-multi-sub-projects-problem-with-path-of-lib/6021/16 "2022-07-12T10:21:31Z")

</div>

You are right,

I had SET(CMAKE\_SYSTEM\_NAME Generic) switching to Linux works also. Thanks.
