drogon
drogon copied to clipboard
Unable to build with WSL2
I'm trying to build my project under WSL2 but always I get error on CMAKE_INSTALL_PREFIX variable being defined multiple times. I can correctly build and run project under Windows with vcpkg installed libs, but I can't do the same for WSL2.
I added these lines in CMakeLists.txt file:
if(WIN32)
message(STATUS "Configuring for Windows")
set(CMAKE_TOOLCHAIN_FILE "[...]/vcpkg/scripts/buildsystems/vcpkg.cmake")
set(VCPKG_TARGET_TRIPLET "x64-windows")
elseif(UNIX AND NOT APPLE)
message(STATUS "Configuring for Linux")
set(CMAKE_TOOLCHAIN_FILE "[...]/vcpkg/scripts/buildsystems/vcpkg.cmake")
set(VCPKG_TARGET_TRIPLET "x64-linux")
endif()
It correctly finds all the libs as I see Found Brotli: [...]/vcpkg/installed/x64-linux/debug/lib/libbrotlidec-static.a
and it takes ~ 139 seconds to build all, instead of 10 secs in windows env.
As soon as I start build it I get CMAKE_INSTALL_PREFIX error plus other 900 error too.
My goal is to use build this project for windows and arm64 linux, but I'm only able to build for windows...
What I'm missing?
Drogon can be built using WSL2 by installing its dependencies with sudo apt install <pkg_name>
, running cmake . -B ./build
in the project folder, and then make
if you are using Unix Makefiles.
For Ninja build system, run cmake . -B ./build -G Ninja
, and then ninja -C ./build
.
I haven't tried building the project using Visual Studio for Linux. I used VS and vcpkg for building drogon for Windows, then switched to Neovim and CMake/Ninja for Linux on WSL2.
Drogon can be built using WSL2 by installing its dependencies with
sudo apt install <pkg_name>
, runningcmake . -B ./build
in the project folder, and thenmake
if you are using Unix Makefiles.For Ninja build system, run
cmake . -B ./build -G Ninja
, and thenninja -C ./build
.I haven't tried building the project using Visual Studio for Linux. I used VS and vcpkg for building drogon for Windows, then switched to Neovim and CMake/Ninja for Linux on WSL2.
I don't use VS for Linux, instead I'm on Windows machine and connect to WSL2, but it is missing something and can't build the project even if all packages are installed (from apt install or vcpkg install). The Cmake output is ok as you can see (brotli and other dependencies found)
Does the path to your vcpkg contain spaces?
And when you double click the CMAKE_INSTALL_PREFIX variable being defined multiple times error, does it take you anywhere?
Does the path to your vcpkg contain spaces?
No. I'm sure CMake finds installed package on WSL2, otherwise it would fail before build time
And when you double click the CMAKE_INSTALL_PREFIX variable being defined multiple times error, does it take you anywhere?
Unluckily no, that error is making me crazy
Another question is, I've Ubuntu 23 on this WSL2, will I be able to build for arm64-linux? Or should I use a raspberry with linux on it to correctly build arm64 version of my project?
I would suggest that you use Visual Studio for Windows development, and use Linux specific programs to do the building on Linux. For code editing, you can use any code editor, it can be Visual Studio or VSCode. For building, It is best to stick to the platform specific programs instead of bridging commands and paths from one to another.
Another question is, I've Ubuntu 23 on this WSL2, will I be able to build for arm64-linux? Or should I use a raspberry with linux on it to correctly build arm64 version of my project?
Don't try building Drogon or any big enough project on a Raspberry Pi that is not as powerful as your development machine, I compiled a drogon project with another library called rocksdb, if I recall, it took 3 to 4 hours to compile on 1 thread, because of memory limitations. If you have more memory than 1 GB on the Raspberry Pi, you can use multiple threads.
I suggest using toolchains and cross-compilation. Basically you get the installed packages from your Raspberry Pi to your development machine, and tell CMake to treat these /lib
and /include
folders as the go-to for searching for libs and headers. You will also copy over all binary executables from the Raspberry Pi over to the development machine, that is including the compiler and some other executables if needed.
This is a detailed step by step instruction manual on how to do this.
The toolchain file for Raspberry Pi OS 64-bit:
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR aarch64)
set(tools /home/muhammad/sysroots/rpi/gcc) # warning change toolchain path here.
set(rootfs_dir /home/muhammad/sysroots/rpi) # warning change root dir path here.
set(CMAKE_FIND_ROOT_PATH ${rootfs_dir})
set(CMAKE_SYSROOT ${rootfs_dir})
set(CMAKE_LIBRARY_ARCHITECTURE aarch64-linux-gnu) # warning change architecture here.
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIC -Wl,-rpath-link,${CMAKE_SYSROOT}/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE} -L${CMAKE_SYSROOT}/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}")
set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wl,-rpath-link,${CMAKE_SYSROOT}/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE} -L${CMAKE_SYSROOT}/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wl,-rpath-link,${CMAKE_SYSROOT}/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE} -L${CMAKE_SYSROOT}/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}")
## Compiler Binary
SET(BIN_PREFIX ${tools}/bin/${CMAKE_LIBRARY_ARCHITECTURE})
SET (CMAKE_C_COMPILER ${BIN_PREFIX}-gcc)
SET (CMAKE_CXX_COMPILER ${BIN_PREFIX}-g++ )
SET (CMAKE_LINKER ${BIN_PREFIX}-ld
CACHE STRING "Set the cross-compiler tool LD" FORCE)
SET (CMAKE_AR ${BIN_PREFIX}-ar
CACHE STRING "Set the cross-compiler tool AR" FORCE)
SET (CMAKE_NM {BIN_PREFIX}-nm
CACHE STRING "Set the cross-compiler tool NM" FORCE)
SET (CMAKE_OBJCOPY ${BIN_PREFIX}-objcopy
CACHE STRING "Set the cross-compiler tool OBJCOPY" FORCE)
SET (CMAKE_OBJDUMP ${BIN_PREFIX}-objdump
CACHE STRING "Set the cross-compiler tool OBJDUMP" FORCE)
SET (CMAKE_RANLIB ${BIN_PREFIX}-ranlib
CACHE STRING "Set the cross-compiler tool RANLIB" FORCE)
SET (CMAKE_STRIP {BIN_PREFIX}-strip
CACHE STRING "Set the cross-compiler tool RANLIB" FORCE)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)