CMake中link_libraries的使用

Posted fengbingchun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CMake中link_libraries的使用相关的知识,希望对你有一定的参考价值。

      CMake中的link_libraries命令用于将库链接到稍后添加的所有targets.其格式如下:

link_libraries([item1 [item2 [...]]]
               [[debug|optimized|general] <item>] ...)

      指定在通过诸如add_executable或add_library等命令链接稍后在当前目录或更低(below)目录中创建的任何targets时要使用的库或标志。
      A debug, optimized, or general keyword immediately followed by another <item>:这样的关键字后面的item将仅用于相应的构建配置。debug关键字对应于Debug配置。optimized关键字对应于所有其它配置。general关键字对应所有配置,完全是可选的。通过创建和链接到IMPORTED库目标,可以为每个配置规则实现更高的粒度。这些关键字由该命令立即解释,因此在由生成器表达式生成时,它们没有特殊含义。
      注意:应尽可能首选target_link_libraries命令。库依赖项是自动链接的,因此很少需要目录范围(directory-wide)的链接库规范(specification)。
      target_link_libraries的使用参考:https://blog.csdn.net/fengbingchun/article/details/128161746

include_directories($CMAKE_CURRENT_SOURCE_DIR/include)
add_library(add SHARED $CMAKE_CURRENT_SOURCE_DIR/source/add.cpp) # 将会在build目录下生成libadd.so
add_library(subtraction SHARED $CMAKE_CURRENT_SOURCE_DIR/source/subtraction.cpp) # 将会在build目录下生成libsubtraction.so

link_libraries(add subtraction) # 若注释掉此句,则会报 error: sample_add.cpp:(.text+0x25): undefined reference to `add(int, int)'
                                #                          sample_subtraction.cpp:(.text+0x25): undefined reference to `subtraction(int, int)'
                                # 注意: link_libraries位置需要在add_executable前,而target_link_libraries一般放在add_executable之后

add_executable(sample_add $CMAKE_CURRENT_SOURCE_DIR/samples/sample_add.cpp)
add_executable(sample_subtraction $CMAKE_CURRENT_SOURCE_DIR/samples/sample_subtraction.cpp)

      执行测试代码需要多个文件

      build.sh内容如下

#! /bin/bash

# supported input parameters(cmake commands)
params=(function macro cmake_parse_arguments \\
		find_library find_path find_file find_program find_package \\
		cmake_policy cmake_minimum_required project include \\
		string list set foreach message option if while return \\
		math file configure_file \\
		include_directories add_executable add_library link_libraries target_link_libraries install \\
		target_sources add_custom_command add_custom_target \\
		add_subdirectory aux_source_directory \\
		set_property set_target_properties define_property \\
		add_definitions target_compile_definitions target_compile_features \\
		add_compile_options target_include_directories link_directories \\
		add_link_options)

usage()

	echo "Error: $0 needs to have an input parameter"

	echo "supported input parameters:"
	for param in $params[@]; do
		echo "  $0 $param"
	done

	exit -1


if [ $# != 1 ]; then
	usage
fi

flag=0
for param in $params[@]; do
	if [ $1 == $param ]; then
		flag=1
		break
	fi
done

if [ $flag == 0 ]; then
	echo "Error: parameter \\"$1\\" is not supported"
	usage
	exit -1
fi

if [[ ! -d "build" ]]; then
	mkdir build
	cd build
else
	cd build
fi

echo "==== test $1 ===="

# test_set.cmake: cmake -DTEST_CMAKE_FEATURE=$1 --log-level=verbose ..
# test_option.cmake: cmake -DTEST_CMAKE_FEATURE=$1 -DBUILD_PYTORCH=ON ..
cmake -DTEST_CMAKE_FEATURE=$1 ..
# It can be executed directly on the terminal, no need to execute build.sh, for example: cmake -P test_set.cmake
make
# make install # only used in cmake files with install command

      主CMakeLists.txt内容如下:

cmake_minimum_required(VERSION 3.22)
project(cmake_feature_usage)

message("#### current cmake version: $CMAKE_MAJOR_VERSION.$CMAKE_MINOR_VERSION.$CMAKE_PATCH_VERSION")
include(test_$TEST_CMAKE_FEATURE.cmake)
message("==== test finish ====")

      test_link_libraries.cmake内容为上面的所有测试代码段

      另外还包括三个目录:include,source,samples,它们都是非常简单的实现,仅用于测试,如下:

      可能的执行结果如下图所示:

 

      GitHub: https://github.com/fengbingchun/Linux_Code_Test

以上是关于CMake中link_libraries的使用的主要内容,如果未能解决你的问题,请参考以下文章

[cmake][转载]cmake中的link_directories, LINK_LIBRARIES, target_link_libraries的区别

CMake 入门学习3 基本指令宏模块函数

CMake 入门学习3 基本指令宏模块函数

CMake - 构建没有首选 32 位标志的 C#

在 cmake c++ 项目中包含库的首选方法是啥?

CMake命令target_link_libraries链接库的顺序