diff --git a/tools/graph2dot.c b/tools/graph2dot.c
index 51a1ab109c7256aea7ba8148a4fda13a33da2ecf..e7f487d1466383b747a1a78bb959fcf60018773d 100644
--- a/tools/graph2dot.c
+++ b/tools/graph2dot.c
@@ -18,7 +18,10 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "config.h"
+#if HAVE_UNISTD_H
 #include <unistd.h>             /* getopt */
+#endif
 #include <stdio.h>
 #include <string.h>
 
@@ -27,6 +30,10 @@
 #include "libavutil/audioconvert.h"
 #include "libavfilter/avfiltergraph.h"
 
+#if !HAVE_GETOPT
+#include "compat/getopt.c"
+#endif
+
 static void usage(void)
 {
     printf("Convert a libavfilter graph to a dot file\n");