如何在没有外部脚本的情况下链接CMake和SQLite?

时间:2017-01-13 17:16:58

标签: c++ sqlite cmake

我有以下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)

然而,当我cmake时,我收到以下消息:

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.

我还尝试了thisthis替代CMakeLists文件,但这些都没有奏效。

我也尝试了这个并且它不起作用:

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)

如何在不使用扩展程序的情况下链接SQLite?

谢谢!

2 个答案:

答案 0 :(得分:6)

你基本上有两种选择:
1)在项目根目录下的FindSQLite3.cmake目录中有cmake,如下所示FindSQLite3.cmake 您已经尝试但需要具备以下内容

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)因为您知道sqlite3包含目录和库的位置,您可以直接设置这些目录和库的路径,在CMakeLists.txt中,您将拥有link_directories()include_directories()之类的内容,例如你将有以下几行:

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)

这两个方向的某些方面应该有效 就个人而言,我建议采用第一种方法。

答案 1 :(得分:0)

我刚刚将以下字符串添加到了我的 CMakeLists.txt 文件中,它可以正常工作。

find_package (SQLite3)

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

cmake版本3.14.3