其他分享
首页 > 其他分享> > Step 2: Adding a Library

Step 2: Adding a Library

作者:互联网

参考
参考

1完整代码

	|-step2
	|--CMakeLists.txt
	|--tutorial.cxx
	|--MathFunctions
					|--CMakeLists.txt
					|--mysqrt.h
					|--mysqrt.cxx
	
	
	
	|--CMakeLists.txt
	
		cmake_minimum_required(VERSION 3.10)
		
		project(Tutorial)
		
		# 设置宏变量
		option(USE_MYMATH "Use tutorial provided math implementation" OFF)

		# 根据宏变量的配置,决定是否生成库文件
		if(USE_MYMATH)
		  add_subdirectory(MathFunctions)
		  list(APPEND EXTRA_LIBS MathFunctions)
		  list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/MathFunctions")
		endif()
		
		# 在执行cmake ../build时,会显示message信息
		if(USE_MYMATH)
			message("option USE_MYMATH is ${USE_MYMATH}")
		else()
			message("option USE_MYMATH not define")
		endif()

		#生成可执行文件Tutorial
		add_executable(Tutorial tutorial.cxx)
		
		# 将库文件链接到Tutorial
		target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})
		
		# 添加目录文件所需要的头文件
		target_include_directories(Tutorial PUBLIC 
							${PROJECT_BINARY_DIR}
							${EXTRA_INCLUDES}
							)
			
	
	|--MathFunctions
		|--CMakeLists.txt
	
	cmake_minimum_required(VERSION 3.10)
	
	#递归获取目录下所有的.cxx文件
	#file(GLOB_RECURSE cpp_files ./*.cxx)
			
	# 递归获取目录下所有的h文件
	#file(GLOB_RECURSE h_files ./*.h)
			
	project(MathFunctions)
			
	# 生成默认的静态库MathFunctions,win为MathFunctions.lib, linux为libMathFunctions.a
	add_library(MathFunctions mysqrt.cxx)

2命令学习

生成库文件,可以生成静态、动态等库文件,一般放在某一个子文件中。然后在主CMakeLists.text中,通过使用add_subdirectory来生成。

例如:
## MathFunctions/CMakeLists.txt

	add_library(MathFunctions mysqrt.cxx)

就是生成静态库MathFunctions,即libMathFunctions.a

	## CMakeLists.txt

	# add the MathFunctions library,执行子文件夹MathFunctions下的CMakeLists.txt
	add_subdirectory(MathFunctions)
	
	# add the executable,生成Tutorial可执行文件,源代码为tutorial.cxx
	add_executable(Tutorial tutorial.cxx)
	
	target_link_libraries(Tutorial PUBLIC MathFunctions)
	
	# add the binary tree to the search path for include files
	# so that we will find TutorialConfig.h
	target_include_directories(Tutorial PUBLIC
	                          "${PROJECT_BINARY_DIR}"
	                          "${PROJECT_SOURCE_DIR}/MathFunctions"
	                          )

参考

将被依赖的库放在依赖它的库的后面,上面中表示Tutorial依赖libMathFunctions.so

举例:

	target_link_libraries(myProjectExecutable A B.a C.so)


	在上面的命令中,libA.so可能依赖于libB.a和libC.so,如果顺序有错,链接时会报错。还有一点,B.a会告诉CMake优先使用静态链接库libB.a,C.so会告诉CMake优先使用动态链接库libC.so,也可直接使用库文件的相对路径或绝对路径。使用绝对路径的好处在 于,当依赖的库被更新时,make的时候也会重新链接。

参考

注意区分target_link_librarieslink_libraries的区别,请参考

例如

	set(LINK_DIR  /Users/haoran/Downloads/wfdb/lib)
	link_directories({LINK_DIR})

这样在使用target_link_libraries时,只需要给出动态库的名字就行,如:

	target_link_libraries(myProjectExecutable eng mx)

完整的例子

	cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
	include_directories("/opt/MATLAB/R2012a/extern/include")
	link_directories("/opt/MATLAB/R2012a/bin/glnxa64")
	add_executable(myProject main.cpp)
	target_link_libraries(myProject eng mx)

	#equals to below
	#target_link_libraries(myProject -leng -lmx)
	#target_link_libraries(myProject libeng.so libmx.so)

注意区分target_include_directoriesinclude_directories

标签:Adding,target,MathFunctions,add,Library,libraries,Step,link,so
来源: https://www.cnblogs.com/gwzz/p/15878558.html