Search code examples
c++cassandraclionlibs

C++ Cassandra build error


Hello i have a problem with building my code in c++. I have installed cassandra on my Mac OS and all libraries that i need from datastax. But when i build my test project i always get

     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

I know that i'm missing something in my CMakelist but i dont know exactly what

Here is my test code:

#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;
}

Solution

  • On MacOS the default installation directory is /usr/local with header files being installed in include and libraries being installed in lib. To ensure that the driver is installed properly you can compile your example code via clang++

    clang++ <source_file_name> -o <executable_output> -lcassandra
    

    To correct your issue with the CMake (CMakelists.txt) configuration you need to make sure that you use target_link_libraries(<target> cassandra) in order for your application to link against the driver library installed on your system.