6

I am making a GUI program using Qt4, under git source control (Github page). Small part of project requires scanning and parsing. So I want to use flex and bison with the project. I can think of 3 ways-

  1. To keep flex and bison files out of project and source control. Generate the C source files and add it to project.
  2. Add flex and bison files to project, but run flex and bison commands separately.
  3. Integrate properly with IDE (Qt Creator on Ubuntu 12.04) and source control, so that when I build the flex and bison is called to generate lexer and parser.

I obviously want third option, but have no idea how to do it.

Please suggest the best option and the way to do it. Or there is any other way to do it?

Note - I want the project to be cross platform, to build for Windows as well.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Vinayak Garg
  • 6,518
  • 10
  • 53
  • 80
  • When asking questions about the lexical analyzer; please do not tag them with Flex; which is used for the Adobe/Apache UI Framework. Please tag them with gnu-flex or Lex which are used for the lexical analyzer. – JeffryHouser Jan 20 '13 at 01:01
  • 1
    I have not used qt creator for projects yet. However, what you want is an additional step before compilation. So you might look into [custom steps](http://doc.qt.digia.com/qtcreator-2.4/creator-build-settings.html) and [this blog](http://jonmacey.blogspot.com/2012/03/using-flex-with-qt.html). – Bort Jan 21 '13 at 17:08

2 Answers2

3

By following the link in @Bort's comment I was able to integrate Flex and Bison with my Qt project nicely. I added custom build steps in project settings. Below is the screenshot in Qt Creator.

enter image description here

I asked a related question for this - Undefined reference error even though the functions are present

Community
  • 1
  • 1
Vinayak Garg
  • 6,518
  • 10
  • 53
  • 80
  • 1
    This is good in Qt Creator, but any other IDE or even the commandline won't know about these. `qmake` has "native" facilities for pre-build and pre-link commands. – rubenvb Jan 24 '13 at 14:56
1

I was unable to find the Qt Creator config page @VinayakGarg references in his answer.

One solution (from here) is to edit the .pro file directly. This worked for me.

### For building the gnu flex part
FLEXSOURCES += my_lex_sourcefile.l
flex.input = FLEXSOURCES
flex.output = ${QMAKE_FILE_BASE}.c
flex.commands = flex  -o ${QMAKE_FILE_IN_BASE}.c ${QMAKE_FILE_IN}
flex.variable_out = SOURCES
flex.CONFIG += target_predeps
flex.clean =  ${QMAKE_FILE_IN_BASE}.c
flex.name = flex
# finally add this to the qmake project so it will have the makefile generated
QMAKE_EXTRA_COMPILERS += flex

Where (obviously) my_lex_sourcefile.l is your flex-input filename.

Any extra command line options should be added on the flex.commands line.

Ref: https://jonmacey.blogspot.com/2012/03/using-flex-with-qt.html

Kingsley
  • 14,398
  • 5
  • 31
  • 53