0

I'm simply trying to use functions from a C-libary (the Space Group library spglib) inside my C++ code. The library is already compiled and installed in an Anaconda environment.

A minimal example looks like this:

extern "C"
{
#include "spglib.h"
}

#include <iostream>

static void test_spg_get_symmetry(void);

int main(void)
{
    test_spg_get_symmetry();

    return 0;
}

static void test_spg_get_symmetry(void)
{
    double lattice[3][3] = {{4, 0, 0}, {0, 4, 0}, {0, 0, 3}};
    double position[][3] =
        {
            {0, 0, 0},
            {0.5, 0.5, 0.25},
            {0.3, 0.3, 0},
            {0.7, 0.7, 0},
            {0.2, 0.8, 0.25},
            {0.8, 0.2, 0.25},
            {0, 0, 0.5},
            {0.5, 0.5, 0.75},
            {0.3, 0.3, 0.5},
            {0.7, 0.7, 0.5},
            {0.2, 0.8, 0.75},
            {0.8, 0.2, 0.75}};
    int types[] = {1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2};
    int num_atom = 12;
    int max_size = 50;
    int i, j, size;
    int rotation[max_size][3][3];
    double translation[max_size][3];

    double origin_shift[3] = {0.1, 0.1, 0};
    for (i = 0; i < num_atom; i++)
    {
        for (j = 0; j < 3; j++)
        {
            position[i][j] += origin_shift[j];
        }
    }

    printf("*** Example of spg_get_symmetry (Rutile two unit cells) ***:\n");
    size = spg_get_symmetry(rotation,
                            translation,
                            max_size,
                            lattice,
                            position,
                            types,
                            num_atom,
                            1e-5);
    for (i = 0; i < size; i++)
    {
        printf("--- %d ---\n", i + 1);
        for (j = 0; j < 3; j++)
            printf("%2d %2d %2d\n", rotation[i][j][0], rotation[i][j][1], rotation[i][j][2]);
        printf("%f %f %f\n",
               translation[i][0], translation[i][1], translation[i][2]);
    }
}

I'm compiling with a command similar to this: g++ -g symm.cpp -o main.exe -I/miniconda3/env/include -Lminiconda3/env/aims-env/lib/libsymspg.a

But I get a linking error every time:

undefined reference to 'spg_get_symmetry'.

What am I missing? Any help is appreciated.

roman
  • 123
  • 9
  • 1
    You don't need `extern "C"`, since `"spglib.h"` already contains one. `-Lminiconda3/env/aims-env/lib/libsymspg.a` is not how you link libraries. Use `-Lminiconda3/env/aims-env/lib -lsymspg`. – HolyBlackCat Apr 18 '21 at 11:47
  • 1
    You apparently missed to link the library. The should be a flag like `-lspg` or something similar – πάντα ῥεῖ Apr 18 '21 at 11:47

0 Answers0