Index: trunk/ab5.0/abdev/compiler_x86/Compile_ProcOp.cpp
===================================================================
--- trunk/ab5.0/abdev/compiler_x86/Compile_ProcOp.cpp	(revision 632)
+++ trunk/ab5.0/abdev/compiler_x86/Compile_ProcOp.cpp	(revision 636)
@@ -328,6 +328,24 @@
 	}
 
+	char *backupBasbuf = NULL;
+	int backupCurrentRelationalObjectModuleIndexForSource = compiler.GetCurrentRelationalObjectModuleIndexForSource();
+
 	if( !pUserProc->IsAutoGeneration() )
 	{
+		// テンプレート展開がされている場合、対象のソースコードが現在のオブジェクトモジュールではない場合がある
+		if( compiler.staticLibraries[compiler.GetCurrentRelationalObjectModuleIndexForSource()]->GetName() != pUserProc->GetSourceCodePosition().GetObjectModuleName() )
+		{
+			for( int i=0; i<static_cast<int>(compiler.staticLibraries.size()); i++ )
+			{
+				const ObjectModule *pObjectModule = compiler.staticLibraries[i];
+
+				if( pObjectModule->GetName() == pUserProc->GetSourceCodePosition().GetObjectModuleName() )
+				{
+					compiler.SetCurrentRelationalObjectModuleIndexForSource( i );
+					basbuf = const_cast<char *>(compiler.GetCurrentSource().GetBuffer());
+				}
+			}
+		}
+
 		cp=pUserProc->GetSourceCodePosition().GetPos();
 		for(;;cp++){
Index: trunk/ab5.0/abdev/compiler_x86/MakePeHdr.cpp
===================================================================
--- trunk/ab5.0/abdev/compiler_x86/MakePeHdr.cpp	(revision 632)
+++ trunk/ab5.0/abdev/compiler_x86/MakePeHdr.cpp	(revision 636)
@@ -120,4 +120,5 @@
 
 	// 静的リンク
+	compiler.PreStaticLink( compiler.staticLibraries );
 	compiler.StaticLink( compiler.staticLibraries );
 
@@ -144,5 +145,5 @@
 	{
 		ActiveBasic::Compiler::LexicalAnalyzer::CollectEnums(
-			compiler.GetObjectModule().GetCurrentSource().GetBuffer(),
+			compiler.GetCurrentSource().GetBuffer(),
 			compiler.enumInfoCollection
 		);
@@ -157,5 +158,5 @@
 	// 名前空間情報を取得
 	ActiveBasic::Compiler::LexicalAnalyzer::CollectNamespaces(
-		compiler.GetObjectModule().GetCurrentSource().GetBuffer(),
+		compiler.GetCurrentSource().GetBuffer(),
 		compiler.GetObjectModule().meta.GetNamespaces()
 	);
@@ -164,5 +165,5 @@
 	{
 		ActiveBasic::Compiler::LexicalAnalyzer::CollectDelegates(
-			compiler.GetObjectModule().GetCurrentSource().GetBuffer(),
+			compiler.GetCurrentSource().GetBuffer(),
 			compiler.GetObjectModule().meta.GetDelegates()
 		);
@@ -180,5 +181,5 @@
 	//     ※オブジェクトの内容までは取得しない
 	ActiveBasic::Compiler::LexicalAnalyzer::CollectClassesForNameOnly(
-		compiler.GetObjectModule().GetCurrentSource().GetBuffer(),
+		compiler.GetCurrentSource().GetBuffer(),
 		compiler.GetObjectModule().meta.GetClasses()
 	);
@@ -186,5 +187,5 @@
 	//TypeDef情報を収集
 	ActiveBasic::Compiler::LexicalAnalyzer::CollectTypeDefs(
-		compiler.GetObjectModule().GetCurrentSource().GetBuffer(),
+		compiler.GetCurrentSource().GetBuffer(),
 		compiler.GetObjectModule().meta.GetTypeDefs()
 	);
@@ -201,5 +202,5 @@
 	//定数情報を取得
 	ActiveBasic::Compiler::LexicalAnalyzer::CollectConsts(
-		compiler.GetObjectModule().GetCurrentSource().GetBuffer(),
+		compiler.GetCurrentSource().GetBuffer(),
 		compiler.GetObjectModule().meta.GetGlobalConsts(),
 		compiler.GetObjectModule().meta.GetGlobalConstMacros()
@@ -209,5 +210,5 @@
 	compiler.SetCompilingClass( NULL );
 	ActiveBasic::Compiler::LexicalAnalyzer::CollectProcedures(
-		compiler.GetObjectModule().GetCurrentSource().GetBuffer(),
+		compiler.GetCurrentSource().GetBuffer(),
 		compiler.GetObjectModule().meta.GetUserProcs(),
 		compiler.GetObjectModule().meta.GetDllProcs()
@@ -216,5 +217,5 @@
 	// クラス情報を取得（※注 - CollectProceduresの後に呼び出す）
 	ActiveBasic::Compiler::LexicalAnalyzer::CollectClasses(
-		compiler.GetObjectModule().GetCurrentSource().GetBuffer(),
+		compiler.GetCurrentSource().GetBuffer(),
 		compiler.GetObjectModule().meta.GetClasses()
 	);
