drogon
drogon copied to clipboard
Simple project cmake build fails with error "Unknown CMake command "drogon_create_views""
Simple project build fails with error "Unknown CMake command "drogon_create_views""
Steps to reproduce the behavior:
- drogon_ctl.exe create project drogon-test
- cd drogon-test
- create conanfile.txt and add into conanfile.txt the following lines:
[requires]
drogon/1.9.1
jsoncpp/1.9.5
boost/1.83.0
[generators]
CMakeDeps
CMakeToolchain
- cd build
- conan install .. --build=missing -s compiler.cppstd=20
- cmake .. -DCMAKE_TOOLCHAIN_FILE="conan_toolchain.cmake" -G "Visual Studio 17 2022" -A x64 -DCMAKE_POLICY_DEFAULT_CMP0091=NEW
Context: [!] drogon_ctl.exe version
Version: 1.9.10
Git commit: f6b5404dbb8f0b5a914b0dbdc3868f1f28d55bb7
Compilation:
Compiler: cl.exe
Compiler ID: MSVC
Compilation flags: /Ob0 /Od /RTC1 -std=c++17 -IC:/Users/saint/.conan2/p/b/jsonccf57c65ee1f79/p/include -ID:/include
Libraries:
postgresql: no (pipeline mode: no)
mariadb: no
sqlite3: yes
ssl/tls backend: OpenSSL
brotli: yes
hiredis: yes
c-ares: no
yaml-cpp: no
[!] CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(drogon-test CXX)
include(CheckIncludeFileCXX)
check_include_file_cxx(any HAS_ANY)
check_include_file_cxx(string_view HAS_STRING_VIEW)
check_include_file_cxx(coroutine HAS_COROUTINE)
if (NOT "${CMAKE_CXX_STANDARD}" STREQUAL "")
# Do nothing
elseif (HAS_ANY AND HAS_STRING_VIEW AND HAS_COROUTINE)
set(CMAKE_CXX_STANDARD 20)
elseif (HAS_ANY AND HAS_STRING_VIEW)
set(CMAKE_CXX_STANDARD 17)
else ()
set(CMAKE_CXX_STANDARD 14)
endif ()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_executable(${PROJECT_NAME} main.cc)
# ##############################################################################
# If you include the drogon source code locally in your project, use this method
# to add drogon
# add_subdirectory(drogon)
# target_link_libraries(${PROJECT_NAME} PRIVATE drogon)
#
# and comment out the following lines
find_package(Drogon CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE Drogon::Drogon)
# ##############################################################################
if (CMAKE_CXX_STANDARD LESS 17)
message(FATAL_ERROR "c++17 or higher is required")
elseif (CMAKE_CXX_STANDARD LESS 20)
message(STATUS "use c++17")
else ()
message(STATUS "use c++20")
endif ()
aux_source_directory(controllers CTL_SRC)
aux_source_directory(filters FILTER_SRC)
aux_source_directory(plugins PLUGIN_SRC)
aux_source_directory(models MODEL_SRC)
drogon_create_views(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/views
${CMAKE_CURRENT_BINARY_DIR})
# use the following line to create views with namespaces.
# drogon_create_views(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/views
# ${CMAKE_CURRENT_BINARY_DIR} TRUE)
# use the following line to create views with namespace CHANGE_ME prefixed
# and path namespaces.
# drogon_create_views(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/views
# ${CMAKE_CURRENT_BINARY_DIR} TRUE CHANGE_ME)
target_include_directories(${PROJECT_NAME}
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/models)
target_sources(${PROJECT_NAME}
PRIVATE
${SRC_DIR}
${CTL_SRC}
${FILTER_SRC}
${PLUGIN_SRC}
${MODEL_SRC})
# ##############################################################################
# uncomment the following line for dynamically loading views
# set_property(TARGET ${PROJECT_NAME} PROPERTY ENABLE_EXPORTS ON)
# ##############################################################################
add_subdirectory(test)
Screenshots:
Additional context:
I have compiled drogon into directory: D:\saint\tools\Drogon The directory where project is created using drogon_ctl.exe is at: C:\Users\saint\source\repos\drogon-test
The error "Unknown CMake command 'drogon_create_views'" typically means the macro or function drogon_create_views is not available in your CMake context. This usually happens for one of these reasons:
1. drogon_create_views is only available when building from source
- The
drogon_create_viewsmacro is defined in the Drogon source tree (in the filecmake/DrogonCreateViews.cmake). - When you use Drogon as a Conan package, the macro is not exported or installed via CMake config files, so
find_package(Drogon CONFIG REQUIRED)does not make this function available. - You only get the macro if you include the Drogon source code directly in your project via
add_subdirectory(drogon).
2. Conan package does not provide the macro
- The Conan package for Drogon provides only the compiled library and its CMake targets (
Drogon::Drogon), not the auxiliary CMake macros, includingdrogon_create_views.
Solutions
A. If using Drogon as a Conan package (your current setup):
-
Do not use
drogon_create_viewsin your CMakeLists.txt.- Remove or comment out the line(s) using
drogon_create_views(...). - You will need to handle view compilation manually or set up your project to build views some other way.
- Remove or comment out the line(s) using
-
Alternative: Manually import the macro:
- Copy
cmake/DrogonCreateViews.cmakefrom Drogon's source into your project. - At the top of your
CMakeLists.txt, add:include(path/to/DrogonCreateViews.cmake) - Now you can use
drogon_create_viewsas before.
- Copy
B. If you want drogon_create_views automatically:
-
Use Drogon as a submodule/subdirectory instead of via Conan:
- Clone Drogon's source into your project (e.g.,
git submodule add ...). - Use
add_subdirectory(drogon)in yourCMakeLists.txt. - Link with
target_link_libraries(${PROJECT_NAME} PRIVATE drogon). - The macro will be available.
- Clone Drogon's source into your project (e.g.,
-
Quick fix: Comment out or remove
drogon_create_views(...)from yourCMakeLists.txtif using only the Conan package. - Or: Manually include the macro as described above.