cmake命令语法问题

发布于 2024-08-26 09:51:39 字数 306 浏览 5 评论 0 原文

我需要 CMAKE 宏的语法,该宏从 lex/yacc 等工具生成 .cc 和 .h 文件。

有人可以告诉我以下人为示例的语法:

假设我有一个文件 y.cc 依赖于 x.cc 和 xh,提到的两个文件是由工具 z_tool 从文件 xz 生成的,其语法是什么?

对于这个例子,假设 y.cc 将变成一个静态库,并且由于我对 CMAKE 很陌生,这个人为示例的完整 CMakellist.txt 将非常有帮助。我正在寻找一个便携式解决方案,因为我的工具am using 可在 Windows 和 UNIX 变体上使用。

I require the syntax of a CMAKE macro that generates .cc and .h files from a tool like lex/yacc.

Could someone please show me the syntax for the following contrived example:

say I have a file y.cc that depends on x.cc and x.h, the two files mentioned are generated by tool z_tool from file x.z. What would the syntax for this be ?

For this example assume that y.cc will be turned into a static library, and as I am quite new to CMAKE the full CMakellist.txt for this contrived example would be very helpful.I'm looking for a portable solution as the tools I am using are available on windows as well as UNIX variants.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与酒说心事 2024-09-02 09:51:39

这里不是给你一个人为示例的答案,而是使用 flex 生成可执行文件的方法和 bison

find_package(BISON)
find_package(FLEX)

BISON_TARGET(MyParser parser.y ${CMAKE_CURRENT_BINARY_DIR}/parser.cc)
FLEX_TARGET(MyScanner lexer.l  ${CMAKE_CURRENT_BINARY_DIR}/lexer.cc)
ADD_FLEX_BISON_DEPENDENCY(MyScanner MyParser)

include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_executable(Foo
  Foo.cc
  ${BISON_MyParser_OUTPUTS}
  ${FLEX_MyScanner_OUTPUTS})
target_link_libraries(Foo ${FLEX_LIBRARIES} ${BISON_LIBRARIES})

Bison/flex 的 CMake 查找包包含在默认安装中,因此这是跨平台的。

一般来说,要创建稍后用作输入的输出,您可以使用 add_custom_command 函数。如果您使用自定义命令的输出作为库或可执行文件的输入,则 CMake 知道在编译库/可执行目标的源之前运行您的自定义命令。

Rather than give you the answer to a contrived example, here is the way you would generate an executable using flex and bison

find_package(BISON)
find_package(FLEX)

BISON_TARGET(MyParser parser.y ${CMAKE_CURRENT_BINARY_DIR}/parser.cc)
FLEX_TARGET(MyScanner lexer.l  ${CMAKE_CURRENT_BINARY_DIR}/lexer.cc)
ADD_FLEX_BISON_DEPENDENCY(MyScanner MyParser)

include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_executable(Foo
  Foo.cc
  ${BISON_MyParser_OUTPUTS}
  ${FLEX_MyScanner_OUTPUTS})
target_link_libraries(Foo ${FLEX_LIBRARIES} ${BISON_LIBRARIES})

The CMake find packages for bison/flex are included in the default installation, so this is cross platform.

In general to create an output that will later be used as an input you can use the add_custom_command function. If you use an output from the custom command as an input to a library or executable, then CMake knows to run your custom command before compiling the sources for the library/executable target.

血之狂魔 2024-09-02 09:51:39

以下行有错字

FLEX_TARGET(MyScanner lexer.l  ${CMAKE_CURRENT_BIANRY_DIR}/lexer.cc)

BIANRY 应拼写为 BINARY

注意:CMake 文档也有此拼写错误。 (在 2.8.0 中,此问题已在 2.8.10 文档中修复)..

The following line has Typo

FLEX_TARGET(MyScanner lexer.l  ${CMAKE_CURRENT_BIANRY_DIR}/lexer.cc)

BIANRY should be spelled as BINARY.

Note: CMake Documentation has this typo there as well. ( in 2.8.0, this is fixed in 2.8.10 documentation)..

眼眸 2024-09-02 09:51:39

/* 让它在我的 Mac 上运行 Lion。我将文件名更改为 y.tab.c 和 lex.yy.c,这是从命令行运行 lex lex.l 和 yacc -D yacc.y 时的输出文件。见下文。
*/

cmake_minimum_required(VERSION 2.8)
project (LexYacc)

find_package(BISON)
find_package(FLEX)

BISON_TARGET(MyParser yacc.y ${CMAKE_CURRENT_BINARY_DIR}/y.tab.c) 
FLEX_TARGET(MyScanner lex.l  ${CMAKE_CURRENT_BINARY_DIR}/lex.yy.c)
ADD_FLEX_BISON_DEPENDENCY(MyScanner MyParser)

include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_executable(LexYacc
  ${BISON_MyParser_OUTPUTS}
  ${FLEX_MyScanner_OUTPUTS})
target_link_libraries(LexYacc ${FLEX_LIBRARIES} ${BISON_LIBRARIES})

/* To make it work on my Mac with Lion. I changed the files names to y.tab.c and lex.yy.c, which are the output files if you run lex lex.l and yacc -D yacc.y from command line. See below.
*/

cmake_minimum_required(VERSION 2.8)
project (LexYacc)

find_package(BISON)
find_package(FLEX)

BISON_TARGET(MyParser yacc.y ${CMAKE_CURRENT_BINARY_DIR}/y.tab.c) 
FLEX_TARGET(MyScanner lex.l  ${CMAKE_CURRENT_BINARY_DIR}/lex.yy.c)
ADD_FLEX_BISON_DEPENDENCY(MyScanner MyParser)

include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_executable(LexYacc
  ${BISON_MyParser_OUTPUTS}
  ${FLEX_MyScanner_OUTPUTS})
target_link_libraries(LexYacc ${FLEX_LIBRARIES} ${BISON_LIBRARIES})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文