How can I link CMake and SQLite without an external script?

16,939

Solution 1

You have basically two options:
1) have a FindSQLite3.cmake in a directory called cmake inside your project's root directory like the following FindSQLite3.cmake that you already tried but you need to have something like the following

cmake_minimum_required (VERSION 2.8.12.2)
project (Tutorial)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
add_executable(tutorial new.cpp)
find_package (SQLite3)
if (SQLITE3_FOUND)
  include_directories(${SQLITE3_INCLUDE_DIRS})
  target_link_libraries (tutorial ${SQLITE3_LIBRARIES})
endif (SQLITE3_FOUND)

2) since you know the location of your sqlite3 include directory and library you could directly set the path to those, in your CMakeLists.txt you will have something like link_directories() and include_directories(), e.g. you will have the following lines:

cmake_minimum_required (VERSION 2.8.12.2)
project (Tutorial)
add_executable(tutorial new.cpp)
include_directories(/usr/include)
link_directories(/usr/lib)
target_link_libraries(tutorial sqlite3)

Something along those two directions should work.
Personally, I would suggest the first approach.

Solution 2

I just added the following sting to my CMakeLists.txt file and it works.

find_package (SQLite3)

include_directories(${SQLite3_INCLUDE_DIRS})
target_link_libraries (${OUT_TARGET} ${SQLite3_LIBRARIES})

cmake version 3.14.3

Share:
16,939

Related videos on Youtube

Tsyvarev
Author by

Tsyvarev

Updated on September 15, 2022

Comments

  • Tsyvarev
    Tsyvarev over 1 year

    I have the following CMakeLists:

    cmake_minimum_required (VERSION 2.8.12.2)
    project (Tutorial)
    find_package (sqlite3)
    if (SQLITE3_FOUND)
      include_directories(${SQLITE3_INCLUDE_DIRS})
      target_link_libraries (new ${SQLITE3_LIBRARIES})
    endif (SQLITE3_FOUND)
    add_executable(Tutorial new.cpp)
    

    However, when I cmake, I get the following message:

    CMake Warning at CMakeLists.txt:3 (find_package):
      By not providing "Findsqlite3.cmake" in CMAKE_MODULE_PATH this project has
      asked CMake to find a package configuration file provided by "sqlite3", but
      CMake did not find one.
    
      Could not find a package configuration file provided by "sqlite3" with any
      of the following names:
    
        sqlite3Config.cmake
        sqlite3-config.cmake
    
      Add the installation prefix of "sqlite3" to CMAKE_PREFIX_PATH or set
      "sqlite3_DIR" to a directory containing one of the above files.  If
      "sqlite3" provides a separate development package or SDK, be sure it has
      been installed.
    

    I have also tried this and this for alternative CMakeLists files, but none of these have worked.

    I also tried this and it didn't work:

    FIND_PATH(SQLITE3_INCLUDE_DIR NAMES sqlite3.h)
    FIND_LIBRARY(SQLITE3_LIBRARY NAMES sqlite)
    INCLUDE(FindPackageHandleStandardArgs)
    FIND_PACKAGE_HANDLE_STANDARD_ARGS(SQLITE3 DEFAULT_MSG SQLITE3_LIBRARY SQLITE3_INCLUDE_DIR)
    IF(SQLITE3_FOUND)
        SET(SQLITE3_LIBRARIES ${SQLITE3_LIBRARY})
        SET(SQLITE3_INCLUDE_DIRS ${SQLITE3_INCLUDE_DIR})
    ELSE(SQLITE3_FOUND)
        SET(SQLITE3_LIBRARIES)
        SET(SQLITE3_INCLUDE_DIRS)
    ENDIF(SQLITE3_FOUND)
    
    MARK_AS_ADVANCED(SQLITE3_INCLUDE_DIRS SQLITE3_LIBRARIES)
    

    How can I link SQLite without using an extension?

    Thank you!

  • sancelot
    sancelot about 6 years
    if you use the linked FindSQLite3.cmake, change the FIND_LIBRARY as follow, otherwis eit will not find libsqlite3 :# Look for the library. FIND_LIBRARY(SQLITE3_LIBRARY NAMES sqlite3)
  • val is still with Monica
    val is still with Monica over 4 years
    Do not use variables directly, link to target instead: target_link_libraries (${OUT_TARGET} SQLite3::SQLite3)