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

huangapple go评论90阅读模式
英文:

undefined reference to method generated as lib .a with 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;
}

NeuralNetwork.hpp

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

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

Example.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)'

(Note: I removed the HTML entity codes like &quot; in your code snippets to make the code more readable in the translation.)

英文:

Can you explain me why my function AllToAll is undefined in my example? I use CMake to generate a libNeuralNetwork.a which is called by the exemple.

LayerFactory.hpp

#pragma once
#include &quot;LayerModel.hpp&quot;
#include &quot;Layer.hpp&quot;

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

LayerFactory.cpp

#include &quot;LayerFactory.hpp&quot;
#include &quot;AllToAll.hpp&quot;

using namespace nn;
using namespace internal;

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

NeuralNetwork.hpp

#pragma once
#include &quot;layer/LayerModel.hpp&quot;
#include &quot;layer/LayerFactory.hpp&quot;

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

Example.cpp

#include &quot;../src/neural_network/NeuralNetwork.hpp&quot;

using namespace nn;

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

error message:

CMakeFiles/UnitTests.out.dir/ExamplesTest.cpp.o: In function `example1()&#39;:
ExamplesTest.cpp:(.text+0x8b3): undefined reference to `nn::AllToAll(int, nn::activationFunction)&#39;

答案1

得分: 2

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

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

using namespace foo;

extern void Bar();

你需要:

namespace foo {
  extern void Bar();
}
英文:

You have declared AllToAll in the top-level namespace and defined it in the nn namespace.

The following will not declare the function in the namespace:

using namespace foo;

extern void Bar();

You need:

namespace foo {
  extern void Bar();
}

huangapple
  • 本文由 发表于 2020年1月3日 21:55:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定