# Customize CMAKE\_XXX\_LINK\_EXECUTABLE with -Wl options when linking shared libraries

**URL:** https://discourse.cmake.org/t/customize-cmake-xxx-link-executable-with-wl-options-when-linking-shared-libraries/14124
**Category:** Usage
**Tags:** os:linux
**Created:** [May 20, 2025, 8:03am UTC](https://discourse.cmake.org/t/customize-cmake-xxx-link-executable-with-wl-options-when-linking-shared-libraries/14124 "2025-05-20T08:03:31Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Believer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@Believer](https://discourse.cmake.org/u/Believer)
#### Post date: [May 20, 2025, 8:03am UTC](https://discourse.cmake.org/t/customize-cmake-xxx-link-executable-with-wl-options-when-linking-shared-libraries/14124/1 "2025-05-20T08:03:32Z")

</div>

Hi, I am a new beginner who is trying to write a new language in cmake, writing files such as CMakeXXXInformation.cmake. I am confused at how -Wl option is passed in CMAKE\_CXX\_LINK\_EXECUTABLE. Among those variables I saw (FLAGS, CMAKE\_CXX\_LINK\_FLAGS, LINK\_FLAGS), seems to be not related to -Wl,rpath option.

When building an executable, it seems that linking a shared library always needed -Wl,rpath to find the path to that shared library during runtime. But I really have no clue how to pass that information in my own customized CMAKE\_XXX\_LINK\_EXECUTABLE.

Any help or suggestion is appreciated. Thanks in advance.

---

<div class="post-metadata">

### Author: ![Believer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@Believer](https://discourse.cmake.org/u/Believer)
#### Post date: [May 21, 2025, 8:04am UTC](https://discourse.cmake.org/t/customize-cmake-xxx-link-executable-with-wl-options-when-linking-shared-libraries/14124/2 "2025-05-21T08:04:36Z")

</div>

In details, in the CMakeCXXInformation.cmake, the command to link executable is:  
set(CMAKE\_CXX\_LINK\_EXECUTABLE  
“\<CMAKE\_CXX\_COMPILER\> \<CMAKE\_CXX\_LINK\_FLAGS\> \<LINK\_FLAGS\> -o \<LINK\_LIBRARIES\>”)

I add some -L options into:  
\<CMAKE\_CXX\_COMPILER\> \<CMAKE\_CXX\_LINK\_FLAGS\> \<LINK\_FLAGS\> -o -LAAA \<LINK\_LIBRARIES\> -LBBB.

And the cmake command turns into:  
-o my\_app -LAAA -Wl,-rpath,xxxxx libA.so -LBBB.  
From my view, this “-Wl,-rpath” definitely comes from a cmake set up, but which variable should I focus on to get this “-Wl,-rpath” information？It must be related to \<LINK\_LIBRARIES\> somehow, but what am I missing that does not have “-Wl,-rpath” in my own cmake language？

---

<div class="post-metadata">

### Author: ![Believer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@Believer](https://discourse.cmake.org/u/Believer)
#### Post date: [May 21, 2025, 9:13am UTC](https://discourse.cmake.org/t/customize-cmake-xxx-link-executable-with-wl-options-when-linking-shared-libraries/14124/3 "2025-05-21T09:13:53Z")

</div>

Seems to be the CMAKE\_EXECUTABLE\_RUNTIME\_CXX\_FLAG that matters. 🙂
