cmake 下 readlink 的奇怪行为

strange behaviour with readlink under cmake

我正在尝试使用 readlink -f 获取所有符号链接后的共享库的绝对路径。

>readlink -f /opt/gcc4.9.3/lib64/libstdc++.so.6
/opt/gcc4.9.3/lib64/libstdc++.so.6.0.20

但是当我在 cmake 中执行此操作时,它不会展开完整路径 例如。 set(CPP11_PATH ${CMAKE_CURRENT_BINARY_DIR}/) execute_process(COMMAND ldd ${CPP11_PATH} COMMAND grep libstdc++ COMMAND awk "{ print ; }" OUTPUT_VARIABLE LIBSTDCPP_PATH) message("LIBSTDCPP_PATH=${LIBSTDCPP_PATH}") execute_process(COMMAND readlink -f ${LIBSTDCPP_PATH} OUTPUT_VARIABLE LIBSTDCPP_ABSPATH) message("LIBSTDCPP_ABSPATH=${LIBSTDCPP_ABSPATH}")

打印:

LIBSTDCPP_PATH=/opt/gcc4.9.3/lib64/libstdc++.so.6
LIBSTDCPP_ABSPATH=/opt/gcc4.9.3/lib64/libstdc++.so.6

如果我将其包装在 shell 脚本中,也会发生这种情况: execute_process(COMMAND doreadlink.sh ${LIBSTDCPP_PATH} OUTPUT_VARIABLE LIBSTDCPP_ABSPATH2) message("LIBSTDCPP_ABSPATH2=${LIBSTDCPP_ABSPATH2}")

>LIBSTDCPP_ABSPATH2=/opt/gcc4.9.3/lib64/libstdc++.so.6

它也发生在使用 get_filename_component() 和 REALPATH 时,这应该是执行此操作的规范 cmake 方法。 get_filename_component(LIBSTDCPP_PATH ${LIBSTDCPP_PATH} REALPATH) message("LIBSTDCPP_PATH2=${LIBSTDCPP_PATH}")

谁能解释一下?

我已经尝试了 cmake rebuild_cache 并删除了 CMakeCache.txt 和 运行 --trace 以确保我认为 运行 确实是 运行. 我还确认它发生在 rhel5 rhel6 和 rhel7 以及 cmake 2.8 和 3.4 上。

有一个工作正常的解决方法: execute_process(COMMAND ldd ${CPP11_PATH} COMMAND grep libstdc++ COMMAND awk "{ print ; COMMAND xargs readlink -f }" OUTPUT_VARIABLE LIBSTDCPP_PATH)

cmake 在幕后会做什么?

我尝试了 运行ning: strace -f cmake <code>pwd

部分输出为:

[pid 23195] execve("/usr/lib64/qt-3.3/bin/readlink", ["readlink", "-f", "/lib64/libstdc++.so.6\n"], [/* 53 vars */]) = -1 ENOENT (No such file or directory)
[pid 23195] execve("/usr/local/bin/readlink", ["readlink", "-f", "/lib64/libstdc++.so.6\n"], [/* 53 vars */]) = -1 ENOENT (No such file or directory)
[pid 23195] execve("/usr/bin/readlink", ["readlink", "-f", "/lib64/libstdc++.so.6\n"], [/* 53 vars */] 
[pid 23174]  "", 1)   = 0
[pid 23195]  )      = 0
[pid 23174] close(11)                   = 0
[pid 23174] rt_sigprocmask(SIG_SETMASK, [],  
[pid 23195] brk(0 
[pid 23174]  NULL, 8) = 0
[pid 23195]  )         = 0x12e5000
[pid 23174] read(9, "", 1024)           = 0
[pid 23174] close(9)                    = 0
[pid 23195] mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 
[pid 23174] close(6)                    = 0
[pid 23195]  )        = 0x7ff41654f000
[pid 23174] close(8)                    = 0
[pid 23195] access("/etc/ld.so.preload", R_OK 
[pid 23174] select(8, [3 5 7], NULL, NULL, NULL 
[pid 23195]  )      = -1 ENOENT (No such file or directory)
[pid 23195] open("tls/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[pid 23195] open("tls/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[pid 23195] open("x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[pid 23195] open("libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[pid 23195] open("/usr/local/lib/tls/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[pid 23195] stat("/usr/local/lib/tls/x86_64", 0x7fff36038350) = -1 ENOENT (No such file or directory)
[pid 23195] open("/usr/local/lib/tls/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[pid 23195] stat("/usr/local/lib/tls", 0x7fff36038350) = -1 ENOENT (No such file or directory)
[pid 23195] open("/usr/local/lib/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)

剪断

O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[pid 23195] stat("/usr/lib/oracle/10.2.0.4/client/lib/tls", 0x7fff36038350) = -1 ENOENT (No such file or directory)
[pid 23195] open("/usr/lib/oracle/10.2.0.4/client/lib/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[pid 23195] stat("/usr/lib/oracle/10.2.0.4/client/lib/x86_64", 0x7fff36038350) = -1 ENOENT (No such file or directory)
[pid 23195] open("/usr/lib/oracle/10.2.0.4/client/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
rt_sigprocmask(SIG_BLOCK, [INT TERM CHLD], [], 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
ioctl(0, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
ioctl(0, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
write(2, "LIBSTDCPP_ABSPATH=/usr/lib64/lib"..., 45LIBSTDCPP_ABSPATH=/usr/lib64/libstdc++.so.6

) = 45

所以它甚至看起来像是在执行正确的系统命令。

这里有一个 CMakeLists.txt 来重现问题:

cmake_minimum_required(VERSION 2.8)
project(TEST CXX) 

set(CPP11_PATH ${CMAKE_CURRENT_BINARY_DIR}/cpp11)
execute_process(COMMAND g++ ${CMAKE_CURRENT_SOURCE_DIR}/cpp11.cpp -o${CPP11_PATH})

#execute_process(COMMAND ldd ${CPP11_PATH} COMMAND grep libstdc++ COMMAND awk "{ print ; }" COMMAND xargs readlink -f OUTPUT_VARIABLE LIBSTDCPP_PATH)
execute_process(COMMAND ldd ${CPP11_PATH} COMMAND grep libstdc++ COMMAND awk "{ print ; }" OUTPUT_VARIABLE LIBSTDCPP_PATH)
message("LIBSTDCPP_PATH=${LIBSTDCPP_PATH}")
execute_process(COMMAND readlink -f ${LIBSTDCPP_PATH} OUTPUT_VARIABLE LIBSTDCPP_ABSPATH)
message("LIBSTDCPP_ABSPATH=${LIBSTDCPP_ABSPATH}")

Tsyvarev 做到了。问题是由我的 awk 输出引起的尾随换行符。 如果我将其更改为: awk "print " 到: awk "printf()"

一切正常。万岁!

许多 shell 实用程序输出 单行 以换行符 (\n) 终止。这是为了在终端中获得漂亮的输出。

不同于 backtricks operator (`exec-some-command`) in Linux shell,它会自动去除尾随的换行符,execute_process 默认情况下不这样做。

execute_process 输出中去除尾随换行符的最简单方法是使用 OUTPUT_STRIP_TRAILING_WHITESPACE 选项:

execute_process(COMMAND <...>
    OUTPUT_VARIABLE LIBSTDCPP_PATH
    OUTPUT_STRIP_TRAILING_WHITESPACE
)

有一个更简单的方法。请使用

“execute_process(COMMAND readlink -fn ${SYMLINK_PATH} OUTPUT_VARIABLE FILE_PATH)

-n 或 —-no-newline 表示“不输出结尾的换行符。”