Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

static library reference resolving 1

Status
Not open for further replies.

zildjohn01

Programmer
Sep 6, 2005
10
US
static library reference resolving

hi, i have two projects ( a static library and a exe ) that i am trying to interface with each other. the

static lib contains the entry point, calls a function in the exe, then the exe must have access back to the

static lib's functions. here is the code (in two projects):

Project StaticLib
Code:
// [highlight]stdafx.h[/highlight]
[ignore]#include <iostream>
#include <tchar.h>
#include <windows.h>[/ignore]
#define EXPOSED __declspec(dllexport)


// [highlight]testclass.h[/highlight]
namespace test {
	class EXPOSED TestClass {
	public:
		static void DoSomething(int val);
	};
}

// [highlight]testclass.cpp[/highlight]
#include "stdafx.h"
#include "TestClass.h"

namespace test {
	void TestClass::DoSomething(int val) {
		std::cout << "in func, val == " << val << "\n";
	}
}


// [highlight]entrypoint.cpp[/highlight]
#include "stdafx.h"

int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);
int TestMain(HINSTANCE hInstance);

int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	return TestMain(hInstance);
}


// [highlight]include.h[/highlight]
#include "stdafx.h"

#undef EXPOSED
#define EXPOSED __declspec(dllimport)
// now exposed means import, and the declarations can be reused
// to import to another project

#include "TestClass.h"

Project Client
[maroon]A reference was added to project StaticLib[/maroon]
Code:
// [highlight]main.cpp[/highlight]
#include "..\StaticLib\include.h"

int TestMain(HINSTANCE hInstance) {
	test::TestClass::DoSomething(42);
	return 50;
}

Partial output from the build: (wrapped for readability):

Code:
------ Build started: Project: Client, Configuration: Debug Win32 ------

Compiling...
main.cpp
Linking...
main.obj : error LNK2019: unresolved external symbol
"__declspec(dllimport) public: static void __cdecl test::TestClass::DoSomething(int)"
(__imp_?DoSomething@TestClass@test@@SAXH@Z)
referenced in function "int __cdecl TestMain(struct HINSTANCE__ *)"
(?TestMain@@YAHPAUHINSTANCE__@@@Z)
Debug/Client.exe : fatal error LNK1120: 1 unresolved externals

wow, i never thought i'd make a post that long [tongue]
 
You are writing a static library so you do not need the to use EXPOSED or the definition of EXPOSED. That is only required for dynamic link libraries.

Just remove all definitions/undefinitions and occurrences of EXPOSED from your code and it will link and run just fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top