首页 > 解决方案 > 如何使用 Clion 和 flex 和 bison 构建 C++ CMake 项目

问题描述

我正在尝试构建一个我克隆的 git 项目,它使用 CMake 进行编译,需要 bison 和 flex。我正在使用 Windows 10 和 Clion IDE。

我已经从https://sourceforge.net/projects/winflexbison/下载了 Win flex-bison, 但我不明白如何将 CMake 指向我下载的 win_flex_bison 文件。

目前构建失败在线:

find_program(BISON_EXECUTABLE bison)

if(NOT EXISTS ${BISON_EXECUTABLE})
    message("Could not find bison executable.")
endif(NOT EXISTS ${BISON_EXECUTABLE})


find_program(FLEX_EXECUTABLE flex)

if(NOT EXISTS ${FLEX_EXECUTABLE})
        message("Could not find flex executable.")
endif(NOT EXISTS ${FLEX_EXECUTABLE})

IF (DEFINED FLEXLEXER_H_DIR)
  include_directories(${FLEXLEXER_H_DIR})
  message("Looking in ${CMAKE_REQUIRED_INCLUDES} for FlexLexer.h")
ELSE()

CHECK_INCLUDE_FILE_CXX("FlexLexer.h" HAVE_FLEXLEXER_H)
IF(NOT HAVE_FLEXLEXER_H)
  UNSET(HAVE_FLEXLER_H CACHE)
  message( FATAL_ERROR "Cannot find FlexLexer.h.  Set FLEXLEXER_H_DIR to point to where it is to be found.." )
ENDIF()

我得到的按摩是:

Could not find bison executable.
Could not find flex executable.
-- Looking for C++ include FlexLexer.h
-- Looking for C++ include FlexLexer.h - not found
CMake Error at VALfiles/parsing/CMakeLists.txt:28 (message):
  Cannot find FlexLexer.h.  Set FLEXLEXER_H_DIR to point to where it is to be
  found..

-- Configuring incomplete, errors occurred!

标签: c++cmakeflexboxbisonclion

解决方案


您应该使用以下命令在 CMake 中查找 FLEX 和 BISON:

find_package(FLEX REQUIRED)
find_package(BISON REQUIRED)

有关FLEXBISON的更多信息为您提供了您需要了解的所有信息。


推荐阅读