I have a series of files include/openssl/foo.gen that have a custom command to build {build-dir}include/openssl/foo.h. And foo.h is include’d in various source files. How do I ensure that the step runs before compiling bar.c ? I thought it would come for free with dependency building, but that doesn’t seem to be done. Thanks.
Add the generated headers with target_sources()
If possible use FILE_SET HEADER
Thanks, but what path do I use?
In the top-level CMakeLists I have
foreach (f ${GENHEADERS}) # openssl/crypto.h, etc …
set(in “include/${f}.in”)
set(out “${CMAKE_BINARY_DIR}/include/${f}”)
add_custom_command(COMMENT “Generating ${out}”
OUTPUT “${out}” DEPENDS “${in}” ${mkhdr}
COMMAND ${PERL} ${mkhdr} ${in} ${out}
VERBATIM)
endforeach ()
In ssl/CMakeLists I have
add_executable(openssl)
target_sources(openssl PUBLIC ${APPSOURCE} ${CMAKE_BINARY_DIR}/include/openssl/crypto.h)
target_link_libraries(openssl crypto ssl)
I get the same error:
; mk openssl
[ 0%] Building C object ssl/CMakeFiles/ssl.dir/d1_lib.c.o
In file included from /home/rsalz/git/quictls/quictls/ssl/d1_lib.c:10:
/home/rsalz/git/quictls/quictls/include/internal/e_os.h:17:11: fatal error: openssl/crypto.h: No such file or directory
17 | # include <openssl/crypto.h>
^~~~~~~~~~~~~~~~~~
It’s not picking up the generated file as a dependency nor building it.
As I recommended, use FILE_SET HEADER, or set the needed include path yourself:
add_library(crypto)
target_sources(crypto PRIVATE ${CRYPTO_SOURCES}
PUBLIC
FILE_SET public_headers
TYPE HEADERS
BASE_DIRS
${CMAKE_BINARY_DIR}/include # ....
FILES
${CMAKE_BINARY_DIR}/include/openssl/crypto.h # ...
)
# later
# ...
target_link_libraries(openssl PUBLIC crypto ssl)
I missed the FILE_SET. Thanks for your patience!
And it turns out the email I got sent cut off that key line! I’m good to go now, thanks again.
A further complication was that “normal” header file foo.h did a #include
of generated file crypto.h, so I had to use add_custom_target
that depended on all the generated files, and then use add_dependencies
on the each target to generate the files. (I’m using 3.21, so couldn’t use FILE_SET)
There is no real reason to stay behind the future.
You may always install the newest version with:
pip install cmake
I’m using 3.21 because it’s what came on my Ubuntu VM and I since I’m aiming for enterprise users, “step 1: update cmake” isn’t appropriate IMO.
Thanks for the info. We’re currently on 22.04 LTS, not yet the next LTS release which seems to be in development.