简体   繁体   English

CMake 项目与 flex/bison

[英]CMake project with flex/bison

I'm working on an hobby project in C++, using flex and bison .我正在使用flexbison在 C++ 中从事一个爱好项目。 I'm using CMake as my build system, and I had struggle in making all work together.我正在使用 CMake 作为我的构建系统,我很难让所有的工作一起工作。

I'm quite new to CMake so the documentation seems quite minimal to me.我对 CMake 很陌生,所以文档对我来说似乎很少。 It is showed how to link everything in one executable.它展示了如何将所有内容链接到一个可执行文件中。 But I'm trying to keep all the parts of my code separated, so I would like to have my flex / bison file in a subfolder.但我试图将代码的所有部分分开,所以我想将我的flex / bison文件放在一个子文件夹中。 My structure is this:我的结构是这样的:

.
├── build
|   └-- ...
├── CMakeLists.txt
├── flexbison
│   ├── CMakeLists.txt
│   ├── lexer.ll
│   └── parser.yy
├── lib
│   ├── CMakeLists.txt
│   └── ...
├── main.cpp
└── run.sh

So I want to have my yyparse function separate from my main.cpp , as well as my other C++ files.所以我想让我的yyparse function 与我的main.cpp以及我的其他 C++ 文件分开。 I managed to get it done in this way:我设法以这种方式完成了它:

  • project_root/CMakeLists.txt : project_root/CMakeLists.txt
cmake_minimum_required(VERSION 3.10)

# set the project name
project(progetto-di-prova VERSION 0.1)

add_subdirectory(flexbison)

add_executable(executable main.cpp)

target_include_directories(executable PUBLIC
  "${PROJECT_BINARY_DIR}"
  "${PROJECT_BINARY_DIR}/flexbison"
  )


set_target_properties(executable PROPERTIES CXX_STANDARD 11)

target_link_libraries(executable parserlib)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
  • flexbison/CMakeLists.txt : flexbison/CMakeLists.txt
find_package(FLEX)
find_package(BISON)

flex_target(lexer lexer.ll "${CMAKE_CURRENT_BINARY_DIR}/lexer.cpp")
bison_target(parser parser.yy ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp
  DEFINES_FILE ${CMAKE_CURRENT_BINARY_DIR}/parser.h)

add_flex_bison_dependency(lexer parser)

add_library(parserlib STATIC
  ${FLEX_lexer_OUTPUTS}
  ${BISON_parser_OUTPUTS}
)
  • the main.cpp file: main.cpp文件:
#include <parser.h>     //  <-this was the line causing troule
#include <iostream>

extern int yylex(void);

int main(int argc, char *argv[])
{
  yyparse();
  return 0;
}

Now, the point is: is this the correct way of doing so ?现在,重点是:这是正确的做法吗? Is it better to put the main function in the bison code?main的function放在bison代码中会更好吗?

My main problem, as highlighted in the code, was to include the parser.h header file in the main.cpp .正如代码中突出显示的那样,我的主要问题是在main.cpp中包含parser.h header 文件。 Is this the CMake-way to do it ?这是 CMake 的方式吗?

Thanks in advance!提前致谢!

If I were you I would have:如果我是你,我会:

flexbison/CMakeLists.txt : flexbison/CMakeLists.txt

...
add_library(parserlib STATIC
  ${FLEX_lexer_OUTPUTS}
  ${BISON_parser_OUTPUTS}
)
target_include_directories(parserlib PUBLIC 
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
  $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
  $<INSTALL_INTERFACE:include/parserlib> # <prefix>/include/parserlib
)

note: $<INSTALL_INTERFACE:include/parserlib> need to be adapted if you want and how you install parserlib...注意: $<INSTALL_INTERFACE:include/parserlib>需要根据需要进行调整以及如何安装 parserlib...

project_root/CMakeLists.txt : project_root/CMakeLists.txt

...
target_include_directories(executable PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)
...

Since executable is "target_link" against parserlib which provides public include directories, you don't need to list these directories anymore in executable 's include dirs.由于executable是针对提供公共包含目录的parserlib的“target_link”,因此您不再需要在executable的包含目录中列出这些目录。

ref: https://cmake.org/cmake/help/latest/command/target_include_directories.html参考: https://cmake.org/cmake/help/latest/command/target_include_directories.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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