Static library and namespaces

Started by
12 comments, last by Zakwayda 15 years, 5 months ago
Hello. I'm making a math library and I would like to ude a namespace for it. but when I implmented it i got around 500 errors. program\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(234) : error C3083: 'vc_attributes': the symbol to the left of a '::' must be a type I don't know why?? I guess there is something with static library settings or something. anyways this is how i included the namespaces //Header files

//the main header, no class

#ifndef _MYMATH_H_
#define _MYMATH_H_
namespace MyMath
{
	//forward declarations
	class CVector3;
	class CVector4;
	class CMatrix4;
	class CQuaternion;


#include "math.h"
#include "CVector3.h"
#include "Vector4.h"
#include "CMatrix4.h"
#include "CQuaternion.h"
}
#endif //_MYMATH_H_



//Matrix header
#ifndef _CMATRIX4_H_
#define _CMATRIX4_H_
#include "MyMath.h"

namespace MyMath
{
	//!  CMatrix4 Class. 
	/*!
	CMatrix4 is row major matrix class for 3D programming
	*/
	class ECMatrix4 
	{
          ..........
        };
}

//vector3 header
#ifndef _CVECTOR3_H_
#define _CVECTOR3_H_
#include "MyMath.h"

namespace MyMath
{
	//!  CVector3 Class. 
	/*!
	CVector3 is a 3D vector class for 3D programming
	*/
	class CVector3 
	{
         .....
        };
}
//end  vector3

and I do this in very class
Cpp files are the same

#include "StdAfx.h"
#include "MyMath.h"

namespace MyMath
{
......cpp function etc
}





Do I have to do something special to make namespaces work when dealing with a static libraty. MSDN show how to use name spaces in a static library project in VS 2005. Don't see why id don't work in VC 2008. //HermanssoN [Edited by - HermanssoN on November 7, 2008 1:16:38 AM]
Advertisement
I don't have an answer to your question (at least not without looking more closely), but I have a couple of questions/suggestions:

1. Edit your original post and enclose your code in [source] tags; this will make it easier to read.

2. It would be better to use a different convention for your header guards; symbols that begin with an underscore followed by a capital letter are reserved by the implementation.

3. What does the 'C' prefix in your class names stand for?

It does look like you might have some strange (perhaps circular) inclusions going on there - perhaps that's where the errors are coming from.

CMyClass, C stand for class, a quite normal prefix?

Ib'e always used _CLASSNAME_H_ for my include guards, but i supose I could use

__CLASSNAME_H__ instead

all classes you see declared in the main header include "MyMath.h" in their h/cpp file.

The error only occure when I added the namespaceif. If I remove the namepace tags from the code I can use the library in other apps. I just world really like to have the nampespace there
FWIW. Do not use double underscores (i.e. __SOMETHING__). Double underscores are reserved by the compiler and should not be used. It is better to not use any underscores, or better yet use pragma once since its supported by virtually every compiler out there.
#pragma oncenamespace MyMath{ //... stuff}

Note: You can use both include guards and pragma once if you want, but often its redundant.
#pragma once#ifndef HEADER_H#define HEADER_Hnamespace MyMath{ //... stuff}#endif /* HEADER_H */
I think the problem is that you are including you other headers inside you namespace. You should put your #includes BEFORE or AFTER namespace MyMath:

#ifndef MYMATH_H_#define MYMATH_H_namespace MyMath{	//forward declarations	class CVector3;	class CVector4;	class CMatrix4;	class CQuaternion;}#include <cmath> // don't use math.h, it's C header file, C++ has cmath#include "CVector3.h"#include "Vector4.h"#include "CMatrix4.h"#include "CQuaternion.h"#endif //MYMATH_H_
Thnx..good to know.

Found the error, I included "math.h" inside my name space

Now i hit a new error. I lost my Quaternions.

if i have one name space there i include header

#pragma oncenamespace MyMath{ #include "Vector3.h" etc}


//Vector3 header#pragma once#include "MyMath.h"namespace MyMath{ class Vector3{}; etc}


//Vector3 cpp#pragma once#include "MyMath.h"namespace MyMath{Vector3::Vector3( void ){}etc


This should make Vector3 a part of namespace MyMath, right?
And if I do this with every class that I would like to include in my math library all classes can declare varibles of each other as long as they share the same namespace?

[Edited by - HermanssoN on November 7, 2008 8:08:05 AM]
Quote:Original post by HermanssoN

CMyClass, C stand for class, a quite normal prefix?


What's wrong with just MyClass?

Prefixes stem from C which lacks namespaces and stricter code organization. Use of CName is abundant on web simply because many C++ developers transitioned from C.

Quote:Ib'e always used _CLASSNAME_H_ for my include guards, but i supose I could use

__CLASSNAME_H__ instead


Double underscores are reserved for the compiler.

Why not just use CLASSNAME_H? Or NAMESPACE_CLASSNAME_H? Or, if you fear conflicts CLASSNAME_H_CURRENTDATEANDTIME.

Leading underscores are commonly seen in code examples, but they all violate the standard in same way and aren't really needed.

If conflicts become a problem, it's much better that they conflict with your own code in predictable way, than with compiler in unknown way.
Remember, #inlcude is a copy and paste operation. This is what the compiler sees, after preprocessing:
namespace MyMath{    namespace MyMath    {        class Vector3        {        };    }}

Thus Vector3 ends up in MyMath::MyMath. And because Vector3.h can be included on its own, you can potentially end up with MyMath::Vector3 and MyMath::MyMath::Vector3. This is not a good idea.

The correct solution:
// mymath.h#ifndef MYMATH_H#define MYMATH_H// note: absence of namespace#include "vector3.h"#include "more_maths.h"// more includes...#endif// vector3.h#ifndef MYMATH_VECTOR3_H#define MYMATH_VECTOR3_H// don't include "mymath.h"namespace MyMath{     struct Vector3     {         // ...     };}#endif// and so on for other files
The problem is that vector3 will have funtions that uses a matrix, matrix will have functions that that uses both vectors and quaternions.

MY original soultion was to include all math classes in MyMayh.h and also make forward declarations there.

as an example if i include Vector.h and quaternion.h in matrix.h
and include matrix.h in vector.h, won't I have a bunch of headers that get included several times? or does the include guards make sure that i won't end up with multiple includes of the same header

This topic is closed to new replies.

Advertisement