Sqlite合并C文件无法在Raspbian上编译 使用sqlite作为CMake文件中的库/包编译sqlite,以便将其全局导入

问题描述

我正在使用RaspBerry Pi运行一个与can接口交互的C程序,并将值存储在数据库中作为项目的一部分。 一个月前我开始使用C,但是我对所有内容仍然有些迷茫。

我的工作区和问题的描述:

我的测试文件test virtual.c包含一个自制文件handler_code.c,该文件包含sqlite3.c合并文件(通过#include "sqlite3.c")。 然后,我得到了一个CMakeLists.txt


if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
    set( STANDALONE TRUE )
    cmake_minimum_required(VERSION 3.13)
    set(THREADS_PREFER_PTHREAD_FLAG ON)
    find_package(Threads required)
    find_package(CARLOS required)
else()
    include_directories(${CMAKE_SOURCE_DIR}/core/include)
    include_directories(${CMAKE_BINARY_DIR}/include)
    include_directories(${CMAKE_SOURCE_DIR}/gui/include)
endif()

add_deFinitions(-DRESOURCES_DIR="${CMAKE_CURRENT_SOURCE_DIR}/resources")

add_executable(test_example "test virtual.c")
target_link_libraries(test_example carlos)

set_target_properties(
    test_example
    PROPERTIES
    C_STANDARD 99
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/examples_bin
)

它将导入我需要的所有库。所有文件都在同一目录中。 然后,我进入子目录build并运行成功的cmake ..,然后运行make。这就是发生的情况:

pi@raspBerrypi:~/Desktop/examples/build $ make
[ 50%] Building C object CMakeFiles/test_example.dir/test_virtual.c.o
In file included from /home/pi/Desktop/examples/handler_code.c:5,from /home/pi/Desktop/examples/test virtual.c:8:
/home/pi/Desktop/examples/sqlite3.c:34058:42: error: ‘mremap’ undeclared here (not in a function); did you mean ‘munmap’?
   { "mremap",(sqlite3_syscall_ptr)mremap,0 },^~~~~~
                                          munmap
/home/pi/Desktop/examples/sqlite3.c: In function ‘unixRemapfile’:
/home/pi/Desktop/examples/sqlite3.c:38625:42: error: ‘MREMAP_MAYMOVE’ undeclared (first use in this function)
     pNew = osMremap(pOrig,nReuse,nNew,MREMAP_MAYMOVE);
                                          ^~~~~~~~~~~~~~
/home/pi/Desktop/examples/sqlite3.c:38625:42: note: each undeclared identifier is reported only once for each function it appears in
/home/pi/Desktop/examples/sqlite3.c:38625:42: warning: passing argument 4 of ‘(void * (*)(void *,size_t,int,...))aSyscall[24].pCurrent’ makes integer from pointer without a cast [-Wint-conversion]
/home/pi/Desktop/examples/sqlite3.c:38625:42: note: expected ‘int’ but argument is of type ‘struct unix_syscall *’
make[2]: *** [CMakeFiles/test_example.dir/build.make:63: CMakeFiles/test_example.dir/test_virtual.c.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/test_example.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

我尝试仅使用gcc编译sqlite3.c,并且得到了这个结果:

pi@raspBerrypi:~/Desktop/examples $ gcc sqlite3.c
/usr/bin/ld: /usr/lib/gcc/arm-linux-gnueabihf/8/../../../arm-linux-gnueabihf/crt1.o: in function `_start':
(.text+0x34): undefined reference to `main'
/usr/bin/ld: /tmp/cc6Quw7S.o: in function `pthreadMutexAlloc':
sqlite3.c:(.text+0x5098): undefined reference to `pthread_mutexattr_init'
/usr/bin/ld: sqlite3.c:(.text+0x50a8): undefined reference to `pthread_mutexattr_settype'
/usr/bin/ld: sqlite3.c:(.text+0x50c8): undefined reference to `pthread_mutexattr_destroy'
/usr/bin/ld: /tmp/cc6Quw7S.o: in function `pthreadMutexTry':
sqlite3.c:(.text+0x51b8): undefined reference to `pthread_mutex_trylock'
/usr/bin/ld: /tmp/cc6Quw7S.o: in function `sqlite3ThreadCreate':
sqlite3.c:(.text+0x9fcc): undefined reference to `pthread_create'
/usr/bin/ld: /tmp/cc6Quw7S.o: in function `sqlite3ThreadJoin':
sqlite3.c:(.text+0xa084): undefined reference to `pthread_join'
/usr/bin/ld: /tmp/cc6Quw7S.o: in function `unixDlOpen':
sqlite3.c:(.text+0x148c0): undefined reference to `dlopen'
/usr/bin/ld: /tmp/cc6Quw7S.o: in function `unixDlError':
sqlite3.c:(.text+0x148f4): undefined reference to `dlerror'
/usr/bin/ld: /tmp/cc6Quw7S.o: in function `unixDlSym':
sqlite3.c:(.text+0x14970): undefined reference to `dlsym'
/usr/bin/ld: /tmp/cc6Quw7S.o: in function `unixDlClose':
sqlite3.c:(.text+0x1498c): undefined reference to `dlclose'
collect2: error: ld returned 1 exit status

我也在Windows计算机上进行了一些测试。像config.c一样,名为handler_code.c文件也包含合并。

PS C:\Users\p107770\Desktop\projects\hmi-rapid-prototyping\C> gcc sqlite3.c
C:/MATLAB/V_R2016b/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): In function `main':
C:/crossdev/src/mingw-w64-v3-git/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
PS C:\Users\p107770\Desktop\projects\hmi-rapid-prototyping\C> gcc config.c
PS C:\Users\p107770\Desktop\projects\hmi-rapid-prototyping\C>

因此,在这里,它成功地编译了包含合并文件文件(即使不是同一文件,这也是我希望在树莓派上使用的文件),但它并没有单独编译合并文件(不明白)。

我在搜索时发现的想法

使用sqlite作为CMake文件中的库/包

类似于this的事物。但是看着我的CMake文件,我意识到我的sqlite合并文件不是“ {package””,就像CARLOSThreads一样(我在安装CARLOS时提供了.deb文件)。因此,我的问题是如何将合并文件转换为包?

编译sqlite,以便将其全局导入

我记得读过一些有关此的东西。如果我没记错的话,我可以将sqlite合并文件编译成.dll,并用#include <sqlite3.c>行而不是#include "sqlite3.c"使其在任何C文件中可用,但是我不知道它是否可以工作在这里,因为我认为使用gcc而非make编译时,这是可行的。

在此先感谢您提供的帮助,如有需要,我可以添加一些代码

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)