为啥cmake没有在makefile中添加库链接命令?

Posted

技术标签:

【中文标题】为啥cmake没有在makefile中添加库链接命令?【英文标题】:Why does cmake not add a library link command in the makefile?为什么cmake没有在makefile中添加库链接命令? 【发布时间】:2015-07-15 15:17:03 【问题描述】:

我是 Ubuntu,我正在学习 cmake 和 make,只是尝试一个简单的例子。我有两个目录:srcbuild。在src 中,我有两个文件:main.cpp,只是一个简单的 hello world 示例,和CMakeLists.txt,带有以下文本:

add_executable(test main.cpp)
target_link_libraries(test blas)

这里,blas 只是我在/usr/lib/libblas.so 找到的一些任意库。我的main.cpp 文件实际上并没有使用它,但我只是在测试......

现在,如果我进入build 目录,并运行cmake ../src,则在build 中生成一个makefile。该 makefile 的内容粘贴在下面。

我感到困惑的是,为什么根本没有提到blas 库? cmake不应该添加-lblas之类的行吗?

谢谢:)

# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 2.8

# Default target executed when no arguments are given to make.
default_target: all
.PHONY : default_target

#=============================================================================
# Special targets provided by cmake.

# Disable implicit rules so canonical targets will work.
.SUFFIXES:

# Remove some rules from gmake that .SUFFIXES does not remove.
SUFFIXES =

.SUFFIXES: .hpux_make_needs_suffix_list

# Suppress display of executed commands.
$(VERBOSE).SILENT:

# A target that is always out of date.
cmake_force:
.PHONY : cmake_force

#=============================================================================
# Set environment variables for the build.

# The shell in which to execute make rules.
SHELL = /bin/sh

# The CMake executable.
CMAKE_COMMAND = /usr/bin/cmake

# The command to remove a file.
RM = /usr/bin/cmake -E remove -f

# Escaping for special characters.
EQUALS = =

# The program to use to edit the cache.
CMAKE_EDIT_COMMAND = /usr/bin/cmake-gui

# The top-level source directory on which CMake was run.
CMAKE_SOURCE_DIR = /home/ejohns/Projects/Test2/src

# The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = /home/ejohns/Projects/Test2/build

#=============================================================================
# Targets provided globally by CMake.

# Special rule for the target edit_cache
edit_cache:
    @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..."
    /usr/bin/cmake-gui -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : edit_cache

# Special rule for the target edit_cache
edit_cache/fast: edit_cache
.PHONY : edit_cache/fast

# Special rule for the target rebuild_cache
rebuild_cache:
    @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..."
    /usr/bin/cmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : rebuild_cache

# Special rule for the target rebuild_cache
rebuild_cache/fast: rebuild_cache
.PHONY : rebuild_cache/fast

# The main all target
all: cmake_check_build_system
    $(CMAKE_COMMAND) -E cmake_progress_start /home/ejohns/Projects/Test2/build/CMakeFiles /home/ejohns/Projects/Test2/build/CMakeFiles/progress.marks
    $(MAKE) -f CMakeFiles/Makefile2 all
    $(CMAKE_COMMAND) -E cmake_progress_start /home/ejohns/Projects/Test2/build/CMakeFiles 0
.PHONY : all

# The main clean target
clean:
    $(MAKE) -f CMakeFiles/Makefile2 clean
.PHONY : clean

# The main clean target
clean/fast: clean
.PHONY : clean/fast

# Prepare targets for installation.
preinstall: all
    $(MAKE) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall

# Prepare targets for installation.
preinstall/fast:
    $(MAKE) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall/fast

# clear depends
depend:
    $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
.PHONY : depend

#=============================================================================
# Target rules for targets named test

# Build rule for target.
test: cmake_check_build_system
    $(MAKE) -f CMakeFiles/Makefile2 test
.PHONY : test

# fast build rule for target.
test/fast:
    $(MAKE) -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/build
.PHONY : test/fast

main.o: main.cpp.o
.PHONY : main.o

# target to build an object file
main.cpp.o:
    $(MAKE) -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/main.cpp.o
.PHONY : main.cpp.o

main.i: main.cpp.i
.PHONY : main.i

# target to preprocess a source file
main.cpp.i:
    $(MAKE) -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/main.cpp.i
.PHONY : main.cpp.i

main.s: main.cpp.s
.PHONY : main.s

# target to generate assembly for a file
main.cpp.s:
    $(MAKE) -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/main.cpp.s
.PHONY : main.cpp.s

# Help Target
help:
    @echo "The following are some of the valid targets for this Makefile:"
    @echo "... all (the default if no target is provided)"
    @echo "... clean"
    @echo "... depend"
    @echo "... edit_cache"
    @echo "... rebuild_cache"
    @echo "... test"
    @echo "... main.o"
    @echo "... main.i"
    @echo "... main.s"
.PHONY : help



#=============================================================================
# Special targets to cleanup operation of make.

# Special rule to run CMake to check the build system integrity.
# No rule that depends on this can have commands that come from listfiles
# because they might be regenerated.
cmake_check_build_system:
    $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
.PHONY : cmake_check_build_system

【问题讨论】:

如果包含相关的头文件/等。 cmake 会更改输出生成文件吗? cmake 是否会发现您实际上并不需要该库并忽略它? 如果您尝试调试生成的makefile,您可以运行make VERBOSE=1 并查看cmake 生成的命令是什么 【参考方案1】:

CMake 使用 3 级层次结构的 makefile。您要查找的行将位于CMakeFiles/Makefile2build.makeCMakeFiles 下的某个位置(也可能是test 目录)。

【讨论】:

以上是关于为啥cmake没有在makefile中添加库链接命令?的主要内容,如果未能解决你的问题,请参考以下文章

如何让 CMake 在任何地方自动添加 MagickWand 库链接

CMAKE 如何告诉 makefile 链接库?

用于链接OpenCV和现有库的Makefile(不使用cmake)

为啥 CMake 在没有 CMAKE_CXX_FLAGS 的情况下生成 Makefile?

cmake利用toolchain.cmake生成makefile之后,make生成静态库失败问题

为啥 CMake 没有与我的自定义库(add_library/set_property)链接?