我注意到我的项目中的cmake生成步骤花费了很长时间,所以我通过strace运行cmake来找出根本原因。我发现cmake试图在错误的位置重复找到内部共享库,这导致了大量不必要的文件系统查找。这是一个简单的项目,说明了问题:
$ ll -R
.:
total 20K
drwxr-xr-x 2 mark mark 4.0K May 25 16:22 alpha
drwxr-xr-x 2 mark mark 4.0K May 25 16:22 beta
drwxr-xr-x 2 mark mark 4.0K May 25 16:42 build
-rw-r--r-- 1 mark mark 185 May 25 16:20 CMakeLists.txt
-rw-r--r-- 1 mark mark 0 May 25 16:16 dummy.cc
drwxr-xr-x 2 mark mark 4.0K May 25 16:22 gamma
./alpha:
total 4.0K
-rw-r--r-- 1 mark mark 0 May 25 16:16 alpha.cc
-rw-r--r-- 1 mark mark 69 May 25 16:20 CMakeLists.txt
./beta:
total 4.0K
-rw-r--r-- 1 mark mark 0 May 25 16:16 beta.cc
-rw-r--r-- 1 mark mark 67 May 25 16:18 CMakeLists.txt
./build:
total 0
./gamma:
total 4.0K
-rw-r--r-- 1 mark mark 35 May 25 16:19 CMakeLists.txt
-rw-r--r-- 1 mark mark 0 May 25 16:16 gamma.cc
alpha.cc,beta.cc,gamma.cc和dummy.cc都是空的cc文件。以下是所有CMakLists.txt文件的内容:
顶级CMakeLists.txt
$ cat CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
add_subdirectory(alpha)
add_subdirectory(beta)
add_subdirectory(gamma)
add_executable(dummy_exec dummy.cc)
target_link_libraries(dummy_exec alpha)
alpha / CMakeLists.txt
$ cat alpha/CMakeLists.txt
add_library(alpha SHARED alpha.cc)
target_link_libraries(alpha beta)
的β/的CMakeLists.txt
$ cat beta/CMakeLists.txt
add_library(beta SHARED beta.cc)
target_link_libraries(beta gamma)
gamma / CMakeLists.txt
$ cat gamma/CMakeLists.txt
add_library(gamma SHARED gamma.cc)
我按如下方式调用cmake(通过strace):
$ cd build/
$ strace -f -o /tmp/s.out cmake ..
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/mark/Downloads/cmake/build
当我检查strace输出时,我看到它试图在无效位置(例如beta /和gamma / sub目录下)访问libalpha:
31430 access("/home/mark/Downloads/cmake/build/beta/libalpha.so", R_OK) = -1 ENOENT (No such file or directory)
31430 access("/home/mark/Downloads/cmake/build/gamma/libalpha.so", R_OK) = -1 ENOENT (No such file or directory)
同样,它试图在无效位置访问libbeta和libgamma:
31430 access("/home/mark/Downloads/cmake/build/alpha/libbeta.so", R_OK) = -1 ENOENT (No such file or directory)
31430 access("/home/mark/Downloads/cmake/build/gamma/libbeta.so", R_OK) = -1 ENOENT (No such file or directory)
31430 access("/home/mark/Downloads/cmake/build/alpha/libgamma.so", R_OK) = -1 ENOENT (No such file or directory)
31430 access("/home/mark/Downloads/cmake/build/beta/libgamma.so", R_OK) = -1 ENOENT (No such file or directory)
对于具有许多共享库和许多依赖项的大型项目,这些无效的查找会加起来,并且似乎在makefile生成步骤中造成了很大的延迟。有关为什么会发生这种情况以及如何阻止cmake在这些无效路径中进行搜索的任何想法?