drogon icon indicating copy to clipboard operation
drogon copied to clipboard

Simple project cmake build fails with error "Unknown CMake command "drogon_create_views""

Open a11saints opened this issue 7 months ago • 1 comments

Simple project build fails with error "Unknown CMake command "drogon_create_views""

Steps to reproduce the behavior:

  1. drogon_ctl.exe create project drogon-test
  2. cd drogon-test
  3. 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
  1. cd build
  2. conan install .. --build=missing -s compiler.cppstd=20
  3. 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:

Image

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

a11saints avatar May 23 '25 12:05 a11saints

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_views macro is defined in the Drogon source tree (in the file cmake/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, including drogon_create_views.

Solutions

A. If using Drogon as a Conan package (your current setup):

  1. Do not use drogon_create_views in 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.
  2. Alternative: Manually import the macro:

    • Copy cmake/DrogonCreateViews.cmake from 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_views as before.

B. If you want drogon_create_views automatically:

  • Use Drogon as a submodule/subdirectory instead of via Conan:
    1. Clone Drogon's source into your project (e.g., git submodule add ...).
    2. Use add_subdirectory(drogon) in your CMakeLists.txt.
    3. Link with target_link_libraries(${PROJECT_NAME} PRIVATE drogon).
    4. The macro will be available.

  • Quick fix: Comment out or remove drogon_create_views(...) from your CMakeLists.txt if using only the Conan package.
  • Or: Manually include the macro as described above.

slesniew avatar Jun 20 '25 14:06 slesniew