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



你能解释一下为什么我的函数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);
}

层工厂.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)'

您已在顶级命名空间中声明了AllToAll,并在nn命名空间中定义了它。

以下内容不会在命名空间中声明函数:

using namespace foo;
extern void Bar();

你需要:

namespace foo {
extern void Bar();
}

最新更新