Sign in to follow this  
5h466y

Compiler warning C4251 : visual studio 2005

Recommended Posts

5h466y    127
hello, i have a little problem with my vs2k5 compiler, i basically have a C4251 warning with this code :
//File: XEntity.h
//Contents: XEntity declarations
#pragma once
#include "Common.h"
#include "XGMGraphics.h"

//XEntity: base class of entity hierarchy
class XGM_API XEntity
{
private:
	//Position
	XGMGVECTOR3	m_vPosition ;
	//D3D Device 
	LPDIRECT3DDEVICE9& m_pD3DDevice ;
protected:
	//Constructor
	XEntity (XGMGVECTOR3 &,
			 XGMGraphics &) ;
	//Destructor
	virtual ~XEntity (VOID) ;
public:
	//Get position
	XGMGVECTOR3 GetPosition (VOID) ;
	//Set position
	VOID SetPosition (XGMGVECTOR3 &) ;
	//Render entity
	virtual VOID Render (VOID) = 0 ;
};

typedef XEntity *LPXENTITY ;
i did this in this files :
//File: Common.h
//Contents: Common declarations
#include <d3d9.h>
#include <d3dx9.h>

#define XGM_API	_declspec(dllexport)

//File: XGMGraphics.h
//Contents: XGMGraphics declarations
#pragma once
#include "Common.h"

//typedefs
XGM_API typedef D3DXVECTOR3 XGMGVECTOR3 ;
XGM_API typedef D3DXVECTOR2 XGMGVECTOR2 ;

...
i made an exported D3DXVECTOR3 type and i used it on XEntity class for solving the problem, but stills giving me that warning... my question is, did i do something wrong? do i have to necessarily do ->
#pragma warning( disable: 4251 )
for getting rid of this ?? any help will be preciated :):) thx ... i read some documentation about it and based on what i readed i think i did the right thing ... again: any help will be preciated, thx :):) btw: here's compiler output for XEntity.cpp
Compiling...
XEntity.cpp
p:\xgm\xgm\xentity.h(16) : warning C4251: 'XEntity::m_vPosition' : struct 'D3DXVECTOR3' needs to have dll-interface to be used by clients of class 'XEntity'
        d:\dxsdk\include\d3dx9math.h(162) : see declaration of 'D3DXVECTOR3'
Build log was saved at "file://p:\xGm\xGm\Debug\BuildLog.htm"
xGm - 0 error(s), 1 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Share this post


Link to post
Share on other sites
ApochPiQ    23064
What is the structure of your project? Are you building a DLL for use in other code? What's the purpose of the dllexport in your header file?

If XEntity is meant to be exported to/from a DLL (hence the _declspec(dllexport) on the class) you will also have to export all of the types it uses (XGMGVECTOR3, for instance, which you've got typedeffed to D3DXVECTOR3). Merely dllexporting the typedef isn't considered enough because typedefs are not added to a DLL's symbol table (AFAIK), whereas class and type names are.


The reason you get a warning for this is because you'd have to manually make sure that the client DLL code always uses the exact same headers (and therefore the exact same D3DXVECTOR3 class) as the main code. Obviously since D3DXVECTOR3 doesn't exactly change every day this isn't a major concern, but it still represents a potentially dangerous assumption.

Share this post


Link to post
Share on other sites
5h466y    127
Quote:
Original post by ApochPiQ
What is the structure of your project? Are you building a DLL for use in other code? What's the purpose of the dllexport in your header file?

If XEntity is meant to be exported to/from a DLL (hence the _declspec(dllexport) on the class) you will also have to export all of the types it uses (XGMGVECTOR3, for instance, which you've got typedeffed to D3DXVECTOR3). Merely dllexporting the typedef isn't considered enough because typedefs are not added to a DLL's symbol table (AFAIK), whereas class and type names are.


The reason you get a warning for this is because you'd have to manually make sure that the client DLL code always uses the exact same headers (and therefore the exact same D3DXVECTOR3 class) as the main code. Obviously since D3DXVECTOR3 doesn't exactly change every day this isn't a major concern, but it still represents a potentially dangerous assumption.


yes, it is dll for use in another program's code...
i do understand that typedefing it is not enough for exporting those types ,but, what else do i have to do for exporting those types and make some good dll integrity ???

Share this post


Link to post
Share on other sites
ApochPiQ    23064
Strictly speaking, you would have to add the _declspec(dllexport) or _declspec(dllimport) to every type from the DirectX headers that you use. This is obviously not really practical, though. The other option is to use your own vector and other structures, and provide an interface internally to your DLL that converts them to the correct DirectX format. This is going to be mandatory if you're aiming for API-independence anyways, but if you're only targeting DirectX, it could be wasteful. A fair few 3D engines use such wrapper code; the costs are minimal, but they can help make the code cleaner and easier to read, especially when working across DLL boundaries. It really comes down to personal preference.

One thing you might try is explicitly exporting each type separately from the DirectX headers. I don't know if this is even valid, it just came to mind randomly, so no promises - but it's worth a shot:

#include <d3d9.h>
#include <d3dx9.h>

#define XGM_API _declspec(dllexport)

XGM_API struct D3DXVECTOR3;



If that doesn't work... You should in theory be safe as long as you're careful to use the same headers for both projects, but the compiler doesn't have any way to know that, so I don't think there's a way to get rid of the warning aside from #pragma.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this