static libs

The Partridge Family were neither partridges nor a family. Discuss.
MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

static libs

Post by MrGodin » March 2nd, 2017, 7:11 pm

Hey guys, for those of yu who build their own libs, i have a question. I've made a lib for my 2D stuff i use a lot but i get a linker error with the matrix. I've made a Debug and release build for the lib in x64.
seems when i use the static functions (I've followed the MSDN example on static libs and the have a class with static members), I get linker unresolved errors to Matrix3x3::Identity(), and the like.
I've used the chili matrix from the Thrust series

Code: Select all

////////////////////////////////////////////////////
	// Matrix 3X3
	/////////////////////////////////////////////////////
	class Matrix3x3
	{
	public:
// added these after first linker error
		Matrix3x3() = default;
		Matrix3x3(float e0, float e1, float e2, float e3, float e4, 
			float e5, float e6, float e7, float e8 ) 
		{
			
			elements[0][0] = e0;
			elements[0][1] = e1;
			elements[0][2] = e2;

			elements[1][0] = e3;
			elements[1][1] = e4;
			elements[1][2] = e5;
			
			elements[2][0] = e6;
			elements[2][1] = e7;
			elements[2][2] = e8;


		}
		Matrix3x3& operator =(const Matrix3x3& rhs);
		Matrix3x3& operator *=(const float rhs);
		Matrix3x3 operator *(const float rhs) const;
		Matrix3x3 operator *(const Matrix3x3& rhs) const;
		Matrix3x3& operator *=(const Matrix3x3& rhs);
		
		static Matrix3x3 Identity();
		static Matrix3x3 Rotation(float theta);
		static Matrix3x3 Scaling(float factor);
		static Matrix3x3 Translation(const Vector2D offset);
		Vector2D operator *(const Vector2D rhs) const;
	public:
		// [ row ][ col ]
		float elements[3][3];
	};
In the cpp

Code: Select all

/////////////////////////////////////////////////////
	// Matrix 3x3
	////////////////////////////////////////////////////////
	Matrix3x3 & Matrix3x3::operator=(const Matrix3x3 & rhs)
	{
		memcpy(elements, rhs.elements, sizeof(elements));
		return *this;
	}

	Matrix3x3 & Matrix3x3::operator*=(const float rhs)
	{
		for (auto& row : elements)
		{
			for (float& e : row)
			{
				e *= rhs;
			}
		}
		return *this;
	}

	Matrix3x3 Matrix3x3::operator*(const float rhs) const
	{
		Matrix3x3 result = *this;
		return result *= rhs;
	}

	Matrix3x3 Matrix3x3::operator*(const Matrix3x3 & rhs) const
	{
		Matrix3x3 result;
		for (int j = 0; j < 3; j++)
		{
			for (int k = 0; k < 3; k++)
			{
				float sum = 0.0f;
				for (int i = 0; i < 3; i++)
				{
					sum += elements[j][i] * rhs.elements[i][k];
				}
				result.elements[j][k] = sum;
			}
		}
		return result;
	}

	Matrix3x3 & Matrix3x3::operator*=(const Matrix3x3 & rhs)
	{
		return *this = *this * rhs;
	}

	Vector2D Matrix3x3::operator*(const Vector2D rhs) const
	{
		Vector2D result;
		result.x = elements[0][0] * rhs.x + elements[0][1] * rhs.y + elements[0][2];
		result.y = elements[1][0] * rhs.x + elements[1][1] * rhs.y + elements[1][2];
		return result;
	}

	Matrix3x3 Matrix3x3::Identity()
	{
		Matrix3x3 i = { 1.0f,0.0f,0.0f,0.0f,1.0f,0.0f,0.0f,0.0f,1.0f };
		return i;
	}

	Matrix3x3 Matrix3x3::Rotation(float theta)
	{
		const float cosTheta = cos(theta);
		const float sinTheta = sin(theta);
//. tried fucking around here with no success
		Matrix3x3 r = { cosTheta,-sinTheta,0.0f,sinTheta,cosTheta,0.0f,0.0f,0.0f,1.0f };
		return Matrix3x3(cosTheta, -sinTheta, 0.0f, sinTheta, cosTheta, 0.0f, 0.0f, 0.0f, 1.0f);
	}

	Matrix3x3 Matrix3x3::Scaling(float factor)
	{
		Matrix3x3 s = { factor,0.0f,0.0f,0.0f,factor,0.0f,0.0f,0.0f,1.0f };
		return s;
	}

	Matrix3x3 Matrix3x3::Translation(const Vector2D offset)
	{
		Matrix3x3 t = { 1.0f,0.0f,offset.x,0.0f,1.0f,offset.y,0.0f,0.0f,1.0f };
		return t;
	}
In the Game.cpp

Code: Select all

#ifdef _DEBUG
#pragma comment(lib,"lib2D_d.lib")
#else
#pragma comment(lib,"lib2D_r.lib")
#endif
Curiosity killed the cat, satisfaction brought him back

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: static libs

Post by LuisR14 » March 3rd, 2017, 5:14 am

i believe you have to __declspec(dllexport) the class
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: static libs

Post by albinopapa » March 3rd, 2017, 6:00 am

Luis, does that apply to .lib files as well? I've made a few .lib files, but I haven't apparently ever used a static member function. I think I have used static variables and callback functions, but never static member functions.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: static libs

Post by MrGodin » March 3rd, 2017, 11:33 am

LuisR14 wrote:i believe you have to __declspec(dllexport) the class
but i have a vector, rect, sphere, AABB, and other classes that have no statics and work fine, what is confusing me is this..
https://msdn.microsoft.com/en-us/library/ms235627.aspx
The demo uses static
Curiosity killed the cat, satisfaction brought him back

User avatar
cyboryxmen
Posts: 190
Joined: November 14th, 2014, 2:03 am

Re: static libs

Post by cyboryxmen » March 3rd, 2017, 4:29 pm

LuisR14 wrote:i believe you have to __declspec(dllexport) the class
You only need to do that with dlls. I've tried to compile your code in my library and it compiles fine. Did you forget to set up the path for your static libraries in the main project?

https://smuswg.dm2302.livefilestore.com ... png?psid=1
Zekilk

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: static libs

Post by albinopapa » March 3rd, 2017, 4:47 pm

Either their or in the Visual C++ Directories under Library Directories
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: static libs

Post by cameron » March 3rd, 2017, 5:23 pm

I guess you probably dont need it unless using a dll. But dlls are really the way to go if distributing a library.
Computer too slow? Consider running a VM on your toaster.

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: static libs

Post by MrGodin » March 3rd, 2017, 5:31 pm

cyboryxmen wrote:
LuisR14 wrote:i believe you have to __declspec(dllexport) the class
You only need to do that with dlls. I've tried to compile your code in my library and it compiles fine. Did you forget to set up the path for your static libraries in the main project?

https://smuswg.dm2302.livefilestore.com ... png?psid=1
It compiles fine, I can call an instance of the Matrix class, i just can't use the static functions. I have put the libs right into the project directory along with all the other project headers and cpp files and linked them using #pragma comment .. The header for the lib is there too. If i put the lib cpp file there and use it without the libs it works fine..Oh well
Curiosity killed the cat, satisfaction brought him back

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: static libs

Post by albinopapa » March 3rd, 2017, 6:09 pm

@Cameron: Don't you still need the lib file during development? Isn't that why you get lib files and dll files for other libraries like zlib?
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: static libs

Post by cameron » March 3rd, 2017, 8:15 pm

If you are making a dll then dllexport is needed. And yea the lib is used when deving to link the dll to code. But dll is better because when the library is used in an app the whole lib gotta be exported with it. Whereas a dll only has the code needed.
Computer too slow? Consider running a VM on your toaster.

Post Reply