cpm-cmake / CPM.cmake

📦 CMake's missing package manager. A small CMake script for setup-free, cross-platform, reproducible dependency management.
MIT License
2.97k stars 184 forks source link

How to install Jsoncpp/drogon with cpm.cmake ? #316

Closed renhiyama closed 2 years ago

renhiyama commented 2 years ago

I have wasted 3 days trying every method possible, reading all the docs online, but still the cmake crashes... Heres my cmake list:

# minimum CMake version required
cmake_minimum_required(VERSION 3.15)

# Project name, version and description
project(rdl.server VERSION 1.0 DESCRIPTION "RDL server")

# Create an executable
add_executable(${PROJECT_NAME}
    src/main.cpp
    # your others files...
)

# add dependencies
include(../CPM.cmake)

# CPM packages
CPMAddPackage(
  NAME Ccache.cmake
  GITHUB_REPOSITORY TheLartians/Ccache.cmake
  VERSION 1.2
)

CPMAddPackage(
    NAME Jsoncpp
    URL https://github.com/open-source-parsers/jsoncpp/archive/refs/tags/1.9.5.tar.gz
    VERSION 1.9.5
)

get_target_property(JSON_INC_PATH jsoncpp_lib INTERFACE_INCLUDE_DIRECTORIES)
include_directories(${JSON_INC_PATH})

CPMAddPackage("gh:drogonframework/drogon@1.7.4")

CPMAddPackage(
    NAME jtv_libpqxx
    VERSION 7.6.0
    URL https://github.com/jtv/libpqxx/archive/refs/tags/7.6.0.zip
)
if(Jsoncpp_ADDED)
  add_library(Jsoncpp INTERFACE IMPORTED)
  target_include_directories(Jsoncpp INTERFACE "${Jsoncpp_SOURCE_DIR}/include")
endif()

# package lock to lock down to specific versions of deps
CPMUsePackageLock(package-lock.cmake)

# Linking libraries
target_link_libraries(${PROJECT_NAME} PUBLIC
    ${Jsoncpp_LIBRARY}
    drogon
)

# Specify includes
target_include_directories(${PROJECT_NAME} PRIVATE
    libs/drogon/include
)

# Set C++ version
set_target_properties(${PROJECT_NAME} PROPERTIES
    CXX_STANDARD 20
    CXX_STANDARD_REQUIRED ON
)

output for cmake

-- CPM: adding package Ccache.cmake@1.2 (v1.2 at /home/user/.cache/CPM/ccache.cmake/3efab1d354e8959e8f108e5431ec8ee51c613af4)
-- JsonCpp Version: 1.9.5
-- CPM: adding package Jsoncpp@1.9.5 (1.9.5 at /home/user/.cache/CPM/jsoncpp/87455b6dd192e67ab53212caf25789970ff319ff)
-- compiler: GNU
-- Could NOT find c-ares (missing: C-ARES_INCLUDE_DIRS C-ARES_LIBRARIES) 
-- Could NOT find GTest (missing: GTEST_LIBRARY GTEST_INCLUDE_DIR GTEST_MAIN_LIBRARY) 
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE) 
-- Found std::filesystem
-- use c++17
-- Found UUID: /usr/lib/x86_64-linux-gnu/libuuid.so
-- Brotli found
-- pg inc: /usr/include/postgresql/usr/include/postgresql/13/server
-- libpq inc path:/usr/include/postgresql/usr/include/postgresql/13/server
-- libpq lib:/usr/lib/x86_64-linux-gnu/libpq.so
-- Could NOT find MySQL (missing: MYSQL_LIBRARIES MYSQL_INCLUDE_DIRS) 
-- Could NOT find SQLite3 (missing: SQLITE3_LIBRARIES SQLITE3_INCLUDE_DIRS) 
-- Could NOT find Hiredis (missing: HIREDIS_LIBRARY HIREDIS_INCLUDE_DIR) 
-- cspFile:/home/user/.cache/CPM/drogon/be2e89a031a10193d100d7b801a1411cccac5a5e/drogon_ctl/templates/cmake.csp
-- view classname:cmake
-- cspFile:/home/user/.cache/CPM/drogon/be2e89a031a10193d100d7b801a1411cccac5a5e/drogon_ctl/templates/config.csp
-- view classname:config
-- cspFile:/home/user/.cache/CPM/drogon/be2e89a031a10193d100d7b801a1411cccac5a5e/drogon_ctl/templates/demoMain.csp
-- view classname:demoMain
-- cspFile:/home/user/.cache/CPM/drogon/be2e89a031a10193d100d7b801a1411cccac5a5e/drogon_ctl/templates/filter_cc.csp
-- view classname:filter_cc
-- cspFile:/home/user/.cache/CPM/drogon/be2e89a031a10193d100d7b801a1411cccac5a5e/drogon_ctl/templates/filter_h.csp
-- view classname:filter_h
-- cspFile:/home/user/.cache/CPM/drogon/be2e89a031a10193d100d7b801a1411cccac5a5e/drogon_ctl/templates/gitignore.csp
-- view classname:gitignore
-- cspFile:/home/user/.cache/CPM/drogon/be2e89a031a10193d100d7b801a1411cccac5a5e/drogon_ctl/templates/model_cc.csp
-- view classname:model_cc
-- cspFile:/home/user/.cache/CPM/drogon/be2e89a031a10193d100d7b801a1411cccac5a5e/drogon_ctl/templates/model_h.csp
-- view classname:model_h
-- cspFile:/home/user/.cache/CPM/drogon/be2e89a031a10193d100d7b801a1411cccac5a5e/drogon_ctl/templates/model_json.csp
-- view classname:model_json
-- cspFile:/home/user/.cache/CPM/drogon/be2e89a031a10193d100d7b801a1411cccac5a5e/drogon_ctl/templates/plugin_cc.csp
-- view classname:plugin_cc
-- cspFile:/home/user/.cache/CPM/drogon/be2e89a031a10193d100d7b801a1411cccac5a5e/drogon_ctl/templates/plugin_h.csp
-- view classname:plugin_h
-- cspFile:/home/user/.cache/CPM/drogon/be2e89a031a10193d100d7b801a1411cccac5a5e/drogon_ctl/templates/restful_controller_base_cc.csp
-- view classname:restful_controller_base_cc
-- cspFile:/home/user/.cache/CPM/drogon/be2e89a031a10193d100d7b801a1411cccac5a5e/drogon_ctl/templates/restful_controller_base_h.csp
-- view classname:restful_controller_base_h
-- cspFile:/home/user/.cache/CPM/drogon/be2e89a031a10193d100d7b801a1411cccac5a5e/drogon_ctl/templates/restful_controller_cc.csp
-- view classname:restful_controller_cc
-- cspFile:/home/user/.cache/CPM/drogon/be2e89a031a10193d100d7b801a1411cccac5a5e/drogon_ctl/templates/restful_controller_custom_cc.csp
-- view classname:restful_controller_custom_cc
-- cspFile:/home/user/.cache/CPM/drogon/be2e89a031a10193d100d7b801a1411cccac5a5e/drogon_ctl/templates/restful_controller_custom_h.csp
-- view classname:restful_controller_custom_h
-- cspFile:/home/user/.cache/CPM/drogon/be2e89a031a10193d100d7b801a1411cccac5a5e/drogon_ctl/templates/restful_controller_h.csp
-- view classname:restful_controller_h
-- cspFile:/home/user/.cache/CPM/drogon/be2e89a031a10193d100d7b801a1411cccac5a5e/drogon_ctl/templates/test_cmake.csp
-- view classname:test_cmake
-- cspFile:/home/user/.cache/CPM/drogon/be2e89a031a10193d100d7b801a1411cccac5a5e/drogon_ctl/templates/test_main.csp
-- view classname:test_main
-- bin:bin
-- Building tests
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE) 
-- CPM: adding package drogon@1.7.4 (v1.7.4 at /home/user/.cache/CPM/drogon/be2e89a031a10193d100d7b801a1411cccac5a5e)
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Generating config.h
-- Generating config.h - done
-- CPM: adding package jtv_libpqxx@7.6.0 (7.6.0 at /home/user/.cache/CPM/jtv_libpqxx/54ce1cfd8b83f11cc5340e42503effb760b27844)
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/discord-list/server/build
renhiyama commented 2 years ago

make:

[ 12%] Built target trantor
Scanning dependencies of target drogon
[ 12%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/lib/src/ConfigLoader.cc.o
[ 12%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/lib/src/FiltersFunction.cc.o
[ 12%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/lib/src/HttpAppFrameworkImpl.cc.o
[ 12%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/lib/src/HttpClientImpl.cc.o
[ 12%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/lib/src/HttpControllersRouter.cc.o
[ 12%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/lib/src/HttpFileImpl.cc.o
[ 12%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/lib/src/HttpRequestImpl.cc.o
[ 12%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/lib/src/HttpRequestParser.cc.o
[ 12%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/lib/src/HttpResponseImpl.cc.o
[ 12%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/lib/src/HttpServer.cc.o
[ 12%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/lib/src/HttpSimpleControllersRouter.cc.o
[ 12%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/lib/src/ListenerManager.cc.o
[ 12%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/lib/src/MultiPart.cc.o
[ 12%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/lib/src/StaticFileRouter.cc.o
[ 14%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/lib/src/Utilities.cc.o
[ 14%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/lib/src/WebSocketClientImpl.cc.o
[ 14%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/lib/src/WebSocketConnectionImpl.cc.o
[ 14%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/lib/src/WebsocketControllersRouter.cc.o
[ 16%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/lib/src/SharedLibManager.cc.o
[ 16%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/orm_lib/src/postgresql_impl/PgConnection.cc.o
[ 16%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/lib/src/RedisClientManagerSkipped.cc.o
[ 18%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/orm_lib/src/DbClient.cc.o
[ 18%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/orm_lib/src/DbClientImpl.cc.o
[ 18%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/orm_lib/src/DbClientLockFree.cc.o
[ 18%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/orm_lib/src/DbConnection.cc.o
[ 20%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/orm_lib/src/SqlBinder.cc.o
[ 20%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/orm_lib/src/TransactionImpl.cc.o
[ 20%] Building CXX object _deps/drogon-build/CMakeFiles/drogon.dir/orm_lib/src/DbClientManager.cc.o
[ 22%] Linking CXX static library ../../lib/libdrogon.a
[ 40%] Built target drogon
[ 42%] Linking CXX executable bin/rdl.server
/usr/bin/ld: cannot find -lJsoncpp_lib
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/rdl.server.dir/build.make:115: bin/rdl.server] Error 1
make[1]: *** [CMakeFiles/Makefile2:628: CMakeFiles/rdl.server.dir/all] Error 2
make: *** [Makefile:171: all] Error 2
Andres6936 commented 2 years ago

At first I tried to use the portion of code you provided to see if it was failures in your CMakeLists. txt, but it wasn't and yet I received a similar error in MSVC 2019, I dug into the code as it seemed strange to me that it would fail to link a headers only library, it failed and failed and I even started to doubt if I was creating a library of only headers correctly, until I decided to copy the headers and move them to the root directory of the project that I had created as an example, link them to the executable and surprise failure with the same error, with this I supposed that it was not an error when creating the library but an error in the definitions of the methods, I reviewed the code and I found the root of the problem: You are trying to link a library as a header-only library when it is not.

Here is my solution:

CMAKE_MINIMUM_REQUIRED(VERSION 3.14)
PROJECT(Help)

SET(CMAKE_CXX_STANDARD 17)

INCLUDE(CPM.cmake)

CPMAddPackage(
        NAME Jsoncpp
        URL https://github.com/open-source-parsers/jsoncpp/archive/refs/tags/1.9.5.tar.gz
        VERSION 1.9.5
)

get_target_property(JSON_INC_PATH jsoncpp_lib INTERFACE_INCLUDE_DIRECTORIES)
include_directories(${JSON_INC_PATH})

ADD_EXECUTABLE(Help main.cpp)
TARGET_LINK_LIBRARIES(Help PUBLIC jsoncpp_static)
#include "json/json.h"
#include <iostream>

int main() {
    Json::Value root;
    Json::Value data;
    constexpr bool shouldUseOldWay = false;
    root["action"] = "run";
    data["number"] = 1;
    root["data"] = data;

    if (shouldUseOldWay) {
        Json::FastWriter writer;
        const std::string json_file = writer.write(root);
        std::cout << json_file << std::endl;
    } else {
        Json::StreamWriterBuilder builder;
        const std::string json_file = Json::writeString(builder, root);
        std::cout << json_file << std::endl;
    }
    return EXIT_SUCCESS;
}
C:\Users\andre\CLionProjects\Help\cmake-build-debug\bin\Help.exe
{
        "action" : "run",
        "data" :
        {
                "number" : 1
        }
}

In fact, if you check the code you will find a different name of the library depending on whether it is compiled as an object of type SHARED or STATIC, so you have to be careful with that when working with your executables or other libraries.

The library even has the definition of the library for a type OBJECT which will be very useful to avoid all this code amalgamation.

renhiyama commented 2 years ago

Oh thanks!

Andres6936 commented 2 years ago

If it worked correctly and you are satisfied with the solution you can close the issue, thank you very much @renhiyama

renhiyama commented 2 years ago

Oh ok sure!