为 cmake 生成文件
File generating for cmake
我有一个带有自动制作构建系统的项目,那里有 flex/bison 文件。现在我不明白如何将它们包含在 cmake 构建系统中。我正在尝试手动执行此操作。以下是项目树:
+ROOT
|---CMakeLists.txt
|---Sources/
| |---Flex
| |---Main
| |---CMakeLists.txt
|---Includes/
Flex
文件夹中有 2 个文件:player_command_parser.ypp; player_command_tok.lpp
。这些文件来自机器人世界杯足球服务器。
我真的不知道如何在新的构建系统中使用它们,所以我决定手动生成所有文件:
flex --c++ player_command_tok.lpp
此命令生成以以下代码开头的lex.RCSSPCom.cc
:
#line 3 "lex.RCSSPCom.cc"
#define YY_INT_ALIGNED short int
/* A lexical scanner generated by flex */
#define FLEX_SCANNER
#define YY_FLEX_MAJOR_VERSION 2
#define YY_FLEX_MINOR_VERSION 5
#define YY_FLEX_SUBMINOR_VERSION 35
#if YY_FLEX_SUBMINOR_VERSION > 0
#define FLEX_BETA
#endif
/* The c++ scanner is a mess. The FlexLexer.h header file relies on the
* following macro. This is required in order to pass the c++-multiple-scanners
* test in the regression suite. We get reports that it breaks inheritance.
* We will address this in a future release of flex, or omit the C++ scanner
* altogether.
*/
#define yyFlexLexer RCSSPComFlexLexer
下一步是:bison -d player_command_parser.ypp
.
我得到了:player_command_parser.tab.cpp; player_command_parser.tab.hpp
现在我正在尝试将所有生成的文件复制到相关文件夹:*.tab.hpp -> 包含,并将 cc&cpp 文件添加到Sources/CMakeLists.txt
:
set (FlexSources
Server/Flex/lex.RCSSPCom.cc
Server/Flex/player_command_parser.tab.cpp
)
和编译输出:
[ 1%] Building CXX object Sources/Flex/lex.RCSSPCom.cc.o
In file included from /Includes/player_command_tok.h:31:0,
from player_command_tok.lpp:28:
/usr/include/FlexLexer.h:112:7: error: redefinition of ‘class RCSSPComFlexLexer’
/usr/include/FlexLexer.h:112:7: error: previous definition of ‘class RCSSPComFlexLexer’
可能出了什么问题?
您的编译错误似乎是由于某些标头被包含两次。您可能需要创建一个额外的文件,该文件只不过是一个包含保护:
player_command_tok_guarded.hpp:
#ifndef PLAYER_COMMAND_TOK_GUARDED
#define PLAYER_COMMAND_TOK_GUARDED
#include "player_command_tok.hpp"
#endif
并改为将您的文件#include
这个新文件。至于将flex和bison集成到您的CMake系统中,请尝试以下操作:
# Find flex and bison.
find_program(FLEX flex DOC "Path to the flex lexical analyser generator.")
if(NOT ${FLEX})
message(SEND_ERROR "Flex not found.")
endif
find_program(BISON bison DOC "Path to the bison parser generator.")
if(NOT ${BISON})
message(SEND_ERROR "Bison not found.")
endif
# Custom commands to invoke flex and bison.
add_custom_command(OUTPUT lex.RCSSPCom.cc
COMMAND ${FLEX} --c++ player_command_tok.lpp
MAIN_DEPENDENCY player_command_tok.lpp
COMMENT "Generating lexer"
VERBATIM)
add_custom_command(OUTPUT player_command_parser.tab.cpp player_command_parser.tab.hpp
COMMAND ${BISON} -d player_command_parser.ypp
MAIN_DEPENDENCY player_command_parser.ypp
COMMENT "Generating parser"
VERBATIM)
并像往常一样将文件添加到文件列表中。
相关文章:
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何使用CMake编译.proto文件来生成.grpcp.pb.cc和.grpc.pb.h文件
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 停止cmake target_link_libraries将插件中静态库的两个对象文件链接到静态库本身
- 现代CMake(3.8+)-FindCUDA已弃用,非.cu文件呢
- 如何通过Conan和CMake找到用于pkg配置的.pc文件
- MinGw 无法从 cmake 编译测试文件
- cmake:添加要搜索头文件的目录
- 库标题在标题中不可见,但在 cmake build 下.cpp文件中完全可见.为什么?
- CMake:如何将库 A 链接到库 B,然后将可执行文件链接到库 A
- 使用 Makefile 中的头文件编译 Pybind (不使用 cmake)
- CMake:在其他目录中找不到我的头文件
- 在Cmake构建CmakeList.txt中读取/解析XML文件
- 无法使用 CMake 从其他文件夹添加源文件
- CMake:我们可以为一组不形成可执行文件或库的特定文件指定包含目录吗?
- 使用 Cmake 在 Qt Creator 中显示头文件
- CMake 生成的 MSVC 项目找不到符号,即使为其相关 dll 正确生成了 lib 文件也是如此
- 告诉CMake链接到自定义文件夹中的Boost
- Vcpkg Pango CMake 构建失败缺少头文件
- 从不同的文件cmake调用类