【问题标题】:C/C++ undefined reference for sqlite3's functions using CLion with CMAKE使用 CLion 和 CMAKE 的 sqlite3 函数的 C/C++ 未定义参考
【发布时间】: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


【解决方案1】:

我也使用 CLion C/C++。操作系统是 Ubuntu 18.04。 但我想编译main.cpp。 CMakelist.txt 是:

cmake_minimum_required(VERSION 3.13)
project(ProjectName)

set(CMAKE_CXX_STANDARD 14)
add_executable(student main.cpp)
target_link_libraries(ProjectName LINK_PUBLIC sqlite3)

成功了。

【讨论】:

  • 这样一个简单的解决方案可以解决困扰我 3 天的问题。谢谢。
【解决方案2】:

我通过target_link_libraries(HelloSqlite3 LINK_PUBLIC sqlite3) 或target_link_libraries(projectName LINK_PUBLIC libraryName) 解决了这个问题。

【讨论】:

    【解决方案3】:

    CLion 使用 CMake 进行所有构建和项目配置。您必须手动修改CMakeLists.txt。实际上这是一个 CMake 问题。

    CMakeLists.txt 中的这一行将解决您的问题:

    add_compile_options(-l sqlite3)
    

    但实际上 CMake 有一个更复杂的依赖发现系统。阅读How To Find Libraries 了解这一点。

    【讨论】:

    • 这行不通 =(。我会阅读如何查找库。谢谢。
    • 你在 add_executable 之前添加了 add_compile_options 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 2017-10-13
    • 2022-01-12
    相关资源
    最近更新 更多