diff --git a/compiler2/makefile.c b/compiler2/makefile.c
index 762ae90c0a2fec51ffa961e351ca84cd2a82d257..4b3636573c58ea7b76da59ac4d9843d441837555 100644
--- a/compiler2/makefile.c
+++ b/compiler2/makefile.c
@@ -3961,6 +3961,13 @@ static void print_makefile(struct makefile_struct *makefile)
         }
       }
     }
+    
+    for (i = 0; i < makefile->nBaseDirs; i++) {
+      fprintf(fp, "\n\n%s/%%.o: %s/%%.c\n"
+        "\t$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) -o $@ $<\n\n", makefile->BaseDirs[i].dir_name, makefile->BaseDirs[i].dir_name);
+      fprintf(fp, "%s/%%.o: %s/%%.cc\n"
+            "\t$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) -o $@ $<", makefile->BaseDirs[i].dir_name, makefile->BaseDirs[i].dir_name);
+    }
     fputs("\n\n%.o: %.c $(GENERATED_HEADERS)\n"
           "\t$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) -o $@ $<\n\n", fp);
     fputs("%.o: %.cc $(GENERATED_HEADERS)\n"
diff --git a/compiler2/xpather.cc b/compiler2/xpather.cc
index 0d5c0a74b5b9ff5be12d6c69cb72ec7a19fe7ca7..55ac07a2d5a5981c3c3dc674819f716418be0a20 100644
--- a/compiler2/xpather.cc
+++ b/compiler2/xpather.cc
@@ -3038,6 +3038,8 @@ static tpd_result process_tpd_internal(const char *p_tpd_name, char *tpdName, co
               } else {
                 ERROR("Internal error: cannot add command to list");
               }
+            } else {
+              Free(command);
             }
             // add working dir to the end of list
             if (sub_project_dirs) {
diff --git a/regression_test/compileonly/mfgen-tpd/Makefile b/regression_test/compileonly/mfgen-tpd/Makefile
index be7d1d3b4b5bb3b47328b8d6dea71f5be78a2940..4970bc56aa08a60710f8842ccd74b09cf6577c57 100644
--- a/regression_test/compileonly/mfgen-tpd/Makefile
+++ b/regression_test/compileonly/mfgen-tpd/Makefile
@@ -32,7 +32,7 @@ MAKEPROG := ${MAKE}
 DIRLIST := buildconfig_param invalid_buildconfig_param invalid_buildconfig_tpd \
 	HP79745 HQ56829 HQ56834 HQ56848 library HR30356 flagTest HR30365 dependency_check \
 	required_config required_config_v2 required_config_v3 disablePredefinedExternalFolder \
-	makefileScript splitting
+	makefileScript splitting compile_once
 
 # The default target.
 ifeq ($(findstring c,$(MFGEN_FLAGS)),) # no -c
diff --git a/regression_test/compileonly/mfgen-tpd/compile_once/Makefile b/regression_test/compileonly/mfgen-tpd/compile_once/Makefile
new file mode 100644
index 0000000000000000000000000000000000000000..91dc96b819f8a037f625db390141c6ebabd34424
--- /dev/null
+++ b/regression_test/compileonly/mfgen-tpd/compile_once/Makefile
@@ -0,0 +1,53 @@
+##############################################################################
+# Copyright (c) 2000-2017 Ericsson Telecom AB
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Eclipse Public License v1.0
+# which accompanies this distribution, and is available at
+# http://www.eclipse.org/legal/epl-v10.html
+#
+# Contributors:
+#   Szabo, Bence Janos
+#
+##############################################################################
+TOPDIR := ../../..
+include $(TOPDIR)/Makefile.regression
+
+ifdef LCOV
+COVERAGE_FLAG := -C
+endif
+
+
+MAKEPROG := ${MAKE}
+
+all: Recursive RecursiveAbsolute
+
+Recursive:
+	-rm -rf a/binDefault b/binDefault c/binDefault && \
+	cd c && $(TTCN3_DIR)/bin/ttcn3_makefilegen $(MFGEN_FLAGS) $(COVERAGE_FLAG) \
+	-t c.tpd -cVfgpl && cd binDefault && make shared_objects && \
+	cd ../../b && $(TTCN3_DIR)/bin/ttcn3_makefilegen $(MFGEN_FLAGS) $(COVERAGE_FLAG) \
+	-t b.tpd -cVfgpl && cd binDefault && make shared_objects && \
+	cd ../../a && $(TTCN3_DIR)/bin/ttcn3_makefilegen $(MFGEN_FLAGS) $(COVERAGE_FLAG) \
+	-t a.tpd -FTrcgpl && cd binDefault && make > compile.txt && \
+	if [ `grep -c "ModuleB\." compile.txt` -ne 4 ] || [ `grep -c "ModuleC\." compile.txt` -ne 5 ]; \
+	then echo "Makefilegen compile once test failed: Overall verdict: fail" && exit 1; fi
+
+RecursiveAbsolute:
+	-rm -rf a/binDefault b/binDefault c/binDefault && \
+	cd c && $(TTCN3_DIR)/bin/ttcn3_makefilegen $(MFGEN_FLAGS) $(COVERAGE_FLAG) \
+	-at c.tpd -cVfgpl && cd binDefault && make shared_objects && \
+	cd ../../b && $(TTCN3_DIR)/bin/ttcn3_makefilegen $(MFGEN_FLAGS) $(COVERAGE_FLAG) \
+	-at b.tpd -cVfgpl && cd binDefault && make shared_objects && \
+	cd ../../a && $(TTCN3_DIR)/bin/ttcn3_makefilegen $(MFGEN_FLAGS) $(COVERAGE_FLAG) \
+	-at a.tpd -FTrcgpl && cd binDefault && make > compile.txt && \
+	if [ `grep -c "ModuleB\." compile.txt` -ne 4 ] || [ `grep -c "ModuleC\." compile.txt` -ne 5 ]; \
+	then echo "Makefilegen compile once test failed: Overall verdict: fail" && exit 1; fi
+
+clean:
+	-rm -rf a/binDefault b/binDefault c/binDefault
+
+distclean: clean
+	-rm -f *.out
+
+.PHONY: all clean distclean Normal Recursive RecursiveAbsolute
+
diff --git a/regression_test/compileonly/mfgen-tpd/compile_once/a/a.tpd b/regression_test/compileonly/mfgen-tpd/compile_once/a/a.tpd
new file mode 100644
index 0000000000000000000000000000000000000000..ca25c0b0a1b05e511c5a5793589cad2a9ad06a85
--- /dev/null
+++ b/regression_test/compileonly/mfgen-tpd/compile_once/a/a.tpd
@@ -0,0 +1,49 @@
+<!--
+ Copyright (c) 2000-2017 Ericsson Telecom AB
+ All rights reserved. This program and the accompanying materials
+ are made available under the terms of the Eclipse Public License v1.0
+ which accompanies this distribution, and is available at
+ http://www.eclipse.org/legal/epl-v10.html
+
+ Contributors:
+  Szabo, Bence Janos – initial implementation
+-->
+<TITAN_Project_File_Information version="1.0">
+  <ProjectName>a</ProjectName>
+  <ReferencedProjects>
+    <ReferencedProject name="b" projectLocationURI="../b/b.tpd"/>
+    <ReferencedProject name="c" projectLocationURI="../c/c.tpd"/>
+  </ReferencedProjects>
+  <Folders>
+    <FolderResource projectRelativePath="src" relativeURI="src"/>
+  </Folders>
+  <Files>
+    <FileResource projectRelativePath="src/ModuleA.ttcn" relativeURI="src/ModuleA.ttcn"/>
+    <FileResource projectRelativePath="src/ModuleA2.ttcnpp" relativeURI="src/ModuleA2.ttcnpp"/>
+    <FileResource projectRelativePath="src/ModuleA3.asn" relativeURI="src/ModuleA3.asn"/>
+  </Files>
+  <ActiveConfiguration>None</ActiveConfiguration>
+  <Configurations>
+    <Configuration name="None">
+      <ProjectProperties>
+        <MakefileSettings>
+          <codeSplitting>none</codeSplitting>
+          <targetExecutable>binDefault/binDefault</targetExecutable>
+        </MakefileSettings>
+        <LocalBuildSettings>
+          <workingDirectory>binDefault</workingDirectory>
+        </LocalBuildSettings>
+        <ConfigurationRequirements>
+          <configurationRequirement>
+            <projectName>b</projectName>
+            <requiredConfiguration>None</requiredConfiguration>
+          </configurationRequirement>
+          <configurationRequirement>
+            <projectName>c</projectName>
+            <requiredConfiguration>None</requiredConfiguration>
+          </configurationRequirement>
+        </ConfigurationRequirements>
+      </ProjectProperties>
+    </Configuration>
+  </Configurations>
+</TITAN_Project_File_Information>
diff --git a/regression_test/compileonly/mfgen-tpd/compile_once/a/src/ModuleA.ttcn b/regression_test/compileonly/mfgen-tpd/compile_once/a/src/ModuleA.ttcn
new file mode 100644
index 0000000000000000000000000000000000000000..3d9c7b13c062ec83ea4986808b948f896dedda78
--- /dev/null
+++ b/regression_test/compileonly/mfgen-tpd/compile_once/a/src/ModuleA.ttcn
@@ -0,0 +1,32 @@
+/******************************************************************************
+ * Copyright (c) 2000-2017 Ericsson Telecom AB
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Szabo, Bence Janos
+ *
+ ******************************************************************************/
+module ModuleAMain {
+	import from ModuleB all;
+	import from ModuleC all;
+
+	import from ModuleA2 all;
+	import from ModuleA3 all;
+
+	import from ModuleB2 all;
+	import from ModuleB3 all;
+	import from ModuleC2 all;
+	import from ModuleC3 all;
+
+
+	const My_Integer_A a := 1;
+	const My_Integer_B b_1 := 1; 
+	const My_Integer_C c_1 := 1;
+
+	function f() return integer {
+		return a + b + c + b_1 + c_1 + a2 + b2 + c2;
+	}
+}
\ No newline at end of file
diff --git a/regression_test/compileonly/mfgen-tpd/compile_once/a/src/ModuleA2.ttcnpp b/regression_test/compileonly/mfgen-tpd/compile_once/a/src/ModuleA2.ttcnpp
new file mode 100644
index 0000000000000000000000000000000000000000..46d893007cc1049f091808d9786fdf3b98bec58a
--- /dev/null
+++ b/regression_test/compileonly/mfgen-tpd/compile_once/a/src/ModuleA2.ttcnpp
@@ -0,0 +1,14 @@
+/******************************************************************************
+ * Copyright (c) 2000-2017 Ericsson Telecom AB
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Szabo, Bence Janos
+ *
+ ******************************************************************************/
+module ModuleA2 {
+	const integer a2 := 1;
+}
\ No newline at end of file
diff --git a/regression_test/compileonly/mfgen-tpd/compile_once/a/src/ModuleA3.asn b/regression_test/compileonly/mfgen-tpd/compile_once/a/src/ModuleA3.asn
new file mode 100644
index 0000000000000000000000000000000000000000..d8967f01cbb77645adf299388c8db0776d21b15c
--- /dev/null
+++ b/regression_test/compileonly/mfgen-tpd/compile_once/a/src/ModuleA3.asn
@@ -0,0 +1,27 @@
+--/////////////////////////////////////////////////////////////////////////////
+-- Copyright (c) 2000-2017 Ericsson Telecom AB
+-- All rights reserved. This program and the accompanying materials
+-- are made available under the terms of the Eclipse Public License v1.0
+-- which accompanies this distribution, and is available at
+-- http://www.eclipse.org/legal/epl-v10.html
+--
+-- Contributors:
+--   Szabo, Bence Janos
+--
+--/////////////////////////////////////////////////////////////////////////////
+ModuleA3
+DEFINITIONS
+
+AUTOMATIC TAGS
+
+::=
+
+BEGIN
+
+IMPORTS ; -- nothing
+
+My-Integer ::= INTEGER
+
+My-Integer-A ::= INTEGER
+
+END
diff --git a/regression_test/compileonly/mfgen-tpd/compile_once/b/b.tpd b/regression_test/compileonly/mfgen-tpd/compile_once/b/b.tpd
new file mode 100644
index 0000000000000000000000000000000000000000..7107d5f6c799ea098a725ae01a672af5eaf2b689
--- /dev/null
+++ b/regression_test/compileonly/mfgen-tpd/compile_once/b/b.tpd
@@ -0,0 +1,44 @@
+<!--
+ Copyright (c) 2000-2017 Ericsson Telecom AB
+ All rights reserved. This program and the accompanying materials
+ are made available under the terms of the Eclipse Public License v1.0
+ which accompanies this distribution, and is available at
+ http://www.eclipse.org/legal/epl-v10.html
+
+ Contributors:
+  Szabo, Bence Janos – initial implementation
+-->
+<TITAN_Project_File_Information version="1.0">
+  <ProjectName>b</ProjectName>
+  <ReferencedProjects>
+    <ReferencedProject name="c" projectLocationURI="../c/c.tpd"/>
+  </ReferencedProjects>
+  <Folders>
+    <FolderResource projectRelativePath="src" relativeURI="src"/>
+  </Folders>
+  <Files>
+    <FileResource projectRelativePath="src/ModuleB.ttcn" relativeURI="src/ModuleB.ttcn"/>
+    <FileResource projectRelativePath="src/ModuleB2.ttcnpp" relativeURI="src/ModuleB2.ttcnpp"/>
+    <FileResource projectRelativePath="src/ModuleB3.asn" relativeURI="src/ModuleB3.asn"/>
+  </Files>
+  <ActiveConfiguration>None</ActiveConfiguration>
+  <Configurations>
+    <Configuration name="None">
+      <ProjectProperties>
+        <MakefileSettings>
+          <codeSplitting>none</codeSplitting>
+          <targetExecutable>binDefault/binDefault</targetExecutable>
+        </MakefileSettings>
+        <LocalBuildSettings>
+          <workingDirectory>binDefault</workingDirectory>
+        </LocalBuildSettings>
+        <ConfigurationRequirements>
+          <configurationRequirement>
+            <projectName>c</projectName>
+            <requiredConfiguration>None</requiredConfiguration>
+          </configurationRequirement>
+        </ConfigurationRequirements>
+      </ProjectProperties>
+    </Configuration>
+  </Configurations>
+</TITAN_Project_File_Information>
diff --git a/regression_test/compileonly/mfgen-tpd/compile_once/b/src/ModuleB.ttcn b/regression_test/compileonly/mfgen-tpd/compile_once/b/src/ModuleB.ttcn
new file mode 100644
index 0000000000000000000000000000000000000000..219f68c003a713660d52bef6c5ed0531c8508167
--- /dev/null
+++ b/regression_test/compileonly/mfgen-tpd/compile_once/b/src/ModuleB.ttcn
@@ -0,0 +1,18 @@
+/******************************************************************************
+ * Copyright (c) 2000-2017 Ericsson Telecom AB
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Szabo, Bence Janos
+ *
+ ******************************************************************************/
+module ModuleB {
+	import from ModuleC all;
+	import from ModuleB2 all;
+	import from ModuleB3 all;
+
+	const integer b := 2;
+}
\ No newline at end of file
diff --git a/regression_test/compileonly/mfgen-tpd/compile_once/b/src/ModuleB2.ttcnpp b/regression_test/compileonly/mfgen-tpd/compile_once/b/src/ModuleB2.ttcnpp
new file mode 100644
index 0000000000000000000000000000000000000000..32a6e43bd15f7516c2bbe4e23c9d0a2ce6d285e7
--- /dev/null
+++ b/regression_test/compileonly/mfgen-tpd/compile_once/b/src/ModuleB2.ttcnpp
@@ -0,0 +1,14 @@
+/******************************************************************************
+ * Copyright (c) 2000-2017 Ericsson Telecom AB
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Szabo, Bence Janos
+ *
+ ******************************************************************************/
+module ModuleB2 {
+	const integer b2 := 1;
+}
\ No newline at end of file
diff --git a/regression_test/compileonly/mfgen-tpd/compile_once/b/src/ModuleB3.asn b/regression_test/compileonly/mfgen-tpd/compile_once/b/src/ModuleB3.asn
new file mode 100644
index 0000000000000000000000000000000000000000..d1087a7929ec481de4b26578f2c99c3784f15d5f
--- /dev/null
+++ b/regression_test/compileonly/mfgen-tpd/compile_once/b/src/ModuleB3.asn
@@ -0,0 +1,27 @@
+--/////////////////////////////////////////////////////////////////////////////
+-- Copyright (c) 2000-2017 Ericsson Telecom AB
+-- All rights reserved. This program and the accompanying materials
+-- are made available under the terms of the Eclipse Public License v1.0
+-- which accompanies this distribution, and is available at
+-- http://www.eclipse.org/legal/epl-v10.html
+--
+-- Contributors:
+--   Szabo, Bence Janos
+--
+--/////////////////////////////////////////////////////////////////////////////
+ModuleB3
+DEFINITIONS
+
+AUTOMATIC TAGS
+
+::=
+
+BEGIN
+
+IMPORTS ; -- nothing
+
+My-Integer ::= INTEGER
+
+My-Integer-B ::= INTEGER
+
+END
diff --git a/regression_test/compileonly/mfgen-tpd/compile_once/c/c.tpd b/regression_test/compileonly/mfgen-tpd/compile_once/c/c.tpd
new file mode 100644
index 0000000000000000000000000000000000000000..f0e6eac280b65f82f450693953f11e934fba4962
--- /dev/null
+++ b/regression_test/compileonly/mfgen-tpd/compile_once/c/c.tpd
@@ -0,0 +1,35 @@
+<!--
+ Copyright (c) 2000-2017 Ericsson Telecom AB
+ All rights reserved. This program and the accompanying materials
+ are made available under the terms of the Eclipse Public License v1.0
+ which accompanies this distribution, and is available at
+ http://www.eclipse.org/legal/epl-v10.html
+
+ Contributors:
+  Szabo, Bence Janos – initial implementation
+-->
+<TITAN_Project_File_Information version="1.0">
+  <ProjectName>c</ProjectName>  
+  <Folders>
+    <FolderResource projectRelativePath="src" relativeURI="src"/>
+  </Folders>
+  <Files>
+    <FileResource projectRelativePath="src/ModuleC.ttcn" relativeURI="src/ModuleC.ttcn"/>
+    <FileResource projectRelativePath="src/ModuleC2.ttcnpp" relativeURI="src/ModuleC2.ttcnpp"/>
+    <FileResource projectRelativePath="src/ModuleC3.asn" relativeURI="src/ModuleC3.asn"/>
+  </Files>
+  <ActiveConfiguration>None</ActiveConfiguration>
+  <Configurations>
+    <Configuration name="None">
+      <ProjectProperties>
+        <MakefileSettings>
+          <codeSplitting>none</codeSplitting>
+          <targetExecutable>binDefault/binDefault</targetExecutable>
+        </MakefileSettings>
+        <LocalBuildSettings>
+          <workingDirectory>binDefault</workingDirectory>
+        </LocalBuildSettings>
+      </ProjectProperties>
+    </Configuration>
+  </Configurations>
+</TITAN_Project_File_Information>
diff --git a/regression_test/compileonly/mfgen-tpd/compile_once/c/src/ModuleC.ttcn b/regression_test/compileonly/mfgen-tpd/compile_once/c/src/ModuleC.ttcn
new file mode 100644
index 0000000000000000000000000000000000000000..f391ab9095ef61464e021fa5245b802dca554282
--- /dev/null
+++ b/regression_test/compileonly/mfgen-tpd/compile_once/c/src/ModuleC.ttcn
@@ -0,0 +1,17 @@
+/******************************************************************************
+ * Copyright (c) 2000-2017 Ericsson Telecom AB
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Szabo, Bence Janos
+ *
+ ******************************************************************************/
+module ModuleC {
+	import from ModuleC2 all;
+	import from ModuleC3 all;
+
+	const integer c := 3;
+}
\ No newline at end of file
diff --git a/regression_test/compileonly/mfgen-tpd/compile_once/c/src/ModuleC2.ttcnpp b/regression_test/compileonly/mfgen-tpd/compile_once/c/src/ModuleC2.ttcnpp
new file mode 100644
index 0000000000000000000000000000000000000000..6c2c2d7a50975fb774cfc89b697b66c5643c7637
--- /dev/null
+++ b/regression_test/compileonly/mfgen-tpd/compile_once/c/src/ModuleC2.ttcnpp
@@ -0,0 +1,14 @@
+/******************************************************************************
+ * Copyright (c) 2000-2017 Ericsson Telecom AB
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Szabo, Bence Janos
+ *
+ ******************************************************************************/
+module ModuleC2 {
+	const integer c2 := 1;
+}
\ No newline at end of file
diff --git a/regression_test/compileonly/mfgen-tpd/compile_once/c/src/ModuleC3.asn b/regression_test/compileonly/mfgen-tpd/compile_once/c/src/ModuleC3.asn
new file mode 100644
index 0000000000000000000000000000000000000000..ad4090789307dc7da723501b3378064ca321488d
--- /dev/null
+++ b/regression_test/compileonly/mfgen-tpd/compile_once/c/src/ModuleC3.asn
@@ -0,0 +1,27 @@
+--/////////////////////////////////////////////////////////////////////////////
+-- Copyright (c) 2000-2017 Ericsson Telecom AB
+-- All rights reserved. This program and the accompanying materials
+-- are made available under the terms of the Eclipse Public License v1.0
+-- which accompanies this distribution, and is available at
+-- http://www.eclipse.org/legal/epl-v10.html
+--
+-- Contributors:
+--   Szabo, Bence Janos
+--
+--/////////////////////////////////////////////////////////////////////////////
+ModuleC3
+DEFINITIONS
+
+AUTOMATIC TAGS
+
+::=
+
+BEGIN
+
+IMPORTS ; -- nothing
+
+My-Integer ::= INTEGER
+
+My-Integer-C ::= INTEGER
+
+END