`add_compile_options` isn't adding options that start with `-l`

Hi all,

I’m attempting to modify a CMake file that is used to build for many platforms. The platform that I am specifically targeting to change is GHS (Green Hills Software). So I modified this:

        add_compile_options(
            -fno-omit-frame-pointer
        )

to this:

        if (GHSMULTI)
            add_compile_options(
              -lmulti    # Be able to call functions from within MULTI
              -gtws      # Generate Target Walkable Stack
              -ga        # Force frame pointer
            )
        endif (GHSMULTI)

because the GHS compiler doesn’t understand -fno-omit-frame-pointer. It kinda worked in that -fno-omit-frame-pointer disappeared and was replaced by -gtws and -ga. However, -lmulti didn’t show up. This was verified by used Process Explorer and randomly selecting a build process and looking at the command line’s argument list.

I further tried to add to the add_compile_options the following switches: -lnk=-wrap=malloc -lnk=-wrap=realloc -lnk=-wrap=calloc -lnk=-wrap=free. but none showed up anywhere.

What am I missing?

Just on the off chance that it had something to do with the switch starting with -l, I just added the letter a in front of the l and it did show up as an error:

Warning: Found unknown option -alnk=-wrap=calloc in file.cpp

So, the question is, is CMake doing something to “help” me and if so, how to I tell it to stop being so “helpful”?

Oh, btw, the version I’m using is cmake version 3.21.1, but the first line in our files show cmake_minimum_required(VERSION 3.1...3.10).

EDIT:

Attempted to use

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-wrap=malloc,-wrap=realloc,-wrap=calloc,-wrap=free")

But that didn’t work either.

Hey Jack,
I have no experience with GHS but my guess is that -l<lib> flags are filtered out by add_compile_options as they are supposed to be added with link_libraries so you could give link_libraries(multi) a try.

Yeah, this is probably it. We probably need a COMPILER: prefix to say that a flag is for the compiler (even if it looks like a link flag)

  • -lmulti
  • -Wlove-me-do (or some other l-starting warning name)

This is worth an issue by the way.

This seems to be a GHS specific problem as I can add these flags fine with make files. As I don’t have GHS I can’t create/test a reprex for the issue :frowning: Maybe @Jack can open one?

I checked in the CMake v3.23.0 and in the nightly build and cannot reproduce the issue with the add_compile_options command.

I tried:
add_compile_options(-lmulti -Wl,-wrap=malloc,-wrap=realloc,-wrap=calloc,-wrap=free)
add_compile_options(-lmulti -lnk=wrap=malloc -lnk=wrap=realloc -lnk=wrap=calloc -lnk=wrap=free)

The project files produced include the -lmulti, -Wl and the -lnk option. Running gbuild with the -commands shows the options being used during the build process.

FYI for others, -lmulti is used to link an executable against the libmulti.a library. Typically what I’ve seen, instead of using -lmulti, is to compile an executable with -G which should automatically add libmulti.a during the link phase.

This is the output from gbuild when it does not recognize a driver option during the build process. I’m not aware of any way to turn off these warnings.