# How to add Linker Script for GHS Multi Generator

**URL:** https://discourse.cmake.org/t/how-to-add-linker-script-for-ghs-multi-generator/770
**Category:** Usage
**Created:** [March 5, 2020, 3:54pm UTC](https://discourse.cmake.org/t/how-to-add-linker-script-for-ghs-multi-generator/770 "2020-03-05T15:54:36Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Qichen\_Huang](https://discourse.cmake.org/user_avatar/discourse.cmake.org/qichen_huang/32/416_2.png) [@Qichen\_Huang](https://discourse.cmake.org/u/Qichen_Huang)
#### Post date: [March 5, 2020, 3:54pm UTC](https://discourse.cmake.org/t/how-to-add-linker-script-for-ghs-multi-generator/770/1 "2020-03-05T15:54:36Z")

</div>

When I use Makefile generator, there is no problem to add the linker script as a link option like this:

```
add_link_options (
    ${PROJECT_LINK_FLAGS}
    ${LD_MAP}
    ${LINKER_SCRIPT}
)

```

However, it doesn’t work for GHS Multi Generator.

The problem is, the linker script file will be used as a linker option in the generated gpj file like this:

```
[Program]
    :binDirRelative="."
    -o "project.elf"
    :outputDirRelative="project.dir"
    -xxx (link flags)
    path/to/linker_script.ld
project.dir/Object Libraries.gpj [Subproject]

```

The GHS compiler doesn’t recognize the linker\_script.ld as a option in the gpj file.

As a workaround, I have to remove the white spaces before the linker\_script.ld, then the compile works. As following:

```
[Program]
    :binDirRelative="."
    -o "project.elf"
    :outputDirRelative="project.dir"
    -xxx (link flags)
path/to/linker_script.ld # <- remove the white spaces
project.dir/Object Libraries.gpj [Subproject]

```

It seems the linker script file must be treated as a source but not as a link option.

Did I used the link option wrong? What is the best way to add the linker script for GHS Multi Generator?

The cmake version I am using is cmake-3.15.3-win64-x64

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [March 6, 2020, 5:40pm UTC](https://discourse.cmake.org/t/how-to-add-linker-script-for-ghs-multi-generator/770/2 "2020-03-06T17:40:11Z")

</div>

> [@Qichen\_Huang](#):
>
> It seems the linker script file must be treated as a source but not as a link option.

I guess you try using `target_link_libraries` instead of `add_link_options`.

---

<div class="post-metadata">

### Author: ![fdk17](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/ea666f/32.png) [@fdk17](https://discourse.cmake.org/u/fdk17)
#### Post date: [March 6, 2020, 6:17pm UTC](https://discourse.cmake.org/t/how-to-add-linker-script-for-ghs-multi-generator/770/3 "2020-03-06T18:17:01Z")

</div>

I don’t recall if GHS takes the Linker Script as an option or as a file input into the executable.

But off hand I would say that a GHS Multi project gets interpreted by gbuild which has it own set of rules and options. Comparison to how gcc works is not always valid.

If it does take the linker script as an option you’ll have to check the manual what the correct flag is.

Otherwise you just add it to the list of files that make up the executable and gbuild should sort it out.

---

<div class="post-metadata">

### Author: ![Qichen\_Huang](https://discourse.cmake.org/user_avatar/discourse.cmake.org/qichen_huang/32/416_2.png) [@Qichen\_Huang](https://discourse.cmake.org/u/Qichen_Huang)
#### Post date: [March 7, 2020, 9:17am UTC](https://discourse.cmake.org/t/how-to-add-linker-script-for-ghs-multi-generator/770/4 "2020-03-07T09:17:51Z")

</div>

> I guess you try using `target_link_libraries` instead of `add_link_options`.

The add\_link\_options will generate a -l"path/to/script.ld"

This -l is not accepted by GHS compiler for linker script. GHS compiler expect to have the linker script without any flags.

---

<div class="post-metadata">

### Author: ![Qichen\_Huang](https://discourse.cmake.org/user_avatar/discourse.cmake.org/qichen_huang/32/416_2.png) [@Qichen\_Huang](https://discourse.cmake.org/u/Qichen_Huang)
#### Post date: [March 7, 2020, 9:45am UTC](https://discourse.cmake.org/t/how-to-add-linker-script-for-ghs-multi-generator/770/5 "2020-03-07T09:45:16Z")

</div>

> Otherwise you just add it to the list of files that make up the executable and gbuild should sort it out.

It is correct, the linker script should be added into the list of files that make up the executable. Can you give me a hint, how to do it in CMake?

---

<div class="post-metadata">

### Author: ![fdk17](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/ea666f/32.png) [@fdk17](https://discourse.cmake.org/u/fdk17)
#### Post date: [March 7, 2020, 1:07pm UTC](https://discourse.cmake.org/t/how-to-add-linker-script-for-ghs-multi-generator/770/6 "2020-03-07T13:07:37Z")

</div>

List it in the add\_executable command with the other source code.

---

<div class="post-metadata">

### Author: ![Qichen\_Huang](https://discourse.cmake.org/user_avatar/discourse.cmake.org/qichen_huang/32/416_2.png) [@Qichen\_Huang](https://discourse.cmake.org/u/Qichen_Huang)
#### Post date: [March 7, 2020, 8:18pm UTC](https://discourse.cmake.org/t/how-to-add-linker-script-for-ghs-multi-generator/770/7 "2020-03-07T20:18:48Z")

</div>

Thanks. It works now for GHS Multi generator.

However, for Makefile generator it doesn’t work. The linker script has to be used as link option.

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [March 7, 2020, 9:50pm UTC](https://discourse.cmake.org/t/how-to-add-linker-script-for-ghs-multi-generator/770/8 "2020-03-07T21:50:57Z")

</div>

Then you’ll need to use a condition based on the generator, something like:

```auto
if(CMAKE_GENERATOR STREQUAL "Green Hills MULTI")
  target_sources(<target> PRIVATE "linker_script.ld")
else()
  target_link_options(<target> PRIVATE "linker_script.ld")
endif()

```

---

<div class="post-metadata">

### Author: ![Qichen\_Huang](https://discourse.cmake.org/user_avatar/discourse.cmake.org/qichen_huang/32/416_2.png) [@Qichen\_Huang](https://discourse.cmake.org/u/Qichen_Huang)
#### Post date: [March 8, 2020, 8:41pm UTC](https://discourse.cmake.org/t/how-to-add-linker-script-for-ghs-multi-generator/770/9 "2020-03-08T20:41:29Z")

</div>

Thanks. It works.
