如何在Fedora中编译sqlite3.c?

imk*_*dal 3 c++ sqlite gnu makefile

我在构建sqlite3合并时遇到了麻烦,就像我以前在windows中一样,通过将源代码编译到我的程序中,这是我当前的makefile

all:
    gcc -g -c sqlite3.c -o sqlite3.o
    g++ -g -c main.cpp -o main.o
    g++ -o test sqlite3.o main.o
Run Code Online (Sandbox Code Playgroud)

我的main.cpp文件:

#include "sqlite3.h"

int main(){
  //nothing
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

以下是编译时收到的错误:

sqlite.o: In function `pthreadMutexAlloc':
/home/kendal/c++/sqlite3/test/../lib/sqlite3.c:17910: undefined reference to
`pthread_mutexattr_init'
/home/kendal/c++/sqlite3/test/../lib/sqlite3.c:17911: undefined reference to
`pthread_mutexattr_settype'
/home/kendal/c++/sqlite3/test/../lib/sqlite3.c:17913: undefined reference to`pthread_mutexattr_destroy'
sqlite.o: In function    
`pthreadMutexTry':
/home/kendal/c++/sqlite3/test/../lib/sqlite3.c:18042: undefined reference to `pthread_mutex_trylock'
sqlite.o: In function `unixDlOpen': /home/kendal/c++/sqlite3/test/../lib/sqlite3.c:28164: undefined reference to `dlopen' 
sqlite.o: In function `unixDlError':
/home/kendal/c++/sqlite3/test/../lib/sqlite3.c:28178: undefined reference to `dlerror'
sqlite.o: In function `unixDlSym':
/home/kendal/c++/sqlite3/test/../lib/sqlite3.c:28204: undefined reference to `dlsym'
sqlite.o: In function `unixDlClose':
/home/kendal/c++/sqlite3/test/../lib/sqlite3.c:28209: undefined reference to `dlclose'
Run Code Online (Sandbox Code Playgroud)

mau*_*uve 8

你还必须链接到pthreads和dl,这样做:

all:
  gcc -g -c sqlite3.c -o sqlite3.o
  g++ -g -c main.cpp -o main.o
  g++ -o test -pthread -ldl sqlite3.o main.o
Run Code Online (Sandbox Code Playgroud)

使用"-lpthread"与使用"-pthread"略有不同," - lpthread"表示链接到pthread库,而"-pthread"表示g ++将为您选择带有pthread接口的相应线程库.这就是为什么你更喜欢使用"-pthread"的原因.

添加"-ldl"意味着您链接到"dl"库,该库是包含dlsym,dlopen等的库.