androidc++cmakelinker

CMake: link options


I have an Android CMakeList.txt that defines multiple target libraries (static and dynamic).

For one of these libraries I need to pass the -Wl,--version-script with a specific version script, to achieve this I'm using target_link_libraries:

target_link_libraries( # Specifies the target library.
        native-lib1
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib}
        -Wl,--version-script=${CMAKE_SOURCE_DIR}/../../../../../../native1.map
        )

Unfortunately the -Wl option is added to all the other libraries I'm targeting causing me any sort of linking issues because native1.map refers to the functions in native-lib1 only.

I've also tried to use target_link_options and add_link_options but Android Studio returns error saying these are not valid CMake commands.

Is there a way to define linker options for a single library targeted in CMakeList.txt?


Solution

  • I assume the option is treated as PUBLIC here. In general I recommend always specifying the "visibility" when using target_*** commands.

    In this case I'd recommend using target_link_options though, since this states the intention more clearly.

    target_link_libraries( # Specifies the target library.
            native-lib1
            # Links the target library to the log library
            # included in the NDK.
            ${log-lib}
            )
    target_link_options(native-lib1
        PRIVATE 
            "-Wl,--version-script=${CMAKE_SOURCE_DIR}/../../../../../../native1.map"
    )