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

杀马特。学长 韩版系。学妹 提交于 2020-12-08 07:16:05

问题


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!


回答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.




回答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



来源:https://stackoverflow.com/questions/41640029/how-can-i-link-cmake-and-sqlite-without-an-external-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!