mercredi 24 août 2022

Why is this template instantiated in two different types from the same typedef? [duplicate]

The title might be misguiding, but I had to give one, so let's at least base it on my suspicions.

I have a program that depends on this type and the following functions, declared in one header file (in this case, named particle.hpp) and implemented in a source file (particle.cpp):

using World = std::vector<Particle>;

World* initWorld();
void update(World* w, double dt);
void draw(World* w);
void destroyWorld(World* w);

These functions compile fine. The given files are correctly included and recognized by the build system, according to all the tests I've made to ascertain it. However, when they are used in the rest of my code, ld fails with the following:

main.cpp:(.text+0x65): undefined reference to `initWorld()'
/usr/bin/ld: main.cpp:(.text+0xa6): undefined reference to `update(std::vector<._anon_156, std::allocator<._anon_156> >*, double)'
/usr/bin/ld: main.cpp:(.text+0xdb): undefined reference to `draw(std::vector<._anon_156, std::allocator<._anon_156> >*)'
/usr/bin/ld: main.cpp:(.text+0xf9): undefined reference to `destroyWorld(std::vector<._anon_156, std::allocator<._anon_156> >*)'

Stranger yet, g++ gives these warnings:

/home/whoanders/projects/particle/mainloop.hpp:12:6: warning: ‘void destroyWorld(World*)’ used but never defined
   12 | void destroyWorld(World* w);
      |      ^~~~~~~~~~~~
/home/whoanders/projects/particle/mainloop.hpp:11:6: warning: ‘void draw(World*)’ used but never defined
   11 | void draw(World* w);
      |      ^~~~
/home/whoanders/projects/particle/mainloop.hpp:10:6: warning: ‘void update(World*, double)’ used but never defined
   10 | void update(World* w, double dt);
      |      ^~~~~~
/home/whoanders/projects/particle/mainloop.hpp:9:8: warning: ‘World* initWorld()’ used but never defined

This is the core of my problem: these functions compile correctly, but are not recognized in other files. What is suspect to be the source of the problem is the definition of type World. In ld's warnings, it does not seem to be defined as a std::vector of a Particle, but rather as that of an anonymous type, which leads me to believe that the compiler thinks the definition of a World is different between the various files of the source tree, and that because of that, it doesn't see the functions as defined in particle.cpp and those used in the rest of the code as one and the same.

My question is, if this explanation is correct, why does this problem surface and how do I solve it?

Aucun commentaire:

Enregistrer un commentaire