CMake引用自定义外部库


参考:https://www.bbsmax.com/A/ZOJPNNEadv/


# 指定库头文件所在路径
FIND_PATH(calc_INCLUDE_DIR calc_interface.h ${CMAKE_CURRENT_SOURCE_DIR}/ext/calc/include)
# 指定库文件所在路径
FIND_LIBRARY(calc_LIBRARY calc.lib ${CMAKE_CURRENT_SOURCE_DIR}/ext/calc/lib)
# 为了下游可以继续使用
set(calc_FOUND FALSE)
if (calc_INCLUDE_DIR AND calc_LIBRARY)
    set(calc_FOUND TRUE)
endif()

-----------------------------------------------------


# 指定动态库头文件路径
target_include_directories(${PROJECT_NAME} PUBLIC ${calc_INCLUDE_DIR})

# 连接动态库
target_link_libraries(${PROJECT_NAME} PUBLIC ${calc_LIBRARY})

# 拷贝动态库到可执行目录下
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        "${CMAKE_CURRENT_SOURCE_DIR}/ext/calc/dll/$<CONFIGURATION>"
        $<TARGET_FILE_DIR:${PROJECT_NAME}>)

-----------------------------------------------------

posted @ 2023-01-31 11:40  ParamousGIS  阅读(219)  评论(0编辑  收藏  举报