C ++中头文件的问题:链接器命令失败

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

提前道歉我确定这是一个非常简单的问题,但我一直无法找到解决方案。我正在将一个非常简单的 C++ 项目放在一起,但无法正确使用头文件。

目录结构如下:

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

距离头文件(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

最后 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';
}

现在,当我实际尝试使用以下命令编译 hnsw.cpp 时:g++ hnsw.cpp --std=c++11 我收到以下错误:

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)

如果我将 main 移动到 distances.cpp 文件中,那么一切都会顺利进行。为了简洁起见,我没有发布整个文件,因为它很大,而且我很确定它不相关。如果这里有任何关于理解一般过程的资源,那也会非常有帮助。

如果您不想为以后 linking 构建单独的目标文件,而是一次构建整个可执行文件,您需要指定 all 命令行参与翻译单位:

g++ -std=c++11 hnsw.cpp ../utils/distances.cpp

输出文件将被称为 a.out,除非您还使用 -o 标志覆盖该名称。

通常您会单独编译翻译单元,然后 link 在单独的步骤中编译它们:

g++ -std=c++11 mainGraph/hnsw.cpp -c -o mainGraph/hnsw.o
g++ -std=c++11 utils/distances.cpp -c -o utils/distances.o

g++ -std=c++11 mainGraph.hnsw.o utils/distances.o -o myprog

您也不会手动维护这些命令,而是将它们粘贴到 Makefile 或某些等效的构建系统中。关键是你不需要重新编译没有改变的翻译单元只是因为你改变了你的源代码的一些部分。

当您 运行 g++ hnsw.cpp --std=c++11 时,您是在告诉编译器使用 c++ 标准库编译 hnsw.cpp 和 link 输出以创建可执行文件。 linker 无法找到 distances 的实现,因为它位于 distances.cpp 中。

您需要编译这两个文件并将它们 link 一起编译。有很多方法可以实现这一点,但最简单的是在调用 gcc 时指定两个源文件,如下所示:

g++ -std=c++11 hnsw.cpp ../utils/distances.cpp