4

I'm using CMake with the TI compiler (cl6x). The goal is to create a shared object. The compiler is used correctly to create object files, but during linking, CMake defaults to using the compiler (cl6x) to combine all object files into one binary.

cl6x    -shared -Wl,-soname,libwhatever.so -o libwhatever.so CMakeFiles/whatever.dir/__/whatever.c.o

While this is fine with gcc, in this case, I require a call to lnk6x instead.

I use add_library(whatever SHARED ${whatever_srcs}) to generate the *.so. I also set:

set(CMAKE_LINKER "path/to/lnk6x")
set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_LINKER> <FLAGS> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")

And I issue:

cmake .. -DCMAKE_C_COMPILER=cl6x -DCMAKE_SYSTEM_NAME=Generic 

Short of issuing a custom command, is there a way around this?

MarkP
  • 4,168
  • 10
  • 43
  • 84
  • 1
    `Short of issuing a custom command, is there a way around this?` - It is not clear what do you want, could you elaborate? Do you want to pass linker to `cmake` as you do with compiler? BTW, setting `CMAKE_SYSTEM_NAME` in the command line looks *suspicious*. Normally this is done in **toolchain file**. – Tsyvarev Sep 11 '17 at 16:48
  • 1
    I'd like cmake to call the linker instead of using the compiler front-end as a way to talk to the linker. – MarkP Sep 11 '17 at 18:07
  • You set `CMAKE_C_LINK_EXECUTABLE` variable for force CMake to use the linker. CMake cannot use linker automatically for any compiler, because .. it simply doesn't know which linker to use. This can be achived by writting platform scripts used for compiler detection ... but this would be much more complex than you have. For me, setting `CMAKE_C_LINK_EXECUTABLE` in `CMakeLists.txt` and allowing the user to pass `CMAKE_LINKER` option to the `cmake` is the best way. – Tsyvarev Sep 11 '17 at 19:14
  • 1
    Sure, that seems logical. But cmake still doesn't execute `CMAKE_C_LINK_EXECUTABLE`. By adding in `CMAKE_SYSTEM_NAME=Generic`, cmake is able to cross-compile and use the TI tools. It calls `ar6x` after all object files have been created. Without it, it defaults to using the system's `/usr/bin/ar`. – MarkP Sep 12 '17 at 14:01
  • 2
    Variable `CMAKE_C_LINK_EXECUTABLE` is used only when create an **executable**. For create a **shared library** [CMAKE_C_CREATE_SHARED_LIBRARY](https://cmake.org/cmake/help/v3.9/variable/CMAKE_LANG_CREATE_SHARED_LIBRARY.html) variable is used. – Tsyvarev Sep 12 '17 at 16:44
  • Did you found a solution to your problem? – sergej Nov 26 '19 at 18:26

0 Answers0