libm is treated differently when full path is passed to target_link_libraries
Add the following code to CMakeLists.txt
:
cmake_minimum_required(VERSION 3.16)
cmake_policy(VERSION 3.16)
project(abc VERSION 0.1 LANGUAGES C)
add_executable(abc_bin b.c)
target_link_libraries(abc_bin "/usr/lib/x86_64-linux-gnu/libpcre.so")
Now run cmake .
followed by VERBOSE=1 cmake --build .
. The linking step shows that the command for linking is
/usr/bin/cc CMakeFiles/abc_bin.dir/b.c.o -o abc_bin /usr/lib/x86_64-linux-gnu/libpcre.so
This is correct. But if I change the path to libpcre.so to libm.so (/usr/lib/x86_64-linux-gnu/libm.so
), the linking command would be changed to:
/usr/bin/cc CMakeFiles/abc_bin.dir/b.c.o -o abc_bin -lm
Note that the full path to libm.so is lost. This can be unexpected when one wants to link a custom libm build.
I tested this on current master.
Edited by Hong Xu