Relative path issue in CMAKE

I have developed a library to a target for my project where i am facing an intresting issue related to relative path of CMAKE. During QNX debug build, linker is trying to link lib as absolute path instead of relative path.
Ex:
[100%] Linking CXX executable SampleMain
cd D:/Repo/build-CMake_Sample_inclib-HMI_Qnx-Debug/SampleApp && C:/qnx700/host/win64/x86_64/usr/bin/qcc.exe -Vgcc_ntoaarch64le -lang-c++ -g -g “CMakeFiles/SampleMain.dir/src/main.cpp.o” -o SampleMain -Wl,-rpath,D:/Repo/CMake_Sample_inclib/inclib/lib D:/Repo/CMake_Sample_inclib/inclib/lib/libincrtos.so

The expectation is
[100%] Linking CXX executable SampleMain
cd D:/Repo/build-CMake_Sample_inclib-HMI_Qnx-Debug/SampleApp && C:/qnx700/host/win64/x86_64/usr/bin/qcc.exe -Vgcc_ntoaarch64le -lang-c++ -g -g “CMakeFiles/SampleMain.dir/src/main.cpp.o” -o SampleMain -Wl,-rpath,…/…/CMake_Sample_inclib/inclib/lib /lib/libincrtos.so

Can you show the CMake code that adds the -Wl,-rpath flag?

Thanks for the reply. I will share the code soon…

The below lines of code i have used to set the flag.
Could you please let me know any other things to be added…

set(CMAKE_SKIP_RPATH TRUE CACHE BOOL "If set, runtime paths are not added when using shared libraries.")

SET(CMAKE_BUILD_RPATH_USE_ORIGIN TRUE)

SET(CMAKE_SKIP_BUILD_RPATH FALSE)

SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)

SET(CMAKE_INSTALL_RPATH "")

set(CMAKE_BUILD_WITH_INSTALL_RPATH OFF)

set(CMAKE_VERBOSE_MAKEFILE ON)

set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")

set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

SET(CMAKE_INSTALL_RPATH $ORIGIN)

SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")

You’re overwriting variables all over the place here. You code is the same as:

set(CMAKE_SKIP_RPATH TRUE CACHE BOOL "If set, runtime paths are not added when using shared libraries.")
set(CMAKE_BUILD_RPATH_USE_ORIGIN TRUE)
set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH OFF)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")

${CMAKE_INSTALL_PREFIX} is almost always going to be an absolute path, so that’s one possible location for the absolute path to come from, though your settings seem to not be using that so much. I would suggest setting CMAKE_BUILD_WITH_INSTALL_RPATH to ON, making the build tree layout look like your install tree and using an install rpath of $ORIGIN/../lib (or something like it).

Our Build tree layout and install tree layout do not match.

Is the recommendation to have same build tree structure as install tree ?

Is it mandatory?

With the below code, able to get relative path in CMAKE linker stage as shown below.
set_property(
** TARGET inclib**
** PROPERTY IMPORTED_NO_SONAME 1**
** )**
Is it direct solution or do we have any deviation in the functionality?

OUTPUT with the above code:

[100%] Linking CXX executable SampleMain

cd D:/Repo/build-CMake_Sample_inclib-HMI_Qnx-Debug/SampleApp && C:/qnx700/host/win64/x86_64/usr/bin/qcc.exe -Vgcc_ntoaarch64le -lang-c++ -g -g “CMakeFiles/SampleMain.dir/src/main.cpp.o” -o SampleMain -LD:/Repo/CMake_Sample_inclib/inclib/lib -Wl,-rpath,D:/Repo/CMake_Sample_inclib/inclib/lib -lincrtos

make.exe[2]: Leaving directory ‘D:/Repo/build-CMake_Sample_inclib-HMI_Qnx-Debug’

[100%] Built target SampleMain

make.exe[1]: Leaving directory ‘D:/Repo/build-CMake_Sample_inclib-HMI_Qnx-Debug’

C:/cmake/bin/cmake.exe -E cmake_progress_start D:/Repo/build-CMake_Sample_inclib-HMI_Qnx-Debug/CMakeFiles 0

17:36:38: The process “C:\cmake\bin\cmake.exe” exited normally.

17:36:38: Elapsed time: 00:01.

I’d recommend it; it makes your RPATH testing actually useful. It’s not mandatory since CMake has mechanisms to make different RPATH entries for the build and install trees, but, IMO, that just doubles the amount of logic you need to do.

I’m not familiar with this property or its behaviors. It might be the answer or it might just be a side effect. @brad.king?

ok. I will check its behavior. Thanks for the reply