在CLion中使用flex / bison进行CMake

时间:2017-07-02 02:03:24

标签: c makefile cmake bison flex-lexer

我正在尝试将Makefile翻译成CMakeLists.txt。

Makefile工作

fb1-5:  fb1-5.l fb1-5.y
    bison -d fb1-5.y
    flex fb1-5.l
    cc -o $@ fb1-5.tab.c lex.yy.c -lfl

CMakeLists.txt尝试

cmake_minimum_required(VERSION 3.7)
project(calc)

set(CMAKE_C_STANDARD 99)

FIND_PACKAGE(BISON REQUIRED)
SET(BisonOutput ${CMAKE_SOURCE_DIR}/parser.c)
IF(BISON_FOUND)
    ADD_CUSTOM_COMMAND(
            OUTPUT ${BisonOutput}
            COMMAND ${BISON_EXECUTABLE}
            -d
            ${CMAKE_SOURCE_DIR}/fb1-5.y
            COMMENT "Generating parser.c"
    )
ENDIF()

FIND_PACKAGE(FLEX REQUIRED)
SET(FlexOutput ${CMAKE_SOURCE_DIR}/scanner.c)
IF(FLEX_FOUND)
    ADD_CUSTOM_COMMAND(
            OUTPUT ${FlexOutput}
            COMMAND ${FLEX_EXECUTABLE}
            ${CMAKE_SOURCE_DIR}/fb1-5.l
            COMMENT "Generating fb1-5.l"
    )
ENDIF()

ADD_LIBRARY(calc ${BisonOutput} ${FlexOutput})

它说它在

中找到了野牛和弯曲
-- Found BISON: /usr/bin/bison (found version "3.0.4") 
-- Found FLEX: /usr/bin/flex (found version "2.6.0") 

但我的CMake脚本不会生成可执行文件。我应该如何定义“可执行文件”以及如何使CMake构建脚本在CLion中运行?

1 个答案:

答案 0 :(得分:2)

  

但我的CMake脚本不会生成可执行文件。我该如何定义"可执行文件"如何让CMake构建脚本在CLion中运行?

我认为你至少应该这样做:

add_executable(fb1-5
  ${BisonOutput}
  ${FlexOutput}
)

请参阅add_executable