mardi 28 mars 2017

Trouble with header files in c++: linker command failed

Apologizing ahead of time for what I'm certain is a very simple problem, but I've been unable to find a solution for this. I'm putting a very simple C++ project together, but having trouble using a header file appropriately.

The directory structure is below:

.
├── mainGraph
│   └── hnsw.cpp
└── utils
    ├── distances.cpp
    └── distances.h

The distances header file (distances.h):

#ifndef DISTANCES
#define DISTANCES

#include <vector>

enum class Metric { Cosine, Euclidean};

double distance(const std::vector<double>& A, const std::vector<double>& B, Metric metric);
double similarity(const std::vector<double>& A, const std::vector<double>& B, Metric metric);

#endif

And finally hnsw.cpp:

#include "../utils/distances.h"
#include <vector>
#include <iostream>

int main(){
    std::vector<double> A = {0.1, 0.5, 0.7, 1.1};
    std::vector<double> B = {0.5, 0.3, 0.8, 0.9};

    std::cout << distance(A, B, Metric::Cosine) << '\n';
}

Now, when I actually try to compile hnsw.cpp with the following command: g++ hnsw.cpp --std=c++11 I get the following error:

Undefined symbols for architecture x86_64:
  "distance(std::__1::vector<double, std::__1::allocator<double> > const&, std::__1::vector<double, std::__1::allocator<double> > const&, Metric)", referenced from:
      _main in hnsw-ad0e05.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

If I move the main into the distances.cpp file then everything works smoothly. For the purposes of brevity I'm not posting that whole file as it's sizable and I'm fairly certain it's not relevant. If there are any resources about understanding the general process here that would also be very helpful.

Aucun commentaire:

Enregistrer un commentaire