# Extract linker args for dependencies

**URL:** https://discourse.cmake.org/t/extract-linker-args-for-dependencies/9702
**Category:** Code
**Created:** [December 22, 2023, 10:44pm UTC](https://discourse.cmake.org/t/extract-linker-args-for-dependencies/9702 "2023-12-22T22:44:48Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![tgross35](https://discourse.cmake.org/user_avatar/discourse.cmake.org/tgross35/32/3608_2.png) [@tgross35](https://discourse.cmake.org/u/tgross35)
#### Post date: [December 22, 2023, 10:44pm UTC](https://discourse.cmake.org/t/extract-linker-args-for-dependencies/9702/1 "2023-12-22T22:44:48Z")

</div>

I have a setup where an external tool (cargo) produces a shared library, but it needs to be able to link dependencies generated by CMake. My current setup looks like this:

```cmake
add_custom_target(${target_name} ALL
  COMMAND ${CMAKE_COMMAND}
  ${env_args}
  ${cargo_cmd}
  --crate-type=cdylib
  --
  # This needs to contain `-l`/`-L` arguments
  ${rustc_extra_args}
  WORKING_DIRECTORY ${rust_dir}
  COMMENT "start cargo for ${target_name} with '${cargo_cmd}' dynamic"
  VERBATIM
)

# `foo` and `bar` are created via `add_library` in some other file,
# also has some `target_link_libraryes` set
add_dependencies(${target_name} foo bar)

```

My problem is that the `add_custom_target` command needs to be passed `-l` and `-L` linker args to correctly link `libfoo` and `libbar`, including their transitive dependencies. This seems like a case for generator expressions, but I don’t see any on [this page](https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html#link-context) that helps locate information about dependencies.

I also cannot query anything with `get_target_property` before the dependency is added.

Ideally I would have something simple that expands to `-Lnative=/build/foo-path -Lnative=/build/bar-path -lstatic=foo -lstatic=bar`, or just the full library paths to pass to the linker.

What is the best way to go about this? My current solution is to hand-write library output paths, but this isn’t very maintainable.

---

<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: [December 24, 2023, 1:30am UTC](https://discourse.cmake.org/t/extract-linker-args-for-dependencies/9702/2 "2023-12-24T01:30:28Z")

</div>

It’s a bit annoying but you can:

- write a target that links like the Rust target should
- set the [`<LANG>_COMPILER_LAUNCHER` property](https://cmake.org/cmake/help/latest/prop_tgt/LANG_COMPILER_LAUNCHER.html) on that target and capture the relevant bits and write it to a file
- read that file and pass it to the `cargo` command as you see fit
