LINK_DIRECTORIES 命令来指定第三方库所在路径,比如,你的动态库在/home/myproject/libs这个路径下,则通过命令:LINK_DIRECTORIES(/home/myproject/libs),把该路径添加到第三方库搜索路径中,这样就可以使用相对路径了,使用TARGET_LINK_LIBRARIES的时候,只需要给出动态链接库的名字就行了。
也可以set(LINK_DIR /Users/haoran/Downloads/wfdb/lib),LINK_DIRECTORIES({LINK_DIR})
官方不建议使用该命令,取而代之的为find_package() find_library()
Note that this command [link_directories] is rarely necessary. Library locations returned by find_package() and find_library() are absolute paths. Pass these absolute library file paths directly to the target_link_libraries() command. CMake will ensure the linker finds them.
本文介绍了在CMake中如何使用LINK_DIRECTORIES命令来指定第三方库路径,以便于链接动态库。虽然这种方法被官方不推荐,通常建议使用find_package()和find_library()函数来寻找库的绝对路径,并直接传递给target_link_libraries()命令。这样做可以确保链接器找到正确的库文件。注意,使用LINK_DIRECTORIES可能带来一些问题,因此最佳实践是遵循官方推荐的方法。
1115

被折叠的 条评论
为什么被折叠?



