我正在尝试在一个项目中使用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二进制文件,但找不到库文件呢?谢谢。
谢谢。
NAMES
变量中有什么?如果 Flex 库的名称不在该变量中,则可能无法找到它。 - SethMMorton