# Make Linker Script Available To An Upstream Dependency

**URL:** https://discourse.cmake.org/t/make-linker-script-available-to-an-upstream-dependency/12688
**Category:** Code
**Created:** [October 5, 2024, 5:37pm UTC](https://discourse.cmake.org/t/make-linker-script-available-to-an-upstream-dependency/12688 "2024-10-05T17:37:55Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Scruffmeister](https://discourse.cmake.org/user_avatar/discourse.cmake.org/scruffmeister/32/4985_2.png) [@Scruffmeister](https://discourse.cmake.org/u/Scruffmeister)
#### Post date: [October 5, 2024, 5:37pm UTC](https://discourse.cmake.org/t/make-linker-script-available-to-an-upstream-dependency/12688/1 "2024-10-05T17:37:55Z")

</div>

I have a linker script (`link1.ld`) in `depL` that I need to use in `depA`.

Please could someone explain to me how make `depL` make the `link1.ld` script available to `depA` so that the following works:

`target_link_options(depA PRIVATE -T#[[Valid reference to]]link1.ld)`

The full directory structure and file content for my example is below.

## Directory Structure

```
.
│ CMakeLists.txt
│
├───depA
│ │ CMakeLists.txt
│ │
│ └───src
│ depA.c
│
└───depL
    │ CMakeLists.txt
    │
    ├───src
    │ link1.ld
    │
    └───include
            mem_map.h

```

## Top-Level CMakeLists.txt

```
cmake_minimum_required(VERSION 3.29)
project(proj_cmake1 C)

add_subdirectory(depA)
add_subdirectory(depL)

```

## depL CMakeLists.txt

```
cmake_minimum_required(VERSION 3.29)
project(proj_depL C)

add_library(depL INTERFACE)
target_sources(depL INTERFACE src/link1.ld)
target_include_directories(depL INTERFACE include)

```

## depA CMakeLists.txt

```
cmake_minimum_required(VERSION 3.29)
project(proj_depA C)

add_executable(depA src/depA.c)
target_link_libraries (depA depL)
target_link_options(depA PRIVATE -T#[[Valid reference to]]link1.ld)

```

---

<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: [October 5, 2024, 9:08pm UTC](https://discourse.cmake.org/t/make-linker-script-available-to-an-upstream-dependency/12688/2 "2024-10-05T21:08:23Z")

</div>

A possible solution is to specify link option as part of `depL` target.  
depL CMakeLists.txt:

```auto
add_library(depL INTERFACE)
target_link_options(depL INTERFACE -T${CMAKE_CURRENT_SOURCE_DIR}/src/link1.ld)
target_include_directories(depL INTERFACE include)

```

And linking `depA` with `depL` will be enough:

```auto
add_executable(depA src/depA.c)
target_link_libraries (depA PRIVATE depL)

```

---

<div class="post-metadata">

### Author: ![Scruffmeister](https://discourse.cmake.org/user_avatar/discourse.cmake.org/scruffmeister/32/4985_2.png) [@Scruffmeister](https://discourse.cmake.org/u/Scruffmeister)
#### Post date: [October 6, 2024, 1:02pm UTC](https://discourse.cmake.org/t/make-linker-script-available-to-an-upstream-dependency/12688/3 "2024-10-06T13:02:14Z")

</div>

Thank you Marc, that works perfectly and results in a very elegant solution.
