How to Install CMake on OpenWRT

  • • OpenWRT’s opkg supports most software packages, but some specific domain packages still require source installation, such as the SRS server (streaming media server). Source programming typically requires CMake. This article attempts to compile and install CMake from source in OpenWRT system based on Host mode, summarizing the problems encountered and their solutions as follows.

Introduction to CMake

CMake is a cross-platform open-source build system used to manage the build process of software projects.

It is simple yet powerful. First, CMake can automatically generate the build files required by various compilers and platforms, such as Makefile or Visual Studio project files, significantly reducing the burden on developers when porting projects across different systems.

CMake describes the project structure by writing a CMakeLists.txt file, which includes the locations of source files, header files, library files, and the rules for generating target executables or libraries. This makes the project’s organizational structure clear and easy for developers to maintain and modify.

In large projects, the advantages of CMake become even more apparent. It can effectively manage complex dependencies, whether linking external libraries or handling mutual references between internal modules. Moreover, it supports multiple programming languages, such as C, C++, Fortran, etc., facilitating the construction of cross-language projects and promoting collaborative development of complex software systems.

OpenWRT’s opkg does not support CMake installation, but some software like SRS can only be installed via CMake. Therefore, this article attempts to compile and install CMake from source.

Source Download

wget https://github.com/Kitware/CMake/releases/download/v3.31.3/cmake-3.31.3.tar.gz

Installation

Configure Issues

After downloading and extracting the source code, executing configure encounters issues.

/usr/bin/ld: cannot find -ldl

./configure
rhash-librhash-sha256.c.o rhash-librhash-sha3.c.o rhash-librhash-sha512.c.o  -ldl -lrt -o cmake
/usr/bin/ld: cannot find -ldl
/usr/bin/ld: cannot find -lrt

The solution is to temporarily remove the dependency on the dl/rt libraries.

find . -name "*.cmake.in" -exec sed -i  "s/-lrt//g" '{}' 
find . -name "*.cmake.in" -exec sed -i  "s/-ldl//g" '{}' 
sed -i  "s/-ldl//g" bootstrap &&  sed -i  "s/-lrt//g" bootstrap

Could NOT find OpenSSL

-- Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR (missing: OPENSSL_CRYPTO_LIBRARY OPENSSL_INCLUDE_DIR) 
CMake Error at Utilities/cmcurl/CMakeLists.txt:587 (message):
  Could not find OpenSSL.  Install an OpenSSL development package or
  configure CMake with -DCMAKE_USE_OPENSSL=OFF to build without OpenSSL.

The solution is to disable OpenSSL.

./bootstrap -- -DCMAKE_USE_OPENSSL=OFF

Additionally, the Makefile generated by bootstrap still depends on the dl/rt libraries, which need to be completely removed.

# After generating the Makefile, remove -dl/-rt
find . -name "link.txt" -exec sed -i  "s/-ldl//g" '{}' 
find . -name "link.txt" -exec sed -i  "s/-lrt//g" '{}' 
find . -name Makefile2 -exec sed -i  "s/-ldl//g" '{}' 
find . -name Makefile2 -exec sed -i  "s/-lrt//g" '{}' 

Execution

CMake Error: Could not find CMAKE_ROOT !!!

Running the compiled CMake directly works fine, but it fails when ported to other environments.

root@test:~# cmake
CMake Error: Could not find CMAKE_ROOT !!!
CMake has most likely not been installed correctly.
Modules directory not found in
/usr/share/cmake-3.27

The solution is to copy the module directory from the CMake source package to the specified directory.

root@test:~/cmake-3.27.7# mkdir /usr/share/cmake-3.27
root@test:~/cmake-3.27.7# cp Modules/ /usr/share/cmake-3.27/ 

Leave a Comment