1

I'm trying to build a library for CLang compiler with CMake. I now need to pass it some linker variables. I'm looking at an example which was done for some other architecture and I see the person who wrote that used a variable named 'CMAKE_C_LINK_EXECUTABLE' like this:

set(CMAKE_C_LINK_EXECUTABLE   "<CMAKE_C_COMPILER>   --run_linker <OBJECTS>
<CMAKE_C_LINK_FLAGS>   <LINK_FLAGS> --map_file=<TARGET>.map
--output_file=<TARGET> <LINK_LIBRARIES>")

Now I suppose that in order to add linker options I need to append the flags to the 'CMAKE_C_LINK_FLAGS' variable before I set the 'CMAKE_C_LINK_EXECUTABLE' variable. Something like this:

set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS } --myFlag")

Please tell me if that's the proper way to set linker options. Also I have searched around for the 'CMAKE_C_LINK_EXECUTABLE' variable but I did not quite understand what it does. Can you please explain to me how this variable is used? Thank you for reading!

Kennedy
  • 277
  • 2
  • 7
  • 21
  • 1
    If you want to just **add** some **linker flags**, see that question: https://stackoverflow.com/questions/11783932/how-to-add-linker-or-compile-flag-in-cmake-file. If you want to change **linking rule** (for support new platform or new compiler), then modify `CMAKE_C_LINK_EXECUTABLE` variable. Its partial description you may find in [wiki](https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/Build-Rules). So, what exactly is your problem? Please, do not ask unrelated questions in single question post. – Tsyvarev May 07 '18 at 09:12
  • my problem is that I want to add some linker flags and I was not sure if I had to also make use of that 'CMAKE_C_LINK_EXECUTABLE' variable or not – Kennedy May 07 '18 at 09:57
  • You needn't to use `CMAKE_C_LINK_EXECUTABLE` variable for add linker flags. Just see the question I have noted in my previous comment. – Tsyvarev May 07 '18 at 10:46
  • Possible duplicate of [How to add linker or compile flag in cmake file?](https://stackoverflow.com/questions/11783932/how-to-add-linker-or-compile-flag-in-cmake-file) – Tsyvarev May 07 '18 at 10:47
  • thank you @Tsyvarev. I have added the linker options by setting them to the 'CMAKE_EXE_LINKER_FLAGS' variable in a toolchain file. I hope that is correct. Should I add the compiler flags in the toolchain as well or can they be added in another cmake file? I have read in an answer that you should always pass linker flags via a toolchain. Is the same true for compiler options? – Kennedy May 08 '18 at 07:21
  • 1
    If some compiler flags are needed for **every project**, which is built for *specific platform*, these flags should be added in the toolchain file. Otherwise, let projects themselves to decide, whether they need these flags or not. (That is, do not add these flags in the toolchain file). – Tsyvarev May 08 '18 at 08:09

0 Answers0