c++ - 未定义对使用CMake生成为lib .a的方法的引用

标签 c++ cmake

您能解释一下为什么我的示例中的AllToAll函数未定义吗?我使用CMake生成一个libNeuralNetwork.a,以示例为例。

LayerFactory.hpp

#pragma once
#include "LayerModel.hpp"
#include "Layer.hpp"

namespace nn
{
    extern internal::LayerModel AllToAll(int numberOfNeurons, activationFunction activation = sigmoid);
}

LayerFactory.cpp

#include "LayerFactory.hpp"
#include "AllToAll.hpp"

using namespace nn;
using namespace internal;

LayerModel AllToAll(int numberOfNeurons, activationFunction activation)
{
    LayerModel model
    {
        allToAll,
        activation,
        numberOfNeurons
    };
    return model;
}

神经网络

#pragma once
#include "layer/LayerModel.hpp"
#include "layer/LayerFactory.hpp"

namespace nn
{
    class NeuralNetwork
    {
    public:
        NeuralNetwork(int numberOfInputs, std::vector<internal::LayerModel> models);
        //...
    };
}

范例.cpp

#include "../src/neural_network/NeuralNetwork.hpp"

using namespace nn;

int example1()
{
    NeuralNetwork neuralNetwork(3, {AllToAll(5), AllToAll(2)});
}

错误信息:
CMakeFiles/UnitTests.out.dir/ExamplesTest.cpp.o: In function `example1()':
ExamplesTest.cpp:(.text+0x8b3): undefined reference to `nn::AllToAll(int, nn::activationFunction)'

最佳答案

您已在顶级 namespace 中声明了AllToAll,并在nn namespace 中对其进行了定义。

以下内容不会在 namespace 中声明该函数:

using namespace foo;

extern void Bar();

你需要:

namespace foo {
  extern void Bar();
}

关于c++ - 未定义对使用CMake生成为lib .a的方法的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59579808/

相关文章:

c++ - OpenCV VS 2010 C++ CMake

在 C 中使用 pow 时,CMake 能否检测到我是否需要链接到 libm?

c++ - 如何在显示多个属性的 graphviz 中打印图形

c++ - 我该怎么办 : convert surface to a texture or create a texture with certain multisampling parameters or render a surface with an alpha layer

c++ - unique_ptr<char[]> 困惑

c++ - apientry dllmain 出错

c++ - 如何修复 OpenGL Superbible 第 6 版书籍提供的 CMake 文件

c++ - FLTK 1.3 链接错误

c - 在 Windows 上使用 Cmake/CLion 编译 lzma 库的一种简单方法?

c++ - 如何正确使用 intptr 将 char* 值从 C++ DLL 返回到 Vb.net