6

I am trying to use flex on a project and I am trying to use CMake to link flex with my project. I found a FindFLEX.cmake online which I am using for this. You can find it here. This was supposed to be in CMake by default, but I dont think it was. My directory structure is as follows

root
---src
   ---CMakeLists.txt
   ---cmake
      ---Modules
         ---FindFLEX.cmake
---build
---external
   ---flex - Where flex is installed
      ---bin
         ---flex.exe
      ---lib
         ---libfl.a

My src/CMakeLists.txt is as follows

cmake_minimum_required(VERSION 2.8)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
message(${CMAKE_MODULE_PATH})
set( project_name "try_flex" )
message(${project_name})
project(${project_name})
find_package(FLEX)
FLEX_TARGET(Mylexer tokenize.lex ${CMAKE_CURRENT_BINARY_DIR}/tokenize.cpp)
add_executable(${project_name} ${FLEX_Mylexer_OUTPUTS})
target_link_libraries(${project_name} ${FLEX_LIBRARIES})

FLEX_TARGET is supposed to be provided by FindFLEX.cmake when it finds the Flex package. Running the following command in build/ directory didnt find the flex packages

build> cmake ..\src

Then I added the prefix and that worked partially

build> cmake -DCMAKE_PREFIX_PATH=c:\root\external\flex\ ..\src

That found the executable flex.exe , but not the library. The relevant portions of FindFLEX.cmake is shown below

FIND_PROGRAM(FLEX_EXECUTABLE flex DOC "path to the flex executable")
message("DEBUG:"${FLEX_EXECUTABLE})
MARK_AS_ADVANCED(FLEX_EXECUTABLE)

FIND_LIBRARY(FL_LIBRARY NAMES fl DOC "path to the fl library")
message("DEBUG:FL_LIBRARY"${FL_LIBRARY})
MARK_AS_ADVANCED(FL_LIBRARY)
SET(FLEX_LIBRARIES ${FL_LIBRARY})

The message I get on running cmake is

DEBUG:c:/root/external/flex/bin/flex.exe
DEBUG:FL_LIBRARYFL_LIBRARY-NOTFOUND
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake file s:FL_LIBRARY (ADVANCED)
linked by target "try_flex" in directory C:/root/src

-- Configuring incomplete, errors occurred!

Could anyone tell me why I am finding the flex binary but not the library after including the prefix path? Any help would be appreciated.

Thanks

JeffryHouser
  • 39,401
  • 4
  • 38
  • 59
  • Is this about the Adobe/Apache UI Framework Flex? OR the Lexical Analyzer? Or something different named Flex? – JeffryHouser May 22 '13 at 17:21
  • I'm not sure that's the relevant portion of FindFLEX.cmake... can you show the part that attempts to locate the library? How is it different from that part that locates the executable? – SethMMorton May 22 '13 at 17:58
  • @Reboog711 : It is the Lexical Analyzer – Mahesh Ravishankar May 22 '13 at 18:41
  • @SethMMorton That is the relevant part... it seems to use the CMake FIND_LIBRARY routines. There isnt anything else there. You can check out the entire file [here](https://github.com/bro/cmake/blob/master/FindFLEX.cmake) – Mahesh Ravishankar May 22 '13 at 18:43
  • @MaheshRavishankar Thanks; I retagged the question from "Flex" which is used for the Adobe/Apache framework to "Flex-lexer" which is used for the Lexical Analyzer. – JeffryHouser May 22 '13 at 18:51
  • What is in the `NAMES` variable? If the flex library name is not in that variable it probably won't be able to find it. – SethMMorton May 22 '13 at 20:39

1 Answers1

5

I figured out what the problem is. On Windows, cmake is looking for libfl.lib. But the Windows installation of Flex provides only libfl.a So I needed to add these two lines to my cmake

LIST(APPEND CMAKE_FIND_LIBRARY_SUFFIXES ".a")
FIND_LIBRARY(FL_LIBRARY NAMES libfl fl DOC "path to the fl library")

The first line adss .a to list of suffixes searched for libraries, and the second line looks for libfl. That worked