[CMake] Generating include files

Andrew Fuller afuller at teradici.com
Fri May 19 19:08:43 EDT 2017


> Is there a way to specify that foo.o depends on tab.c?  For some

> reason cmake's scan does not seem to find this.

Does the following work for you?  The first bit is just putting some fixed files for convenience.  The second bit is the interesting bit.  In your build directory you won't have a data.c file until you compile.  When it compiles, data.c will first be generated, placed in your build directory, then compiled into data.c.o and archived into libdata.a.  Finally the executable links against libdata.a


cmake_minimum_required( VERSION 3.0 )

# Files that exist before the build starts:

# Header file with with no matching .c file
file( WRITE ${CMAKE_CURRENT_SOURCE_DIR}/data.h "extern int somevalue;" )
# main() that references the header
file( WRITE ${CMAKE_CURRENT_SOURCE_DIR}/hello.c "
#include \"data.h\"
#include <stdio.h>
int main()
{
   printf( \"Magic data: %d\\n\", somevalue );
}
" )



# This file is generated at build time and is relative to the binary dir
add_custom_command( OUTPUT data.c
 COMMAND echo "int somevalue = 42; " > data.c
 VERBATIM
)

add_library( data ${CMAKE_CURRENT_BINARY_DIR}/data.c )
add_executable( hello hello.c )

target_link_libraries( hello
 PRIVATE data
)


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20170519/87b24c97/attachment.html>


More information about the CMake mailing list