Windows上的CMake:如何查找和链接不同的库以进行发布和调试

时间:2019-09-17 06:36:15

标签: windows cmake

我正在将代码从Linux移植到Windows。在Windows上,我需要链接到发布模式的libqwt.a和调试模式的libqwtd.a

IF OS==Windows AND CMAKE_BUILD_TYPE==Debug
    I'm trying to have find_library() find libqwtd.a
ELSE
    find libqwt.a

但是它只能找到libqwt.a

这是CMake代码:

include_directories("${QWT_DIR}/include")
set(QWT_LIB_NAME qwt)
if (WIN32)
    message(STATUS "OS=windows")
    if (${CMAKE_BUILD_TYPE} STREQUAL "Debug")
        set(QWT_LIB_NAME qwtd)
        set(CMAKE_FIND_LIBRARY_SUFFIXES "d.a")
        message(STATUS "Build=debug, Qwt lib="${QWT_LIB_NAME})
    endif (${CMAKE_BUILD_TYPE} STREQUAL "Debug")
endif (WIN32)
message(STATUS "Searching Qwt lib="${QWT_LIB_NAME})
find_library(LIB_QWT ${QWT_LIB_NAME} PATHS "${QWT_DIR}/lib" NO_DEFAULT_PATH)
message(STATUS "LIB_QWT=${LIB_QWT}")
target_link_libraries(${PROJECT_NAME} "${LIB_QWT}")

这是输出:

QWT_DIR=C:/lib/Qwt-6.1.4
OS=windows
Build=debug, Qwt lib=qwtd
Searching Qwt lib=qwtd
LIB_QWT=C:/lib/Qwt-6.1.4/lib/libqwt.a

1 个答案:

答案 0 :(得分:0)

正如Tsyvarev所指出的,我需要在构建目录中删除CMakeCache.txt。因此,现在,用于处理Windows(调试和发行版)和Linux构建的代码缩减为:

include_directories("${QWT_DIR}/include")
set(QWT_NAME qwt)
if ( (WIN32) AND (${CMAKE_BUILD_TYPE} STREQUAL "Debug") )
    set(QWT_NAME qwtd)
endif ( (WIN32) AND (${CMAKE_BUILD_TYPE} STREQUAL "Debug") )

find_library(LIB_QWT ${QWT_NAME} PATHS "${QWT_DIR}/lib" NO_DEFAULT_PATH)
message(STATUS "Found LIB_QWT=${LIB_QWT}")
target_link_libraries(${PROJECT_NAME} "${LIB_QWT}")