Blog content

Latest posts

Active categories:

More tags ...

By date:

    (No recent posts)

Blog calendar

November 2017
Mo Tu We Th Fr Sa Su
 << Oct Dec >>
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30

Blog features

RSS feed icon RSS feed

RSS comment feed icon RSS comment feed

Integrating Flex and Bison into Visual Studio

posted by Martin Rubli at 08:18

There's a pretty good tutorial from Microsoft (what do you know, they do like open source ;-) about how to integrate Flex and Bison into Visual Studio:

Understanding Custom Build Rules in Visual C++ 2005: Building Flex and Bison Files from the IDE

Unfortunately I ran into a problem after integrating it into my project. Flex kept crashing with the following output:

1>------ Build started: Project: MyProject, Configuration: Debug Win32 ------
1>Generating lexical analyser...
1>flex: fatal internal error, exec failed
1>Project : error PRJ0002 : Error result 1 returned from 'C:\WINDOWS\system32\cmd.exe'.
1>Build log was saved at "file://C:\MyProject\Debug\BuildLog.htm"
1>MyProject - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Some experiments showed that Flex was unable to find M4 (i.e. m4.exe) and then crashed with the above not-so-helpful error message.

The workaround is easy: Make sure that either m4.exe is in the path or set the M4 environment variable. Since I use a somewhat special directory structure for Flex, Bison, and some other tools I already had a wrapper script in place, so integration was easy:

: Wrapper script for Flex
:
: This script sets some environment variables that are required for Flex to
: find all its libraries and helper files/tools.
@echo off
: Determine the path of the wrapper script set GNU_PATH=%~d0%~ps0
: Set PATH for cygwin1.dll and M4 for the M4 tool itself. : If we don't define M4 Flex fails with the following error message: : "flex: fatal internal error, exec failed" set PATH=%GNU_PATH%\lib;%PATH% set M4=%GNU_PATH%\bin\m4.exe
: Launch the real Flex executable with the same arguments %GNU_PATH%\bin\flex.exe %*

In case you're wondering, the directory structure I'm using looks something like this:

%GNU_PATH%
¦   bison.cmd
¦   flex.cmd
¦
+---bin
¦       bison.exe
¦       flex.exe
¦       m4.exe
¦
+---lib
¦       cygwin1.dll
¦
+---share
    +---bison
        ¦
        +---m4sugar
                m4sugar.m4

You can see the two wrapper scripts at the top level.

For completeness' sake the wrapper script for Bison, which defines one additional variable, so that Bison can find its M4 helper files:

: Wrapper script for Bison
:
: This script sets some environment variables that are required for Bison to
: find all its libraries and helper files/tools.
@echo off
: Determine the path of the wrapper script set GNU_PATH=%~d0%~ps0
: Set PATH for cygwin1.dll, BISON_PKGDATADIR for the M4 meta files : and M4 for the M4 tool itself. set PATH=%GNU_PATH%\lib;%PATH% set BISON_PKGDATADIR=%GNU_PATH%\share\bison set M4=%GNU_PATH%\bin\m4.exe
: Launch the real Bison executable with the same arguments %GNU_PATH%\bin\bison.exe %*

By the way, if you use Lex in your Visual Studio project, you may also want to check out this article which documents a bug in Microsoft's original FlexBison.rules file.


  1. Marc says:

    Thanks for the tips, this helped me solve my M4 path problems!

  2. Mogur says:

    Thank you very much. I was trying to have flex working and the "fatal internal error, exec failed" was a pain!

Leave a comment

Your comment: