diff --git a/Source/cmLocalUnixMakefileGenerator2.cxx b/Source/cmLocalUnixMakefileGenerator2.cxx
index ce0090d517a45b472921a4b74a5357cc737e6657..db4e5ed4dc6330df6a880437706f67c873c1bebe 100644
--- a/Source/cmLocalUnixMakefileGenerator2.cxx
+++ b/Source/cmLocalUnixMakefileGenerator2.cxx
@@ -1924,9 +1924,11 @@ cmLocalUnixMakefileGenerator2
   if(cmSystemTools::FileIsFullPath(cc.GetOutput().c_str()) &&
      (cc.GetOutput().find(m_Makefile->GetStartOutputDirectory()) == 0))
     {
+    // Use the relative path but convert it to a valid file name.
     customName =
       cmSystemTools::RelativePath(m_Makefile->GetStartOutputDirectory(),
                                   cc.GetOutput().c_str());
+    cmSystemTools::ReplaceString(customName, "/", "_");
     }
   else
     {