【发布时间】:2016-12-21 17:30:15
【问题描述】:
CMAKE的文件有这样的代码:
cmake_minimum_required(VERSION 3.6)
project(HelloSqliteC)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.c)
add_executable(HelloSqliteC ${SOURCE_FILES})
文件 main.c 有这样的代码:
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
int main() {
sqlite3 *db;
int rc;
rc = sqlite3_open("database.db", &db);
if (rc) {
fprintf(stderr, "Can't open database: %s!\n", sqlite3_errmsg(db));
} else {
fprintf(stderr, "Opened database successfully!\n");
}
sqlite3_close(db);
return 0;
}
当我尝试编译时:
/home/marcus/ide/clion/clion-2016.3.1/bin/cmake/bin/cmake --build /home/marcus/projects/native/HelloSqliteC/cmake-build-debug --target HelloSqliteC -- -j 4
[ 50%] Building C object CMakeFiles/HelloSqliteC.dir/main.c.o
[100%] Linking C executable HelloSqliteC
CMakeFiles/HelloSqliteC.dir/main.c.o: In function `main':
/home/marcus/projects/native/HelloSqliteC/main.c:13: undefined reference to `sqlite3_open'
/home/marcus/projects/native/HelloSqliteC/main.c:16: undefined reference to `sqlite3_errmsg'
/home/marcus/projects/native/HelloSqliteC/main.c:21: undefined reference to `sqlite3_close'
collect2: error: ld returned 1 exit status
CMakeFiles/HelloSqliteC.dir/build.make:94: recipe for target 'HelloSqliteC' failed
make[3]: *** [HelloSqliteC] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/HelloSqliteC.dir/all' failed
make[2]: *** [CMakeFiles/HelloSqliteC.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/HelloSqliteC.dir/rule' failed
make[1]: *** [CMakeFiles/HelloSqliteC.dir/rule] Error 2
Makefile:118: recipe for target 'HelloSqliteC' failed
make: *** [HelloSqliteC] Error 2
我尝试使用不同的方法解决这个问题,但没有成功。 我正在使用 CLion C/C++,我的操作系统是 Ubuntu 16.04,我使用 autoconf 安装 sqlite3。
为了测试,我使用上面的 main.c 并在命令行中使用“-l sqlite3”使用 GCC 编译,我成功了,但我想使用 CLion。
帮帮我,谢谢。
【问题讨论】:
-
我假设 CLion 有一个对话框可以将外部库添加到您的项目中。您需要为 sqlite3 执行此操作。然后 CLion 将为您正确重建
CMakeLists.txt。 -
@Olaf,不,这是一个 CLion 使用问题
-
@nega:你读过副本吗?它是通用的,也涵盖了这个问题。 如何添加库是特定于构建工具的,但用户应该自己弄清楚这些事情。换句话说:到 RTFineM。
-
@olaf 这正是使这不是骗局的原因。 OP 已经证明他知道如何在通用(命令行)意义上修复链接故障。但具体询问如何在 CMake 和/或 CLion 中修复它。
标签: sqlite cmake cross-platform clion