Skip to content

Instantly share code, notes, and snippets.

@EricWF
Created February 3, 2018 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EricWF/51a99c2a6fcbb825c4b96eb75ce515d9 to your computer and use it in GitHub Desktop.
Save EricWF/51a99c2a6fcbb825c4b96eb75ce515d9 to your computer and use it in GitHub Desktop.
project(test-cmake CXX)
cmake_minimum_required(VERSION 3.9)
#
set(header_in ${CMAKE_CURRENT_BINARY_DIR}/header.in)
file(WRITE ${header_in}
"
#ifndef MY_HEADER
#define MY_HEADER
int my_x = 42;
#endif
"
)
set(header_file ${CMAKE_CURRENT_BINARY_DIR}/header.h)
add_custom_command(OUTPUT ${header_file}
COMMAND cp ${header_in} ${header_file}
DEPENDS ${header_in}
)
set_source_files_properties(${header_file} PROPERTIES GENERATED TRUE)
add_custom_target(my_headers DEPENDS ${header_file})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_library(my_lib STATIC libfile.cpp ${header_file})
add_dependencies(my_lib my_headers)
#ifndef HEADER_H
#define HEADER_H
int my_x = 42;
#endif // HEADER_H
#include "header.h"
int foo() {
return my_x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment