3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2026-10-11 21:55:47 +00:00
z3/examples/c++/CMakeLists.txt
Alex Reinking 69a4be91b2
[CMake] Use z3::libz3 as the preferred way to consume Z3 (#10733)
In #10717 we enhanced the CMake package to support multiple build
configurations, so that Z3 could be installed and consumed as either a
static or shared package, or both side by side. This PR dog-foods that
work: the Python bindings and the example projects now consume Z3
through the same installed CMake package that external users rely on,
rather than through separate in-tree plumbing.

To make that possible, we introduce a `z3::libz3` ALIAS that mirrors the
installed target in-tree. In-tree consumers (like the examples and the
Python bindings) and out-of-tree consumers of the installed package now
refer to Z3 the same way, so now there's one blessed interface. The
examples and package-consumer test were updated accordingly, both to
exercise this path and to serve as a model for how downstream projects
should consume Z3 going forward.

Finally, we add test coverage for the reworked Python build, including a
CI job that installs a shared build of libz3 and then builds the Python
bindings against that installed package, confirming the Python-only
build path works end to end.
2026-09-04 13:33:23 -07:00

31 lines
1.1 KiB
CMake

################################################################################
# Example C++ project
################################################################################
cmake_minimum_required(VERSION 3.5)
project(Z3_CXX_EXAMPLE CXX)
find_package(Z3
REQUIRED
CONFIG
# `NO_DEFAULT_PATH` is set so that -DZ3_DIR has to be passed to find Z3.
# This should prevent us from accidentally picking up an installed
# copy of Z3. This is here to benefit Z3's build system when building
# this project. When making your own project you probably shouldn't
# use this option.
NO_DEFAULT_PATH
)
message(STATUS "Z3_FOUND: ${Z3_FOUND}")
message(STATUS "Found Z3 ${Z3_VERSION_STRING}")
message(STATUS "Z3_DIR: ${Z3_DIR}")
add_executable(cpp_example example.cpp)
target_link_libraries(cpp_example PRIVATE z3::libz3)
if (CMAKE_SYSTEM_NAME MATCHES "[Ww]indows")
# On Windows we need to copy the Z3 libraries
# into the same directory as the executable
# so that they can be found.
add_custom_command(TARGET cpp_example POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:z3::libz3> $<TARGET_FILE_DIR:cpp_example>)
endif()