Windows环境下CMake FindFLEX无法定位FLEX头文件与库文件的问题求助
FLEX_INCLUDE_DIR and FL_LIBRARY NOTFOUND with winflexbison3 Let’s break down your problem and fix it step by step—first, let’s clear up a critical detail about the winflexbison3 package from Chocolatey: it only provides the win_flex and win_bison executable tools, not a full system-level installation of flex’s runtime libraries or headers (unlike standard flex packages on Linux or via managers like vcpkg/msys2). This is why CMake’s FindFLEX module can locate the executables but fails to find the header and library—they aren’t in the standard paths CMake searches by default.
Step 1: Resolve the FlexLexer.h Header Path Issue
You already found FlexLexer.h lives in C:\ProgramData\chocolatey\lib\winflexbison3\tools. Here are two reliable ways to make CMake recognize this:
Option A: Manually set the path in CMakeLists.txt
Add this line before find_package(FLEX) to explicitly tell CMake where to find the header:
set(FLEX_INCLUDE_DIR "C:/ProgramData/chocolatey/lib/winflexbison3/tools")
Option B: Pass the path via CMake command line
You can specify the path when running CMake to avoid modifying your project files:
cmake .. -DFLEX_INCLUDE_DIR="C:/ProgramData/chocolatey/lib/winflexbison3/tools"
Step 2: Fix the FL_LIBRARY NOTFOUND Error
The winflexbison3 package does not include the libfl runtime library (which provides the yywrap() function). The good news is you don’t need it—you can modify your lexer code to eliminate the dependency entirely:
- Open your
lexer.lfile and add this line at the top (before any rules):%option noyywrap - Update your CMakeLists.txt to remove
${FLEX_LIBRARIES}fromtarget_link_libraries, since you no longer need to link againstlibfl.
If you must use yywrap() for specific reasons, you’ll have to compile libfl yourself (source code is available in standard flex distributions) or switch to a flex package that includes the library (like vcpkg’s flex port). But %option noyywrap is the standard, simplest fix for Windows with winflexbison3.
Modified CMakeLists.txt Example
Here’s a cleaned-up version of your CMake file incorporating these fixes:
cmake_minimum_required(VERSION 3.10) project(Foo) # Manually specify path to FlexLexer.h from winflexbison3 set(FLEX_INCLUDE_DIR "C:/ProgramData/chocolatey/lib/winflexbison3/tools") find_package(BISON REQUIRED) find_package(FLEX REQUIRED) message("FLEX_FOUND: ${FLEX_FOUND}") message("FLEX_EXECUTABLE: ${FLEX_EXECUTABLE}") message("FLEX_INCLUDE_DIRS: ${FLEX_INCLUDE_DIR}") # Generate scanner with --noyywrap to avoid libfl dependency FLEX_TARGET(MyScanner lexer.l ${CMAKE_CURRENT_BINARY_DIR}/lexer.cpp COMPILE_FLAGS "--noyywrap") BISON_TARGET(MyParser parser.y ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp) ADD_FLEX_BISON_DEPENDENCY(MyScanner MyParser) include_directories(${CMAKE_CURRENT_BINARY_DIR} ${FLEX_INCLUDE_DIR}) add_executable(Foo ${BISON_MyParser_OUTPUTS} ${FLEX_MyScanner_OUTPUTS}) # No need to link FLEX_LIBRARIES anymore thanks to noyywrap
Why Did CMake’s Automatic Search Fail?
CMake’s FindFLEX module is built to work with standard flex installations—where headers live in system include paths (like C:\Program Files\... or /usr/include) and libraries are in standard library paths. The winflexbison3 package prioritizes portability over system integration, so it doesn’t copy files to these default locations. This isn’t a flaw in CMake’s auto-search; it’s just a mismatch between the package’s design and the module’s expected setup. Manual path specification is the correct workaround here.
内容的提问来源于stack exchange,提问作者alagris

