我正在尝试在项目中使用 flex,并且正在尝试使用 CMake 将 flex 与我的项目链接。我在网上找到了我正在使用的 FindFLEX.cmake。你可以在这里找到它。默认情况下,这应该在 CMake 中,但我认为不是。我的目录结构如下
root
---src
---CMakeLists.txt
---cmake
---Modules
---FindFLEX.cmake
---build
---external
---flex - Where flex is installed
---bin
---flex.exe
---lib
---libfl.a
我的 src/CMakeLists.txt 如下
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 应该由 FindFLEX.cmake 在找到 Flex 包时提供。在 build/ 目录中运行以下命令没有找到 flex 包
build> cmake ..\src
然后我添加了前缀并且部分工作
build> cmake -DCMAKE_PREFIX_PATH=c:\root\external\flex\ ..\src
那找到了可执行文件 flex.exe ,但没有找到库。FindFLEX.cmake 的相关部分如下所示
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})
我在运行 cmake 时得到的消息是
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!
谁能告诉我为什么在包含前缀路径后我找到了 flex 二进制文件而不是库?任何帮助,将不胜感激。
谢谢