Meysam
Meysam

Reputation: 18177

CMake: How to write into a file after making a target?

What is the equivalent of the following make file code in CMake?

target: config_header $(OBJS)
    g++ -o $(APPNAME) $(OBJS) $(LIBDIR) $(LIBS)

config_header:
    echo "#ifndef _CONFIG_HEADER_H_" > Config.h
    echo "#define _CONFIG_HEADER_H_" >> Config.h
    echo "#define MAJOR_VERSION \"$(MAJOR)\"" >> Config.h
    echo "#define MINOR_VERSION \"$(MINOR)\"" >> Config.h
    echo "#define PATCH_VERSION \"$(PATCH)\"" >> Config.h
    echo "#define RELEASE_VERSION \"$(RELEASE)\"" >> Config.h
    echo "#endif" >> Config.h

Upvotes: 1

Views: 437

Answers (1)

tune2fs
tune2fs

Reputation: 7705

This copies your cmake config file to your binary directory and adapts the defined parameters.

#############################################################################
# Cmake generated header files
#############################################################################
CONFIGURE_FILE(
    ${CMAKE_CURRENT_SOURCE_DIR}/config.hpp.cmake
    ${CMAKE_CURRENT_BINARY_DIR}/config.hpp
)

The config.hpp.cmake file contains:

#ifndef GUARD
#define GUARD

#cmakedefine _CONFIG_HEADER_H_

#endif //GUARD

During the copy cmake generates the following file:

#ifndef GUARD
#define GUARD

#define _CONFIG_HEADER_H_

#endif //GUARD

Upvotes: 1

Related Questions