brainboxdotcc / DPP

C++ Discord API Bot Library - D++ is Lightweight and scalable for small and huge bots!
https://dpp.dev/
Apache License 2.0
1.01k stars 155 forks source link

Help: How do I make sure the the dpp dll's get copied to the appropriate folder #1172

Closed Advik-B closed 1 month ago

Advik-B commented 1 month ago

Hello, since you guys told me that dpp doesn't support static libraries on windows, I've turned it off (static lib linking)

This is my cmake config

Click to expand my CMakeLists.txt ```cmake cmake_minimum_required(VERSION 3.28) set(PROJECT_NAME "Trying-CPP-stuff") set(EXECUTABLE_NAME "DiscordBot") option(BUILD_SHARED_LIBS "Build shared libraries" ON) set(ASSETS_DIR "${CMAKE_SOURCE_DIR}/assets") set(COPY_ASSETS OFF) project(${PROJECT_NAME} LANGUAGES CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}) find_program(CCACHE_FOUND ccache) if(CCACHE_FOUND) set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache) endif() include(FetchContent) FetchContent_Declare( spdlog GIT_REPOSITORY https://github.com/gabime/spdlog GIT_TAG v1.14.1 ) FetchContent_MakeAvailable(spdlog) FetchContent_Declare(dpp GIT_REPOSITORY https://github.com/brainboxdotcc/DPP.git GIT_TAG v10.0.30 ) FetchContent_MakeAvailable(dpp) # Add include directory include_directories(${CMAKE_SOURCE_DIR}/include) # Recursively add source files in the src directory file(GLOB_RECURSE SOURCES "src/*.cpp") add_executable(${PROJECT_NAME} ${SOURCES}) target_link_libraries( ${PROJECT_NAME} PRIVATE spdlog::spdlog dpp ) if (EXECUTABLE_NAME STREQUAL "") set(EXECUTABLE_NAME ${PROJECT_NAME}) endif() set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "${EXECUTABLE_NAME}") ```

And this is my main.cpp

Click to expand main.cpp ```cpp #include const std::string BOT_TOKEN; int main() { dpp::cluster bot(BOT_TOKEN); bot.on_log(dpp::utility::cout_logger()); bot.on_slashcommand([](const dpp::slashcommand_t& event) { if (event.command.get_command_name() == "ping") { event.reply("Pong!"); } }); bot.on_ready([&bot](const dpp::ready_t&) { if (dpp::run_once()) { bot.global_command_create(dpp::slashcommand("ping", "Ping pong!", bot.me.id)); } }); bot.start(dpp::st_wait); } ```

However, whenever I press Shift + F10 (run in CLion)

It immediately crashes with exit code -1073741515

Upon looking up said exit code, it was saying that the program was missing a dll in the executable folder (which was surprising as dpp.dll WAS present)

So. I took a look at the dpp provided template for windows so that I wasn't missing any dlls

As it turned out, I was actually missing QUITE A FEW: image

None of these dlls were in the build folder, so I manually copied them and only then, the program worked fine.

I thought any dependents of dpp will also be copied over once built:

Click to show CMake generation logs ``` -- The CXX compiler identification is MSVC 19.40.33811.0 -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: D:/Programs/Visual Studio/VC/Tools/MSVC/14.40.33807/bin/Hostx64/x64/cl.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Build spdlog: 1.14.1 -- Performing Test CMAKE_HAVE_LIBC_PTHREAD -- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed -- Looking for pthread_create in pthreads -- Looking for pthread_create in pthreads - not found -- Looking for pthread_create in pthread -- Looking for pthread_create in pthread - not found -- Found Threads: TRUE -- Build type: Debug -- INFO: Using VCPKG if detected -- INFO: Configuring .rc resource script -- Looking for prctl -- Looking for prctl - not found -- Looking for pthread_setname_np -- Looking for pthread_setname_np - not found -- Performing Test AVX1 -- Performing Test AVX1 - Success -- Performing Test AVX2 -- Performing Test AVX2 - Success -- Performing Test AVX512 -- Performing Test AVX512 - Failed -- Detected AMD64 AVX type: AVX2 (FLAGS: /arch:AVX2) -- Building for windows with precompiled packaged dependencies -- Found Opus O:/Development/Other Code/trying-new-cpp-shit/cmake-build-debug/_deps/dpp-src/library/../win32/lib/opus.lib -- Sodium win32 bundled -- Detected libsodium and libopus. VOICE support will be enabled -- Building git version. Be aware git versions may be unstable! -- Configuring test: soaktest with source: ../src/soaktest/*.cpp -- Configuring test: unittest with source: ../src/unittest/*.cpp -- Using bundled nlohmann::json -- Configuring done (118.3s) -- Generating done (0.1s) -- Build files have been written to: O:/Development/Other Code/trying-new-cpp-shit/cmake-build-debug ```

Other observations:

Mishura4 commented 1 month ago

You can use the template, these things are already set up for you.

I thought any dependents of dpp will also be copied over once built

No. Only in the template, because those things are set up at the project level, not at the library level. This is set with the MyBot project files in the template.

The initial build of the project is painfully long (as it builds from the source)

Yes.

dpp.ilk (Next to dpp.dll in the build) is is 4.77 GB

Yes.

Why dosent dpp copy over the dlls that it required?

Why would it? How could it? We have no knowledge of where your project executable is meant to go. This is something you have to do yourself at the project level. Here's an example: https://github.com/Mishura4/Mimiron/blob/main/CMakeLists.txt#L47-L65

Mishura4 commented 1 month ago

For what it's worth this beast helps a lot with build times ;) #NotAnAd

braindigitalis commented 1 month ago

For what it's worth this beast helps a lot with build times ;) #NotAnAd

I'll take four...

Advik-B commented 1 month ago

Why would it? How could it? We have no knowledge of where your project executable is meant to go. This is something you have to do yourself at the project level. Here's an example: https://github.com/Mishura4/Mimiron/blob/main/CMakeLists.txt#L47-L65

Thanks for the snippet 😄

For what it's worth this beast helps a lot with build times ;) #NotAnAd

image

Third world country problems 🥲 #BrokeAF maybe one day... one day...

Advik-B commented 1 month ago

I'll close this issue once I test with the snippet provided

Advik-B commented 1 month ago

After look at your at your snippet for reference, I've added the following lines to my CMakeLists.txt

# If 32 bit windows then the dll's are in cmake-build-debug/_deps/dpp-src/win32/32/bin
# If 64 bit windows then the dll's are in cmake-build-debug/_deps/dpp-src/win32/bin
if (MSVC)
    if (NOT WINDOWS_32_BIT)
        set(DPP_DLL_DIR "${CMAKE_BINARY_DIR}/_deps/dpp-src/win32/bin")
    else()
        set(DPP_DLL_DIR "${CMAKE_BINARY_DIR}/_deps/dpp-src/win32/32/bin")
    endif()

    add_custom_command(
        TARGET ${PROJECT_NAME}
        POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_directory ${DPP_DLL_DIR} $<TARGET_FILE_DIR:${PROJECT_NAME}>
        COMMENT "Copying DPP runtime dependencies"
    )
endif()

Now, the dll's will be copied to the build folder 😄

I am planning on make a template repo for CMake cross-platform so that they dont have to face the same issues that I did

Thank you @Mishura4