cmake在windows上查找sql​​ite3库 [英] cmake find sqlite3 library on windows

查看:387
本文介绍了cmake在windows上查找sql​​ite3库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有更多的麻烦,我希望让CMake找到 sqlite3.dll 在Windows 7(64位,如果这很重要)库。我已经下载并将最新的 sqlite3.dll sqlite3.def 文件放置到 C: \Windows \System32 。我使用 FindSqlite3.cmake 模块:

  IF(SQLITE3_INCLUDE_DIR和SQLITE3_LIBRARY_RELEASE和SQLITE3_LIBRARY_DEBUG)
SET(SQLITE3_FIND_QUIETLY TRUE)
ENDIF(SQLITE3_INCLUDE_DIR和SQLITE3_LIBRARY_RELEASE和SQLITE3_LIBRARY_DEBUG)

FIND_PATH(SQLITE3_INCLUDE_DIR sqlite3.h)

FIND_LIBRARY (SQLITE3_LIBRARY_RELEASE名字sqlite3的)

FIND_LIBRARY(SQLITE3_LIBRARY_DEBUG名字sqlite3的sqlite3d HINTS / usr / lib目录/调试/ usr / lib目录/ C:/在Windows / System32下/)

如果(SQLITE3_LIBRARY_RELEASE或SQLITE3_LIBRARY_DEBUG和SQLITE3_INCLUDE_DIR)
SET(SQLITE3_FOUND TRUE)
ENDIF(SQLITE3_LIBRARY_RELEASE或SQLITE3_LIBRARY_DEBUG和SQLITE3_INCLUDE_DIR)

如果(SQLITE3_LIBRARY_DEBUG和SQLITE3_LIBRARY_RELEASE)
#如果发电机支持配置类型然后设置
#优化和调试库,或者如果CMAKE_BUILD_TYPE具有值
如果(CMAKE_CONFIGURATION_TYPES OR CMAKE_BUILD_TYPE)
设置(SQLITE3_LIBRARIES优化$ {SQLITE3_LIBRARY_RELEASE}调试$ {SQLITE3_LIBRARY_DEBUG})
ELSE(CMAKE_CONFIGURATION_TYPES OR CMAKE_BUILD_TYPE)
#如果没有配置类型和CMAKE_BUILD_TYPE没有价值
#就用释放库
SET(SQLITE3_LIBRARIES $ {SQLITE3_LIBRARY_RELEASE})
ENDIF(CMAKE_CONFIGURATION_TYPES或CMAKE_BUILD_TYPE)
ELSEIF(SQLITE3_LIBRARY_RELEASE)
SET(SQLITE3_LIBRARIES $ {SQLITE3_LIBRARY_RELEASE})
ELSE(SQLITE3_LIBRARY_DEBUG和SQLITE3_LIBRARY_RELEASE)
SET(SQLITE3_LIBRARIES $ {SQLITE3_LIBRARY_DEBUG})
ENDIF(SQLITE3_LIBRARY_DEBUG和SQLITE3_LIBRARY_RELEASE)

如果(SQLITE3_FOUND)
如果(不SQLITE3_FIND_QUIETLY)
(状态找到sqlite3的头文件在$ {SQLITE3_INCLUDE_DIR})
(状态找到SQLITE3库:$ {SQLITE3_LIBRARIES})
ENDIF(不SQLITE3_FIND_QUIETLY)
ELSE(SQLITE3_FOUND)
如果(SQLITE3_FIND_REQUIRED)
MESSAGE(是fatal_error找不到SQLITE3)
ELSE(SQLITE3_FIND_REQUIRED)
(状态可选包SQLITE3未找到)
ENDIF(SQLITE3_FIND_REQUIRED)
ENDIF(SQLITE3_FOUND)

这在Linux上正常工作,但在Windows上不工作。我花了几个小时现在尝试小改变其他CMAKE变量没有运气。看起来应该是直接得到CMake找到这个 dll

解决方案

这里有一些问题,还有一些奇怪的Windows



第一期;当使用MSVC作为生成器在Windows上搜索库时,CMake将始终查找.lib文件,而不是.dll,即使您指定了。 作为 find_library NAMES 参数很抱歉,这没有正确记录,实际上 CMAKE_FIND_LIBRARY_SUFFIXES 错误地陈述:


这指定了当find_library命令寻找图书馆。在Windows系统上,这通常是.lib和.dll,这意味着当试图找到foo库时,它将查找foo.dll等。


你可以很容易地检查这个;只需添加

 消息(CMAKE_FIND_LIBRARY_SUFFIXES:$ {CMAKE_FIND_LIBRARY_SUFFIXES})
pre>

到您的CMakeLists.txt。您应该看到如下输出:


  CMAKE_FIND_LIBRARY_SUFFIXES:.lib 


来自CMake邮件列表的此线程进一步确认此行为。如果您真的需要找到dll,则需要使用 find_file 命令,例如:

  find_file(SQLITE3_DLL_DEBUG NAMES sqlite3d。 dll PATHS ...)





下一个问题是一个小问题。您应该优先于 find_xxx 中的参数 PATHS HINTS 命令,如果它是硬编码的猜测。在 find_library 的文档中


3 。搜索由 HINTS 选项指定的路径。这些应该是由系统内省计算的路径,例如由已经找到的另一个项目的位置提供的提示。硬编码的猜测应使用 PATHS 选项指定。





更严重的问题是:

  FIND_LIBRARY(SQLITE3_LIBRARY_DEBUG NAMES sqlite3 sqlite3d ...)

您应该指定sqlite3d


$ b
$ b

现在的奇怪...



在Windows x64系统上, find_xxx 部分忽略C:\Windows \System32目录,而使用C:\Windows \SysWOW64目录。



如果您没有sqlite3.lib在C:\Windows \SysWOW64,那么 find_library 命令将总是失败,不管 PATHS 参数。



但是,如果你有C:\Windows \SysWOW64\sqlite3.lib, code> C:/ Windows / System32 作为 PATHS C:/ Windows / SysWOW64 / code>参数找到库,但设置 C:/Windows/System32/sqlite3.lib 的完整路径,即使C:/ Windows / System32 / sqlite3.lib不存在!如果图书馆不在那里,这显然是无用的;将会导致链接器错误。



还有一些来自CMake邮件列表的进一步阅读这里



说完之后,如果您要连结,就会使用.lib档案,而不是.dlls和System32& SysWOW64不是真正的.lib文件的地方。


I am having more trouble than I'd expect getting CMake to find the sqlite3.dll library on Windows 7 (64-bit if that matters). I have downloaded and placed the latest sqlite3.dll and sqlite3.def files to C:\Windows\System32. I am using the FindSqlite3.cmake module below:

IF( SQLITE3_INCLUDE_DIR AND SQLITE3_LIBRARY_RELEASE AND SQLITE3_LIBRARY_DEBUG )
    SET(SQLITE3_FIND_QUIETLY TRUE)
ENDIF( SQLITE3_INCLUDE_DIR AND SQLITE3_LIBRARY_RELEASE AND SQLITE3_LIBRARY_DEBUG )

FIND_PATH( SQLITE3_INCLUDE_DIR sqlite3.h )

FIND_LIBRARY(SQLITE3_LIBRARY_RELEASE NAMES sqlite3 )

FIND_LIBRARY(SQLITE3_LIBRARY_DEBUG NAMES sqlite3 sqlite3d HINTS /usr/lib/debug/usr/lib/ C:/Windows/System32/ )

IF( SQLITE3_LIBRARY_RELEASE OR SQLITE3_LIBRARY_DEBUG AND SQLITE3_INCLUDE_DIR )
SET( SQLITE3_FOUND TRUE )
ENDIF( SQLITE3_LIBRARY_RELEASE OR SQLITE3_LIBRARY_DEBUG AND SQLITE3_INCLUDE_DIR )

IF( SQLITE3_LIBRARY_DEBUG AND SQLITE3_LIBRARY_RELEASE )
# if the generator supports configuration types then set
# optimized and debug libraries, or if the CMAKE_BUILD_TYPE has a value
IF( CMAKE_CONFIGURATION_TYPES OR CMAKE_BUILD_TYPE )
SET( SQLITE3_LIBRARIES optimized ${SQLITE3_LIBRARY_RELEASE} debug ${SQLITE3_LIBRARY_DEBUG} )
ELSE( CMAKE_CONFIGURATION_TYPES OR CMAKE_BUILD_TYPE )
    # if there are no configuration types and CMAKE_BUILD_TYPE has no value
    # then just use the release libraries
SET( SQLITE3_LIBRARIES ${SQLITE3_LIBRARY_RELEASE} )
ENDIF( CMAKE_CONFIGURATION_TYPES OR CMAKE_BUILD_TYPE )
ELSEIF( SQLITE3_LIBRARY_RELEASE )
SET( SQLITE3_LIBRARIES ${SQLITE3_LIBRARY_RELEASE} )
ELSE( SQLITE3_LIBRARY_DEBUG AND SQLITE3_LIBRARY_RELEASE )
SET( SQLITE3_LIBRARIES ${SQLITE3_LIBRARY_DEBUG} )
ENDIF( SQLITE3_LIBRARY_DEBUG AND SQLITE3_LIBRARY_RELEASE )

IF( SQLITE3_FOUND )
IF( NOT SQLITE3_FIND_QUIETLY )
MESSAGE( STATUS "Found Sqlite3 header file in ${SQLITE3_INCLUDE_DIR}")
MESSAGE( STATUS "Found Sqlite3 libraries: ${SQLITE3_LIBRARIES}")
ENDIF( NOT SQLITE3_FIND_QUIETLY )
ELSE(SQLITE3_FOUND)
IF( SQLITE3_FIND_REQUIRED)
MESSAGE( FATAL_ERROR "Could not find Sqlite3" )
ELSE( SQLITE3_FIND_REQUIRED)
MESSAGE( STATUS "Optional package Sqlite3 was not found" )
ENDIF( SQLITE3_FIND_REQUIRED)
ENDIF(SQLITE3_FOUND)

This works fine on Linux, but not on Windows. I have spent a few hours now attempting small changes to other CMAKE variables with no luck. It seems like it should be straight forward getting CMake to find this dll. Could I get some help getting this to find the sqlite3 library on Windows?

解决方案

There are some issues here and also some weird Windows stuff!

First issue; when searching for a library on Windows with MSVC as the generator, CMake will always look for a ".lib" file - never a ".dll", even if you specify e.g. sqlite3.dll as the NAMES argument to find_library. This is unfortunately not properly documented, in fact the docs for CMAKE_FIND_LIBRARY_SUFFIXES wrongly state:

This specifies what suffixes to add to library names when the find_library command looks for libraries. On Windows systems this is typically .lib and .dll, meaning that when trying to find the foo library it will look for foo.dll etc.

You can easily check this; simply add

message("CMAKE_FIND_LIBRARY_SUFFIXES:  ${CMAKE_FIND_LIBRARY_SUFFIXES}")

to your CMakeLists.txt. You should see output like:

CMAKE_FIND_LIBRARY_SUFFIXES:  .lib

This thread from the CMake mailing list further confirms this behaviour. If you really need to find the dlls, you'll need to use the find_file command, e.g:

find_file(SQLITE3_DLL_DEBUG NAMES sqlite3d.dll PATHS ...)



The next issue is a minor one. You should prefer PATHS to HINTS as the argument in find_xxx commands if it's a hard-coded guess. From the docs for find_library:

3. Search the paths specified by the HINTS option. These should be paths computed by system introspection, such as a hint provided by the location of another item already found. Hard-coded guesses should be specified with the PATHS option.



A slightly more serious issue is in the line:

FIND_LIBRARY(SQLITE3_LIBRARY_DEBUG NAMES sqlite3 sqlite3d ...)

You should specify sqlite3d first then sqlite3 or sqlite3 will incorrectly be chosen as the Debug library if both are available.



And now the weirdness...

On a Windows x64 system, the find_xxx partially ignores the C:\Windows\System32 directory in favour of the C:\Windows\SysWOW64 one.

If you don't have the sqlite3.lib in C:\Windows\SysWOW64, then the find_library command will always fail, regardless of the PATHS argument.

However, if you do have C:\Windows\SysWOW64\sqlite3.lib, then any combination of C:/Windows/SysWOW64 and/or C:/Windows/System32 as the PATHS argument finds the library, but sets the full path to C:/Windows/System32/sqlite3.lib even if C:/Windows/System32/sqlite3.lib doesn't exist! This is obviously useless if the library isn't there; a linker error will result.

There is some further reading again from the CMake mailing list here.

Having said that, if you're linking, you'll be using the .lib files, not the .dlls, and System32 & SysWOW64 aren't really the place for .lib files.

这篇关于cmake在windows上查找sql​​ite3库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆