cmake find_path不适用于Windows

时间:2014-06-20 16:53:07

标签: cmake

在我的第一个cmake项目中,当我试图引用包libcurl时,看起来find_path在windows上不起作用。我正在使用cmake 2.8.12.2。

我看了一下FindCURL.cmake源代码,并在下面尝试过:

if(WIN32)
set(CMAKE_PREFIX_PATH 
    "${WIN32_LIBS_DIR}/libcurl" 
    "${WIN32_LIBS_DIR}/libcurl/include" 
    "${WIN32_LIBS_DIR}/libcurl/lib"
    )
find_path(CURL_INCLUDE_DIR NAMES curl/curl.h)   # this line is copied from the first line of FindCURL.cmake source

    message("WIN32_LIBS_DIR: ${WIN32_LIBS_DIR}")
message("thu ${CURL_INCLUDE_DIR}")

并获得

的结果
WIN32_LIBS_DIR: ../../../lwqq_root/win32-dev
"thu CURL_INCLUdE_DIR_NOTFOUND", 

顺便说一句,变量WIN32_LIBS_DIR设置为win32-dev,如下所示:

└─win32-dev
   ├─libcurl
   │  ├─bin
   │  ├─include
   │  │  └─curl
   │  │     └─curl.h
   │  ├─lib
   │  │  └─pkgconfig
   │  └─samples

任何想法为什么?

1 个答案:

答案 0 :(得分:0)

使用 CURL 的现代方法是使用由 FindCURL 创建的导入目标并使用 CURL_ROOT 引导搜索,如下所示:

set(CURL_ROOT "${WIN32_LIBS_DIR}")
find_package(CURL REQUIRED)

add_executable(my_target main.cpp)
target_link_libraries(my_target PRIVATE CURL::libcurl)

见:https://cmake.org/cmake/help/latest/module/FindCURL.html


您的问题没有包含足够的细节来重现您的问题。这是我所做的尝试:

cmake_minimum_required(VERSION 3.20)
project(test LANGUAGES NONE)

# Create the directory structure in the question:
file(MAKE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/win32-dev/libcurl/bin")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/win32-dev/libcurl/include/curl")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/win32-dev/libcurl/lib/pkgconfig")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/win32-dev/libcurl/samples")

file(WRITE "${CMAKE_CURRENT_SOURCE_DIR}/win32-dev/libcurl/include/curl/curl.h" "")

# Set WIN32_LIBS_DIR to the root just created
set(WIN32_LIBS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/win32-dev")

# Your code:
set(
  CMAKE_PREFIX_PATH
  "${WIN32_LIBS_DIR}/libcurl" 
  "${WIN32_LIBS_DIR}/libcurl/include" 
  "${WIN32_LIBS_DIR}/libcurl/lib"
)

find_path(CURL_INCLUDE_DIR NAMES curl/curl.h)

message(STATUS "WIN32_LIBS_DIR: ${WIN32_LIBS_DIR}")
message(STATUS "CURL_INCLUDE_DIR ${CURL_INCLUDE_DIR}")

这是在带有 CMake 3.20 的 Windows 上运行的,但我确信它可以返回。我在 Linux 上使用 3.0 和 3.20 之间的每个 CMake 版本对此进行了测试,并且它适用于所有

这是我在终端中看到的:

D:\test>cmake -S . -B build
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.19043.
-- WIN32_LIBS_DIR: D:/test/win32-dev
-- CURL_INCLUDE_DIR D:/test/win32-dev/libcurl/include
-- Configuring done
-- Generating done
-- Build files have been written to: D:/test/build

D:\test>tree /F win32-dev
Folder PATH listing for volume Sources
Volume serial number is 0000008F 943F:828D
D:\TEST\WIN32-DEV
└───libcurl
    ├───bin
    ├───include
    │   └───curl
    │           curl.h
    │
    ├───lib
    │   └───pkgconfig
    └───samples

正如您从输出中看到的,我已经完全地镜像了您的目录布局,并且其他变量的相同设置允许 find_path 成功,即使在 Windows 上也是如此。