简体   繁体   中英

Why CMake does not see FLEX generated source files?

I am trying to set up flex with CMake, however, I get the following error when building the project:

[proc] Executing command: /usr/bin/cmake --build /mnt/d/Files/C/parser/build --config Debug --target all -- -j 14
[build] [ 33%] [FLEX][parser] Building scanner with flex 2.6.4
[build] Scanning dependencies of target parser
[build] make[2]: Warning: File 'CMakeFiles/parser.dir/depend.make' has modification time 0.15 s in the future
[build] [ 33%] [FLEX][parser] Building scanner with flex 2.6.4
[build] [ 66%] Building C object CMakeFiles/parser.dir/lex.yy.c.o
[build] gcc: error: /mnt/d/Files/C/parser/build/lex.yy.c: No such file or directory
[build] gcc: fatal error: no input files
[build] compilation terminated.
[build] make[2]: *** [CMakeFiles/parser.dir/build.make:67: CMakeFiles/parser.dir/lex.yy.c.o] Error 1
[build] make[1]: *** [CMakeFiles/Makefile2:131: CMakeFiles/parser.dir/all] Error 2
[build] make: *** [Makefile:117: all] Error 2
[build] Build finished with exit code 2

My CMakeLists.txt is as follows:

cmake_minimum_required(VERSION 3.0.0)
project(parser C)

find_package(FLEX REQUIRED)

include(CTest)
enable_testing()

set(SOURCE_PATH source)
flex_target(parser ${SOURCE_PATH}/parser.l lex.yy.c)

add_executable(parser ${FLEX_parser_OUTPUTS})

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

All the source files (the parser.l file for now) are located in the /source folder, relative to the project folder.

What am I doing wrong? This is the first time I am trying to set up FLEX with CMake, so I am a bit confused on what is going wrong here.

You should specify a full path to the output file in your flex_target command (as the examples in the documentation show):

flex_target(parser ${SOURCE_PATH}/parser.l ${CMAKE_CURRENT_BINARY_DIR}/lex.yy.c)

Because this is a generated file, you should place it in the CMake binary directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM