Thread: Problem with dll, template and library dir

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    14

    Problem with dll, template and library dir

    I have a linking problem, i have a project that compiles in a dll:

    header:
    Code:
    #ifndef MESH_TOOL_H
    #define MESH_TOOL_H
    
    #include "Ogre.h"
    
    namespace Game
    {
    	class _declspec(dllexport) MeshTool : public Ogre::Singleton<MeshTool>
    	{
    		public:
    		MeshTool();
    		~MeshTool();
    
    		void GetMeshInformation(const Ogre::Entity* const ent,
    			size_t &vertex_count,
    			Ogre::Vector3* &vertices,
    			size_t &index_count,
    			unsigned long* &indices,
    			const Ogre::Vector3 &position,
    			const Ogre::Quaternion &orient,
    			const Ogre::Vector3 &scale);
    	};
    }
    #endif
    module:
    Code:
    #include "MeshTool.h"
    
    using namespace Ogre;
    
    template<> Game::MeshTool* Ogre::Singleton<class Game::MeshTool>::ms_Singleton = 0;
    
    namespace Game
    {
    	MeshTool::MeshTool()
    	{
    
    	};
    
    	MeshTool::~MeshTool()
    	{
    
    	};
    
    	void MeshTool::GetMeshInformation(const Entity* const ent,
    		size_t &vertex_count,
    		Vector3* &vertices,
    		size_t &index_count,
    		unsigned long* &indices,
    		const Vector3 &position,
    		const Quaternion &orient,
    		const Vector3 &scale)
    	{
    		Implementation....
    	}
    }
    from another project i use the dll and the header, when i compile the project i receive this error:
    Code:
    Error	6	error LNK2001: unresolved external symbol "protected: static class Game::MeshTool * Ogre::Singleton<class Game::MeshTool>::ms_Singleton" (?ms_Singleton@?$Singleton@VMeshTool@Game@@@Ogre@@1PAVMeshTool@Game@@A)	PhysicsEngine.obj	PhysEng
    The Ogre::Singleton is:
    Code:
    #ifndef _SINGLETON_H__
    #define _SINGLETON_H__
    
    // Added by Steve Streeting for Ogre
    #include "OgrePrerequisites.h"
    
    #if OGRE_COMPILER == OGRE_COMPILER_MSVC
    // Turn off warnings generated by this singleton implementation
    #   pragma warning (disable : 4311)
    #   pragma warning (disable : 4312)
    #endif
    
    #if defined ( OGRE_GCC_VISIBILITY )
    #   pragma GCC visibility push(default)
    #endif
    namespace Ogre {
    // End SJS additions
        /** Template class for creating single-instance global classes.
        */
        template <typename T> class Singleton
        {
        protected:
    
            static T* ms_Singleton;
    
        public:
            Singleton( void )
            {
                assert( !ms_Singleton );
    #if defined( _MSC_VER ) && _MSC_VER < 1200	 
                int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1;
                ms_Singleton = (T*)((int)this + offset);
    #else
    	    ms_Singleton = static_cast< T* >( this );
    #endif
            }
            ~Singleton( void )
                {  assert( ms_Singleton );  ms_Singleton = 0;  }
            static T& getSingleton( void )
    		{	assert( ms_Singleton );  return ( *ms_Singleton ); }
            static T* getSingletonPtr( void )
    		{ return ms_Singleton; }
        };
    }
    #if defined ( OGRE_GCC_VISIBILITY )
    #   pragma GCC visibility pop
    #endif
    #endif
    so i don't understand why it gives me that error.. i'm using that pointer (ms_Singleton) in PhysicsEngine in this way:
    Code:
    MeshTool::getSingletonPtr()->GetMeshInformation(ent, vertex_count, vertices, index_count, indices, pos ,orient , scale);
    Obviously i added MeshTools.lib in PhysicsEngine project and also the include directories.. so should i export in some way the template/property? how to do it?.

    And another different question.. srry to mix things.. why Visual Studio never finds library or includes if i add their directories in Project->Options, but when i do it in Tools->Options etc it works?.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I would first try explicitly instantiating and exporting the base, "Ogre::Singleton<MeshTool>".

    Example code on how to do this on the last bullet here: General Rules and Limitations

    gg

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Thnx for the link, but if i read it i can find:

    Code:
    template class __declspec(dllexport) B<int>;
    class __declspec(dllexport) D : public B<int> {
    // ...
    This workaround fails if the template argument is the deriving class. For example: <--- should be my case
    Code:
    class __declspec(dllexport) D : public B<D> {
    // ...
    Because this is common pattern with templates, the compiler changed the semantics of dllexport when it is applied to a class that has one or more base-classes and when one or more of the base classes is a specialization of a class template. In this case, the compiler implicitly applies dllexport to the specializations of class templates. In Visual C++ .NET, a user can do the following and not get a warning:
    Code:
    class __declspec(dllexport) D : public B<D> {
    // ...
    As you can see it says that Visual C++ should export it automatically... but i still have problems.
    Also declaring the template before the class delcaration is not possible...

    Edit: found that it says Visual C++ .Net and i'm using unmanaged C++, anyway the problem is still there since there's no way to declare it before.. and declare it after it makes it compile, but doesn't solve anything.
    Edit2: reading more carefully the page i found that it tells me to declare Ogre::Singleton with dllexport (since is B in the example) but i can't do that... it's a header of the Ogre3D engine... i cannot change it otherwise other class that derives from it will be affected.
    Last edited by Smjert; 08-12-2009 at 02:09 PM.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    "Visual C++ .Net" is a product name - implying a particular version of the VC++ compiler. In this case, it's the version that came after VC6.0 and before VC2005.

    >> As you can see it says that Visual C++ should export it automatically...
    That depends on the version - What version are you using?

    For VC6.0, you would have to do something like this:
    Code:
    // In header...
    #if defined(BUILDING_DLL)
    #   define MESH_TOOL_API __declspec(dllexport)
    #else
    #   define MESH_TOOL_API __declspec(dllimport)
    #endif
    
    class MESH_TOOL_API MeshTool; // forward decl.
    template class MESH_TOOL_API Ogre::Singleton<MeshTool>; // export of explicit instantiation 
    
    ....
    // In dll Source
    template<> MESH_TOOL_API Game::MeshTool* Ogre::Singleton<Game::MeshTool>::ms_Singleton = 0;
    >> class _declspec(dllexport) MeshTool ...
    If you aren't already using something like "MESH_TOOL_API" - do so.

    gg

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    I'm using Visual Studio 2008, i tried it with forward declaration (forgot about it thnx!), it compiles but it still gives the same linking error.
    Tomorrow i'll try to find more about it .

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Ok i realized it myself, you already gave me the right solution but i interpreted it so i haven't done what you said (i didn't realize that defining MESH_TOOL_API when i use the header from the other project it does dllimport automatically).
    Anyway now it works, even without the forward class declaration trick (so only exporting the class that derives from the Singleton).
    Thnx!.

    Now the last thing to understand is the visual studio problem about library/include directories..
    Last edited by Smjert; 08-13-2009 at 03:51 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 01:49 AM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM