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

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

undefined reference to method generated as lib .a with CMake

问题

你的函数AllToAll在示例中为什么未定义可以解释一下吗?我使用CMake生成了一个名为libNeuralNetwork.a的库,该库在示例中被调用。

LayerFactory.hpp

  1. #pragma once
  2. #include "LayerModel.hpp"
  3. #include "Layer.hpp"
  4. namespace nn
  5. {
  6. extern internal::LayerModel AllToAll(int numberOfNeurons, activationFunction activation = sigmoid);
  7. }

LayerFactory.cpp

  1. #include "LayerFactory.hpp"
  2. #include "AllToAll.hpp"
  3. using namespace nn;
  4. using namespace internal;
  5. LayerModel AllToAll(int numberOfNeurons, activationFunction activation)
  6. {
  7. LayerModel model
  8. {
  9. allToAll,
  10. activation,
  11. numberOfNeurons
  12. };
  13. return model;
  14. }

NeuralNetwork.hpp

  1. #pragma once
  2. #include "layer/LayerModel.hpp"
  3. #include "layer/LayerFactory.hpp"
  4. namespace nn
  5. {
  6. class NeuralNetwork
  7. {
  8. public:
  9. NeuralNetwork(int numberOfInputs, std::vector<internal::LayerModel> models);
  10. //...
  11. };
  12. }

Example.cpp

  1. #include "../src/neural_network/NeuralNetwork.hpp"
  2. using namespace nn;
  3. int example1()
  4. {
  5. NeuralNetwork neuralNetwork(3, {AllToAll(5), AllToAll(2)});
  6. }

错误消息:

  1. CMakeFiles/UnitTests.out.dir/ExamplesTest.cpp.o: In function `example1()':
  2. 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

  1. #pragma once
  2. #include &quot;LayerModel.hpp&quot;
  3. #include &quot;Layer.hpp&quot;
  4. namespace nn
  5. {
  6. extern internal::LayerModel AllToAll(int numberOfNeurons, activationFunction activation = sigmoid);
  7. }

LayerFactory.cpp

  1. #include &quot;LayerFactory.hpp&quot;
  2. #include &quot;AllToAll.hpp&quot;
  3. using namespace nn;
  4. using namespace internal;
  5. LayerModel AllToAll(int numberOfNeurons, activationFunction activation)
  6. {
  7. LayerModel model
  8. {
  9. allToAll,
  10. activation,
  11. numberOfNeurons
  12. };
  13. return model;
  14. }

NeuralNetwork.hpp

  1. #pragma once
  2. #include &quot;layer/LayerModel.hpp&quot;
  3. #include &quot;layer/LayerFactory.hpp&quot;
  4. namespace nn
  5. {
  6. class NeuralNetwork
  7. {
  8. public:
  9. NeuralNetwork(int numberOfInputs, std::vector&lt;internal::LayerModel&gt; models);
  10. //...
  11. };
  12. }

Example.cpp

  1. #include &quot;../src/neural_network/NeuralNetwork.hpp&quot;
  2. using namespace nn;
  3. int example1()
  4. {
  5. NeuralNetwork neuralNetwork(3, {AllToAll(5), AllToAll(2)});
  6. }

error message:

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

答案1

得分: 2

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

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

  1. using namespace foo;
  2. extern void Bar();

你需要:

  1. namespace foo {
  2. extern void Bar();
  3. }
英文:

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:

  1. using namespace foo;
  2. extern void Bar();

You need:

  1. namespace foo {
  2. extern void Bar();
  3. }

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:

确定