Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake command syntax question

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.

like image 960
Hassan Syed Avatar asked Mar 22 '10 10:03

Hassan Syed


People also ask

How use CMake command line?

Running CMake from the command line From the command line, cmake can be run as an interactive question and answer session or as a non-interactive program. To run in interactive mode, just pass the option “-i” to cmake. This will cause cmake to ask you to enter a value for each value in the cache file for the project.

What is the CMake command?

The cmake command creates many files at your current working directory (CWD, the directory you ran the command from), and among them is a file called Makefile , which has rules about which files to compile/build/copy/write/whatever and how to do it.

How do I specify in CMake?

You can use the command line to set entries in the Cache with the syntax cmake -D var:type=value , just cmake -D var=value or with cmake -C CMakeInitialCache. cmake .

What is the CMake command in Linux?

CMake is an open-source, cross-platform tool that uses compiler and platform independent configuration files to generate native build tool files specific to your compiler and platform. The CMake Tools extension integrates Visual Studio Code and CMake to make it easy to configure, build, and debug your C++ project.


3 Answers

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.

like image 57
richq Avatar answered Oct 17 '22 19:10

richq


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)..

like image 28
Mihir Patel Avatar answered Oct 17 '22 21:10

Mihir Patel


/* 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})
like image 44
Richard Catlin Avatar answered Oct 17 '22 21:10

Richard Catlin