通过cmake用sqlite3编译c++项目

问题描述 投票:0回答:0

所以我正在做一个大学项目,我决定要添加一个数据库(我们必须制作一个在控制台上运行的游戏)。我目前正在使用 GitHub Codespaces,所以基本上 VSCode 在 Linux 上运行。

经过一番努力,我设法让 cmake 与我的项目一起工作,并将 Catch2 包含在项目中,但现在我也在尝试向游戏中添加一个数据库。唯一的问题是我研究过的教程/指南/疑难解答帖子似乎都不起作用。我尝试以多种方式配置它,但它总是给出不同的错误,这似乎与以前一样无法解决。

如果你想知道 GitHub Codespaces 已经包含了几乎所有的东西,我使用了像

sudo apt-get install libsqlite3-dev
这样的安装命令,但它没有改变任何东西(安装/更改了 0 个包),因为它已经包含了 sqlite

这是我的文件结构(在我一直在尝试的过程中对 sqlite 进行了一些更改):

main.cpp
CMakeLists.txt
| debug
--| Catch2
--| CMakeFiles
--| sqlite
  --| include (contains sqlite3.c and sqlite3.h and all the amalgamation files)
  --| lib (contains the linux files: "sqldiff", "sqlite3" and "sqlite3_analyzer")
| include (all my header files)
| src (all my .cpp files except for main.cpp)

这是我的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/debug")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/build)

# define libraries to link to final executable
set (PROJECT_LINK_LIBS sqlite3.dll)
# define the sqlite header include file directory
set (SQLITE_INCLUDE debug/sqlite/include)
# define the sqlite link library directory
set (SQLITE_LINK_DIR debug/sqlite/lib)

#Project file name and version
project(game VERSION 0.1)

#Project compilation
file(GLOB_RECURSE SRC_FILES src/*.cpp)
add_executable(game main.cpp ${SRC_FILES} debug/sqlite3/include/sqlite3.c)
include_directories(game PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)

#SQLite integration

find_package(unofficial-sqlite3 CONFIG REQUIRED)
target_link_libraries(main PRIVATE unofficial::sqlite3::sqlite3)
# define the directories to search for libraries
link_directories ( ${SQLITE_LINK_DIR})
# define the directories to search for headers
include_directories (include ${SQLITE_INCLUDE})
# define the libraries to link to the final executable
target_link_libraries (game ${PROJECT_LINK_LIBS})
target_link_libraries(game PUBLIC sqlite3)

#include_directories( /usr/local/lib )
#find_package ( sqlite3  REQUIRED /usr/local/lib/libsqlite3.so)
#add_compile_options(-l sqlite3)
#if(SQLITE3_FOUND)
#    include_directories(${SQLite3_INCLUDE_DIRS})
#    target_link_libraries(${OUT_TARGET} ${SQLITE3_LIBRARIES})
#endif(SQLITE3_FOUND)
#include_directories(${CMAKE_CURRENT_SOURCE_DIR}/debug/sqlite3/)
#set(sqlite3_srcs ${CMAKE_CURRENT_SOURCE_DIR}/debug/sqlite3/sqlite3.c ${CMAKE_CURRENT_SOURCE_DIR}/debug/sqlite3/sqlite3.h )
#add_library(sqlitelib SHARED ${sqlite3_srcs} )
#target_link_libraries(game sqlitelib)

有很多东西被注释掉了,因为它们不起作用,我最终尝试了不同的东西。

我通常会通过像

Could not find a package configuration file provided by "sqlite3" with any of the following names
这样的调试运行“cmake ..”来获得错误,或者当我设法使该部分工作时它会在编译时出现错误,例如
main.cpp.o: in function \main': main.cpp (.text+0x31e): undefined reference to `sqlite3_open'`
以及其他一些似乎不是即使我在 main.cpp 中使用 #include

也被识别

我已经尝试了很多我在谷歌上找到的东西,但老实说,它们都没有用。任何帮助将不胜感激

我用谷歌搜索,查看了 stackoverflow 和其他网页上的多个帖子。他们都没有真正帮助过。

我希望能够使用 cmake 编译我使用 sqlite3 的项目

c++ sqlite cmake c++14
© www.soinside.com 2019 - 2024. All rights reserved.