Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake find sqlite3 library on windows

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?

like image 423
E-rich Avatar asked Dec 26 '22 06:12

E-rich


1 Answers

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.

like image 138
Fraser Avatar answered Jan 04 '23 04:01

Fraser