返回介绍

15.3 检测所需的链接和依赖关系

发布于 2025-05-06 21:46:01 字数 1587 浏览 0 评论 0 收藏

现在已经生成了所有文件,让我们重新构建。我们应该能够配置和编译源代码,不过不能链接:

$ mkdir -p build
$ cd build
$ cmake ..
$ cmake --build .
​
...
Scanning dependencies of target vim
[ 98%] Building C object src/CMakeFiles/vim.dir/main.c.o
[100%] Linking C executable ../bin/vim
../lib64/libbasic_sources.a(term.c.o): In function `set_shellsize.part.12':
term.c:(.text+0x2bd): undefined reference to `tputs'
../lib64/libbasic_sources.a(term.c.o): In function `getlinecol':
term.c:(.text+0x902): undefined reference to `tgetent'
term.c:(.text+0x915): undefined reference to `tgetent'
term.c:(.text+0x935): undefined reference to `tgetnum'
term.c:(.text+0x948): undefined reference to `tgetnum'
... many other undefined references ...

同样,可以从 Autotools 编译中获取日志文件,特别是链接行,通过在 src/CMakeLists.txt 中添加以下代码来解决缺少的依赖关系:

# find X11 and link to it
find_package(X11 REQUIRED)
if(X11_FOUND)
  target_link_libraries(vim
    PUBLIC
        ${X11_LIBRARIES}
    )
endif()
​
# a couple of more system libraries that the code requires
foreach(_library IN ITEMS Xt SM m tinfo acl gpm dl)
  find_library(_${_library}_found ${_library} REQUIRED)
  if(_${_library}_found)
    target_link_libraries(vim
      PUBLIC
        ${_library}
      )
  endif()
endforeach()

我们可以添加一个库的依赖目标,并且不需要构建,以及不需要将库目标放在一个列表变量中,否则将破坏 CMake 代码的自变量,特别是对于较大的项目而言。

修改之后,编译和链接:

$ cmake --build .
​
...
Scanning dependencies of target vim
[ 98%] Building C object src/CMakeFiles/vim.dir/main.c.o
[100%] Linking C executable ../bin/vim
[100%] Built target vim

现在,我们可以执行编译后的二进制文件,我们新编译的 Vim 就可使用了!

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。