C++ Cassandra 构建错误

C++ Cassandra build error

你好,我在用 C++ 构建代码时遇到了问题。 我已经在我的 Mac OS 上安装了 cassandra 以及我需要从 datastax 获得的所有库。 但是当我构建我的测试项目时,我总是得到

     Undefined symbols for architecture x86_64:
     "_cass_cluster_free", referenced from:
      _main in main.cpp.o
    "_cass_cluster_new", referenced from:
      _main in main.cpp.o
  "_cass_cluster_set_contact_points", referenced from:
      _main in main.cpp.o
  "_cass_error_desc", referenced from:
      _main in main.cpp.o
  "_cass_future_error_code", referenced from:
      _main in main.cpp.o
  "_cass_future_free", referenced from:
      _main in main.cpp.o
  "_cass_session_connect", referenced from:
      _main in main.cpp.o
  "_cass_session_free", referenced from:
      _main in main.cpp.o
  "_cass_session_new", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64

我知道我的 CMakelist 中遗漏了一些东西,但我不知道到底是什么

这是我的测试代码:

#include <cassandra.h>
#include <cstdio>

int main() {
    /* Setup and connect to cluster */
    CassCluster* cluster = cass_cluster_new();
    CassSession* session = cass_session_new();

    /* Add contact points */
    cass_cluster_set_contact_points(cluster, "127.0.0.1");

    /* Provide the cluster object as configuration to connect the session */
    CassFuture* connect_future = cass_session_connect(session, cluster);

    /* This operation will block until the result is ready */
    CassError rc = cass_future_error_code(connect_future);

    printf("Connect result: %s\n", cass_error_desc(rc));

    /* Run queries... */

    cass_future_free(connect_future);
    cass_session_free(session);
    cass_cluster_free(cluster);

    return 0;
}

在 MacOS 上,默认安装目录是 /usr/local,头文件安装在 include 中,库安装在 lib 中。为确保驱动程序安装正确,您可以通过 clang++

编译您的示例代码
clang++ <source_file_name> -o <executable_output> -lcassandra

要更正您的 CMake (CMakelists.txt) 配置问题,您需要确保使用 target_link_libraries(<target> cassandra) 以便您的应用程序 link 针对安装在你的系统。