Getting CMake to find flex on Windows

北战南征 提交于 2021-02-20 10:44:58

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/16697549/getting-cmake-to-find-flex-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!