How to add Linker Script for GHS Multi Generator

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

I guess you try using target_link_libraries instead of add_link_options.

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.

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.

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?

List it in the add_executable command with the other source code.

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.

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

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

Thanks. It works.