How to check if a library (libssh) is installed with Cmake before adding executable - cmake

I have an executable in cmake that depends on libssh being installed on the system.
I use this to install it:
sudo apt-get install -y libssh-dev
This is my Cmake:
cmake_minimum_required(VERSION 3.5.0)
project(validateTensor VERSION 0.0.1)
find_package(gflags QUIET)
add_executable(myapplication
"myapplication.cpp"
)
target_link_libraries(myapplication gflags teamApplication -lssh)
add_dependencies(myapplication teamApplication)
My question is how can I use cmake to check if libshh is installed on the system before adding the executable. If it is not installed then I want to exclude the executable from the build but not have the build fail.

How to check if a library (libssh) is installed with Cmake before adding executable
With find_library.
find_library(HAVE_SSH NAMES ssh)
add_executable(myapplication
myapplication.cpp
)
target_link_libraries(myapplication gflags teamApplication)
if (HAVE_SSH)
target_link_libraries(myapplication ssh)
endif()
No need to add_dependencies(myapplication teamApplication) - target_link_libraries already "does that".

Related

Cross Compiling IamGui with SDL2 and Vulkan from Ubuntu 22.04 LTS WSL on Rasptberry Pi 4 aarch64 with cmake and aarch64-linux-gnu-gcc/g++

I'm trying to cross compile from Win10 to Raspian OS on the Raspberry Pi 4. For this reason I use the Ubuntu 22.04 LTS WSL. I installed gcc/g++, CMake, Ninja, SDL2, Vulkan SDK, aarch64-linux-gnu-gcc/g++.
In the CMakeLists.txt I call find_package(SDL2 REQUIRED) and find_package(Vulkan REQUIRED).
When I compile with the normal gcc and g++ there is no problem, but when I try the aarch64-linux-gnu-gcc and aarch64-linux-gnu-g++
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)
set(CMAKE_CROSSCOMPILING TRUE)
set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)
Error message:
CMake Error at CMakeLists.txt:66 (find_package):
By not providing "FindSDL2.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "SDL2", but
CMake did not find one.
Could not find a package configuration file provided by "SDL2" with any of
the following names:
SDL2Config.cmake
sdl2-config.cmake
Add the installation prefix of "SDL2" to CMAKE_PREFIX_PATH or set
"SDL2_DIR" to a directory containing one of the above files. If "SDL2"
provides a separate development package or SDK, be sure it has been
installed.
The libs can be found in the folder /usr/lib/x86_64-linux-gnu
Why are they not in the folder /usr/lib/aarch64-linux-gnu?
How can I use or install them for the aarch64-linux-gnu-gcc/g++?
Thank you in advance!
set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)
In this link at the bottom aarch64-linux-gnu files are mentioned, but there are non installed in my /usr/lib/aarch64-linux-gnu folder
https://ubuntu.pkgs.org/20.04/ubuntu-universe-arm64/libsdl2-dev_2.0.10+dfsg1-3_arm64.deb.html

Use CMake binares to build cpp projects without installing CMake

I want to use specific version of CMake 3.19.0 for Ubuntu 14.04 (32-bits) without installing CMake (use only binaries).
I tried to build CMake 3.19.0 on my test machine. It builded and installed successfully. In install_manifest.txt I see lot of files that were installed on my test system.
So, I tried to copy only installed binaries from /usr/local/bin/ (this is default path where CMake binaries were installed) and paste it to another machine that doesn't know about CMake. I paste 3 binaries: cmake, ctest, cpack to /usr/local/bin/.
If I run which cmake it shows path:
/usr/local/bin/cmake
If I run cmake --verison it shows:
CMake Error: Could not find CMAKE_ROOT !!!
CMake has most likely not been installed correctly.
Modules directory not found in
/usr/local/share/cmake-3.19
cmake version 3.19.0
CMake suite maintained and supported by Kitware (kitware.com/cmake).
It looks like CMake needs some modules that I haven't copied yet. I tried to build my cpp project and it shows me:
CMake Error: Could not find CMAKE_ROOT !!!
CMake has most likely not been installed correctly.
Modules directory not found in
/usr/local/share/cmake-3.19
CMake Error: Error executing cmake::LoadCache(). Aborting.
What are the minimum required modules needed for stable building? And where I should copy it?
Just copied builded Modules and Templates directories from cmake-3.19.0 build directory to /usr/local/share/cmake-3.19

cmake install DESTINATION not working on Linux

This is my CMakeLists.txt, it works correctly on Windows and installs the dll to bin and the lib to .lib.
add_library(test SHARED)
target_sources(test PRIVATE test.cpp)
install(TARGETS test ARCHIVE DESTINATION /home/user/cmake_test/out/lib)
install(TARGETS test RUNTIME DESTINATION /home/user/cmake_test/out/bin)
On Linux the DESTINATION seems to be ignored and cmake tries to install test.so to /usr/local/lib/test.so. I could use CMAKE_INSTALL_PREFIX but I can't see anything in the documentation that suggests that this approach shouldn't work on Linux.
I've tried cmake 16 from the Ubuntu 20.04 repos and the latest cmake 3.19.1. Make and Ninja builds both have the same behaviour.

cmake installation on aix installed but command not found

i installed cmake on my AIX 7.2
bash-3.2# yum install cmake
Setting up Install Process
Package cmake-3.16.0-2.ppc already installed and latest version
Nothing to do
bash-3.2#
but running it .
bash-3.2$ cmake
bash: cmake: command not found
there is also no folder for cmake in /opt/bin directory
The RPM's on AIX likely install software to /opt/freeware so you probably need /opt/freeware/bin in your PATH to casually use "cmake" on the command line.
You can verify the paths installed by a package with e.g.
rpm -ql cmake|grep bin/

cmake MSYS Makefiles generator missing

I have cmake 3.2.3 installed via pacman. I get an error when I try to use it from a msys64 shell:
$ cmake -G "MSYS Makefiles" ..
CMake Error: Could not create named generator MSYS Makefiles
cmake --help does not list it as an available generator.
I do see there is an MSYS.cmake in /usr/share/cmake-3.2.3/Modules/Platform.
What am I missing?
Instead of installing the cmake package, I think you need to install mingw-w64-i686-cmake (or the 64-bit version mingw-w64-x86_64-cmake).
I got the exact same message when trying to run cmake in the MSYS shell. Use a MinGW Shell (for instance MinGW-w64 Win64 Shell) instead.
If you compile native Windows binaries on Linux with MinGW
The MinGW and MSYS generators are only available on Windows based distributions. See #ifdef in cmake.cxx:
#if defined(_WIN32) && !defined(__CYGWIN__)
If you're cross-compiling use one of the available MinGW toolchains. See e.g. "How to use MinGW to cross compile software for Windows" chapter in CMake's wiki.
If you compile Windows binaries on Windows with MinGW
On my Windows PC I only have one CMake installation (the normal MSI Windows Installer with CMake directory added to PATH environment), which works from standard CMD shells and from my MSYS shells.
So in this case there is no need to install a special MinGW version of CMake (like e.g. for CygWin).
But I've rebuild CMake from source with MinGW-w64 several times lately to test some performance optimizations of cmake.exe and it did not work out-of-the-box. To work around the linker errors I've added -DCMAKE_EXE_LINKER_FLAGS="-Wl,--allow-multiple-definition" like recommended here and the resulting cmake.exe still supports the "MSYS Makefiles" generator.
So yes, there is - as you have commented - most probably something wrong with the pacman build.
I guess the pacman build is just broken, so I've resolved this issue by installing the Windows version of CMake from cmake.org with the msi installer.