I would like to set up a custom command in cmake
to generate some files (.h
) before building our test binary, which uses those files during compilation.
My first attempt was to add the custom command to PRE_BUILD
the target:
add_executable(mytest ${SRCS})
add_custom_command(
TARGET mytest PRE_BUILD
COMMAND <generate my_file.h>
BYPRODUCTS my_file.h
)
target_sources(mytest PRIVATE my_file.h)
But when I attempt to build mytest
the impl file (my_file.cpp
) complains that my_file.h
isn't found:
[ 97%] Building CXX object CMakeFiles/my_test.cpp.o
/usr/redacted/project/my_test.cpp:4:10: fatal error: 'my_test.h' file not found
#include "my_test.h"
^~~~~~~~~~~
1 error generated.
So I attempted to move the impl file from ${SRCS}
and explicitly add it after the generated file was added:
#same as above
target_sources(mytest PRIVATE my_file.h)
target_sources(mytest PRIVATE my_file.cpp)
But that didn't seem to work either.
I have cmake_minimum_required(VERSION 3.8)
with 3.25.1 installed.