How do we use packages installed from vcpkg? - c++

I'm installing packages (aws-sdk, jsoncpp, ..) from vcpkg into my cmake project. But I'm having hard time trying to use them in my project, it's really difficult to figure out how to get cmake to find the installed packages, somehow I managed to add the target path for AWS-SDK, but I'm finding it difficult for other packages. So my question is, how do we get cmake to find packages installed using vcpkg?
Here is how my CMakeLists.txt file looks like:
cmake_minimum_required(VERSION 3.2)
set(CMAKE_CXX_STANDARD 11)
project(compiler LANGUAGES CXX)
include_directories(${JSONCPP_INCLUDE_DIRS})
find_package(AWSSDK REQUIRED COMPONENTS s3)
add_executable(${PROJECT_NAME} compiler.cpp)
target_link_libraries(${PROJECT_NAME} ${AWSSDK_LINK_LIBRARIES})
target_link_libraries(${PROJECT_NAME} ${JSONCPP_LINK_LIBRARIES})
And here is the docker file where I'm installing vcpkg and then these packages.
FROM ubuntu:18.04
WORKDIR /app
RUN apt-get update && \
apt-get install ffmpeg \
g++ \
git \
curl \
zip \
unzip \
tar \
openssl \
gcc \
cmake \
zlib1g-dev \
libcurl4-openssl-dev \
libssl-dev \
uuid-dev \
libpulse-dev \
make \
pkg-config -y
RUN git clone https://github.com/Microsoft/vcpkg.git && \
./vcpkg/bootstrap-vcpkg.sh
RUN ./vcpkg/vcpkg install "aws-sdk-cpp[s3]" --recurse
RUN ./vcpkg/vcpkg install "jsoncpp"
COPY . .
RUN DIR=compiler && mkdir ${DIR} && cd ${DIR} && \
cmake .. -DCMAKE_TOOLCHAIN_FILE=../vcpkg/scripts/buildsystems/vcpkg.cmake && \
cmake --build .
CMD cd compiler && ls -a && ./compiler

Related

Error executing a cmake project with Conan dependencies build via Clion's Docker integration

I'm trying to run a cmake project using conan as a package manager where the project is compiled in a docker container. I'm using Clion's docker integration support as outlined here.
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(FormatOutput CXX)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR})
add_definitions("-std=c++11")
if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/0.18.1/conan.cmake"
"${CMAKE_BINARY_DIR}/conan.cmake"
TLS_VERIFY ON)
endif()
include(${CMAKE_BINARY_DIR}/conan.cmake)
conan_cmake_configure(REQUIRES fmt/9.1.0
GENERATORS cmake_find_package)
conan_cmake_autodetect(settings)
conan_cmake_install(PATH_OR_REFERENCE .
BUILD missing
REMOTE conancenter
SETTINGS ${settings})
find_package(fmt)
add_executable(main main.cpp)
target_link_libraries(main fmt::fmt)
Dockerfile
FROM debian:sid
RUN DEBIAN_FRONTEND="noninteractive" apt-get update && apt-get -y install tzdata
RUN apt-get update \
&& apt-get install -y build-essential \
gcc \
g++ \
gdb \
clang \
make \
ninja-build \
cmake \
autoconf \
automake \
libtool \
valgrind \
locales-all \
dos2unix \
rsync \
tar \
python3 \
python3-dev \
python3-pip \
&& apt-get clean
RUN pip install conan
ENV CONAN_USER_HOME=/.conan_docker
RUN mkdir $CONAN_USER_HOME && chmod 777 $CONAN_USER_HOME
docker-compose.yml
version: "3.4"
services:
clion-cpp-env:
image: clion/debian/cpp-env:1.0
build: .
platform: linux/amd64
cap_add:
- SYS_PTRACE
ports:
- 2222:22
volumes:
- "/tmp/conan_docker:/.conan_docker"
restart: on-failure
Clion Docker Toolchain Configuration
Build output
-- Conan: Using autogenerated Findfmt.cmake
-- Found fmt: 9.1.0 (found version "9.1.0")
-- Library fmtd found /.conan_docker/.conan/data/fmt/9.1.0/_/_/package/0d8b943d676dc202f180a2598d04457e173d7b97/lib/libfmtd.a
-- Found: /.conan_docker/.conan/data/fmt/9.1.0/_/_/package/0d8b943d676dc202f180a2598d04457e173d7b97/lib/libfmtd.a
-- Library fmtd found /.conan_docker/.conan/data/fmt/9.1.0/_/_/package/0d8b943d676dc202f180a2598d04457e173d7b97/lib/libfmtd.a
-- Found: /.conan_docker/.conan/data/fmt/9.1.0/_/_/package/0d8b943d676dc202f180a2598d04457e173d7b97/lib/libfmtd.a
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/docker_conan/cmake-build-debug-docker
[Finished]
Output on trying to run executable
====================[ Build | main | Debug-Docker ]=============================
/usr/bin/cmake --build /tmp/docker_conan/cmake-build-debug-docker --target main -j 6
ninja: error: '/.conan_docker/.conan/data/fmt/9.1.0/_/_/package/0d8b943d676dc202f180a2598d04457e173d7b97/lib/libfmtd.a', needed by 'main', missing and no known rule to make it
While the local compilation builds and runs without complaints, on compiling with the Docker toolchain on Clion, I'm able to build but not run the executable. Apparently the library is not found where conan installed it previously. How do I make this work?
Thanks to #Alan Birtles suggestion, the flaw in my approach was that the volumes/mount information was missing from my container instance.
Although I'd mentioned it in my docker-compose.yml file, it's likely that Clion is not running the docker container via docker-compose, and instead using the docker run command internally instead.
Thus, one needs to append to one's Docker Container Settings: the following volume mount information:
-v /path/to/localmachine/conan-docker:/conan_docker

Adding gRPC as dependency via CMAKE fails

I building a project that depends on gRPC. In the project I want to use gRPC, I'm using the following lines to find the package and add it as a dependency
# gRPC
find_package(gRPC CONFIG REQUIRED)
message(STATUS "Using gRPC ${gRPC_VERSION}")
#gRPC C++ plugin
find_program(gRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
In the above snippet, cmake fails while executing find_package command and outputs the following error:
CMake Error at /usr/local/lib/cmake/grpc/gRPCConfig.cmake:15 (include):
include could not find load file:
/usr/local/lib/cmake/grpc/gRPCTargets.cmake
Call Stack (most recent call first):
src/CMakeLists.txt:15 (find_package)
I'm building the project inside a docker image where I've installed gRPC in the image, and the above error happens when trying to build the project that depends on it. This is how gRPC is installed in the Dockerfile (following the documentation)
RUN cd /tmp && git clone --recurse-submodules -b v1.46.3 --depth 1 --shallow-submodules https://github.com/grpc/grpc && \
cd grpc && git submodule update --init && \
mkdir build && cd build && \
cmake -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=/usr/local .. && \
make -j8 && \
make install && \
ldconfig && \
make clean && \
cd ../.. && \
rm -r grpc
Looking at the source code for gRPCConfig.cmake, it fails at the last line
(include(${CMAKE_CURRENT_LIST_DIR}/gRPCTargets.cmake)
It can't find the file gRPCTargets.cmake and the file doesn't exist in the grpc repo.
Has anyone faced a similar issue? How can I fix this?

What is a correct way to setup protobuf and grpc for cpp project?

I have a cpp project, and I want to build everything from source, to get latest things likned in. So under my project root I've created a 3rd_party folder and assemble following script:
sudo apt-get install autoconf automake libtool curl make g++ unzip -y
git clone https://github.com/google/protobuf.git
cd protobuf
git submodule update --init --recursive
./autogen.sh
./configure
make
make check
sudo make install
sudo ldconfig
cd ..
echo installing grpc
git clone --recurse-submodules -b v1.43.0 https://github.com/grpc/grpc
export MY_INSTALL_DIR=$HOME/.local
be sure that its exists
cd grpc
mkdir -p cmake/build
pushd cmake/build
cmake -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=$MY_INSTALL_DIR ../..
make -j
make install
popd
i've encountered following flaws:
grpc v1.43.0 clones 3rd party submodule protoc v3.18, which after build has problems - protoc binary says "cpp plugin not found", when trying to generate
To overcome that I've copied sources obtained in the first part of script, to 3rd party subfolder of second part, to gurantee it to be 3.19 - then after compilation protoc working great, plugins are in place, and grpc is linked against latest version.
Very weird issue, needs understanding why it clones outdated version, and why 3.18 has no plugins under same build parameters as 3.19
in cmakelists find_package(Protobuf REQUIRED) fails at all
find_package( gRPC REQUIRED ) is not operating properly:
Found package configuration file:
[cmake]
[cmake] /home/a/.local/lib/cmake/grpc/gRPCConfig.cmake
[cmake]
[cmake] but it set gRPC_FOUND to FALSE so package "gRPC" is considered to be NOT
[cmake] FOUND. Reason given by package:
[cmake]
[cmake] The following imported targets are referenced, but are missing:
[cmake] protobuf::libprotobuf protobuf::libprotoc
Question
How can I write script, that in an empty folder will get me everything, related to grpc 3rd party libs and tools, that I need for project?
Here are the steps I took:
$ mkdir .local
$ export INSTALL_PREFIX="$PWD/.local"
$ export CMAKE_GENERATOR=Ninja # optional, but recommended
$ git clone --recurse-submodules -b v3.19.3 --depth 1 https://github.com/google/protobuf.git
$ pushd protobuf
$ ./autogen.sh
$ ./configure --prefix=$INSTALL_PREFIX
$ make -j$(nproc)
$ make install
$ popd
$ git clone --recurse-submodules -b v1.43.0 --depth 1 https://github.com/grpc/grpc.git
$ cmake -S grpc -B .build/grpc \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX \
-DCMAKE_PREFIX_PATH=$INSTALL_PREFIX \
-DgRPC_INSTALL=ON \
-DgRPC_BUILD_TESTS=OFF \
-DgRPC_PROTOBUF_PROVIDER=package \
-DABSL_PROPAGATE_CXX_STD=ON
$ cmake --build .build/grpc --target install
...
$ mkdir example
$ echo "int main () { return 0; }" > example/main.cpp
$ vim example/CMakeLists.txt
$ cat example/CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
project(example)
find_package(gRPC REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main PRIVATE gRPC::grpc++)
$ cmake -S example -B .build/example \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_PREFIX_PATH=$INSTALL_PREFIX
$ cmake --build .build/example
...
I build protobuf separately because gRPC doesn't set up its dependencies in CMake correctly when using its internal protobuf build (I tried both ways). This involves passing --prefix to protobuf's ./configure script and passing -DgRPC_PROTOBUF_PROVIDER=package to gRPC's CMake build. The latter is a gRPC-specific variable that tells it not to build protobuf, but to search for it instead. I tell gRPC where to find protobuf by setting CMAKE_PREFIX_PATH, which is a standard variable.

C++ project with OpenCV as external using cmake

I'm new to cmake, and could't figure out how to achieve this after a few days.
I'm trying to build a C++ project that depends on OpenCV using cmake, but I want cmake to clone and install it like this. I found a project's CMakeFile that I'm using as reference to accomplish this.So I have this:
main.cpp:
#include <iostream>
#include <opencv2/opencv.hpp>
int main() {
std::cout << "OpenCV Version: " + cv::getVersionString() << std::endl;
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.12)
project(cv_playground)
set(CMAKE_CXX_STANDARD 14)
find_package(Git REQUIRED)
include(ExternalProject)
# OpenCV
set(OPENCV_INSTALL_LOCATION ${CMAKE_BINARY_DIR}/opencv)
ExternalProject_Add(opencv
GIT_REPOSITORY https://github.com/opencv/opencv
GIT_TAG 68942affdbc4677aa845bc4307d4752182324a0e # 4.0.0-alpha
SOURCE_DIR opencv
BINARY_DIR opencv-build
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=${OPENCV_INSTALL_LOCATION}
)
include_directories(${OPENCV_INSTALL_LOCATION}/include/opencv4)
link_directories(${OPENCV_INSTALL_LOCATION}/lib)
add_executable(cv_playground main.cpp)
add_dependencies(cv_playground opencv)
target_link_libraries(cv_playground opencv_core opencv_dnn opencv_features2d opencv_flann opencv_highgui opencv_imgcodecs)
But when building the project I get a lot of undefined references (pthread, gz, dlopen/dlclose, itt_domain_create_ptr, etc)
I would like to know how to fix those undef refs, I already installed pthread, zlib, etc but I don't know how to make cmake to use them. I tried to add them in the target_link_libraries for example but still gives me the same errors:
CMakeLists.txt (note the CMAKE_DL_LIBS in target_link_libraries):
cmake_minimum_required(VERSION 3.12)
project(cv_playground)
set(CMAKE_CXX_STANDARD 14)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
find_package(Git REQUIRED)
include(ExternalProject)
# OpenCV
set(OPENCV_INSTALL_LOCATION ${CMAKE_BINARY_DIR}/opencv)
ExternalProject_Add(opencv
GIT_REPOSITORY https://github.com/opencv/opencv
GIT_TAG 68942affdbc4677aa845bc4307d4752182324a0e # 4.0.0-alpha
SOURCE_DIR opencv
BINARY_DIR opencv-build
CMAKE_ARGS
-DWITH_OPENGL=OFF
-DCMAKE_INSTALL_PREFIX=${OPENCV_INSTALL_LOCATION}
)
include_directories(${OPENCV_INSTALL_LOCATION}/include/opencv4)
link_directories(${OPENCV_INSTALL_LOCATION}/lib)
add_executable(cv_playground main.cpp)
add_dependencies(cv_playground opencv)
target_link_libraries(cv_playground Threads::Threads ${CMAKE_DL_LIBS} opencv_core opencv_dnn opencv_features2d opencv_flann opencv_highgui opencv_imgcodecs)
Error message (still get undefined reference to dlopen, dlclose, ...)
[ 90%] Linking CXX executable cv_playground
/home/syonekura/CLionProjects/cv_playground/cmake-build-debug/opencv/lib/libopencv_core.a(system.cpp.o): In function `cv::TLSData<cv::(anonymous namespace)::ThreadID>::createDataInstance() const':
system.cpp:(.text._ZNK2cv7TLSDataINS_12_GLOBAL__N_18ThreadIDEE18createDataInstanceEv+0x37): undefined reference to `__itt_thread_set_name_ptr__3_0'
/home/syonekura/CLionProjects/cv_playground/cmake-build-debug/opencv/lib/libopencv_core.a(opencl_core.cpp.o): In function `GetHandle(char const*)':
opencl_core.cpp:(.text._ZL9GetHandlePKc+0x7): undefined reference to `dlopen'
opencl_core.cpp:(.text._ZL9GetHandlePKc+0x1e): undefined reference to `dlsym'
opencl_core.cpp:(.text._ZL9GetHandlePKc+0x53): undefined reference to `dlclose'
/home/syonekura/CLionProjects/cv_playground/cmake-build-debug/opencv/lib/libopencv_core.a(opencl_core.cpp.o): In function `opencl_check_fn(int)':
opencl_core.cpp:(.text._ZL15opencl_check_fni+0x3f): undefined reference to `dlsym'
So I've had quite a few issues with this myself. Cmake should essentially be used just to build the OpenCV library. Provided you're using an ubuntu machine you want to do this:
# Update and upgrade packages
sudo apt-get -y update
sudo apt-get -y upgrade
# Remove any existing versions of x264
sudo apt-get remove x264 libx264-dev
# Install OS Libraries
# Install Dependencies
sudo apt-get -y install build-essential checkinstall cmake pkg-config yasm gfortran git
sudo apt-get -y install libjpeg8-dev libjasper-dev libpng12-dev
# Used for Ubuntu 16.04
sudo apt-get -y install libtiff5-dev
sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
sudo apt-get -y install libxine2-dev libv4l-dev
sudo apt-get -y install libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
sudo apt-get -y install libqt4-dev libgtk2.0-dev libtbb-dev
sudo apt-get -y install libatlas-base-dev
sudo apt-get -y install libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get -y install libvorbis-dev libxvidcore-dev
sudo apt-get -y install libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get -y install x264 v4l-utils
# Install Optional Dependencies
sudo apt-get -y install libprotobuf-dev protobuf-compiler
sudo apt-get -y install libgoogle-glog-dev libgflags-dev
sudo apt-get -y install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen
# Download OpenCV from Github
git clone https://github.com/opencv/opencv.git
cd opencv
git checkout 3.3.0
cd ..
# Download opencv_contrib from Github
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout 3.3.0
cd ..
# Compile and Install OpenCV with contrib
# Create build directory inside opencv directory
cd opencv
mkdir build
cd build
# Run CMake with the following options
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D WITH_TBB=ON \
-D WITH_V4L=ON \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
# Compile and Install
make -j4
sudo make install
sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
You can cherry pick the parts that are most relevant, but essentially what this entails is making the OpenCV library available to your project. You can then either use a make file to make your project, or you can run a command as so:
c++ pkg-config --cflags opencv filename.cpp pkg-config --libs opencv -o filename
This creates an executable called filename

How do I cross compile OpenCV for the Raspberry Pi with additional modules (highgui...)

My goal is to cross-compile OpenCV for Raspberry Pi with CMake on an Ubuntu 16.04 Host. The Problem lies within the CMake toolchain file which is unable to handle the compilation process correctly. As soon as pkg-config comes into play, a lot of "fatal error: No such file or directory" errors arise (e.g. "fatal error: gtk/gtk.h: No such file or directory).
I figured out that further "-isystem" flags with the directories containing the missing files seem to help in some way, but I doubt that this is the right way to address these errors.
The toolchain file works, as long as the "PKG_CONFIG"-stuff is commented out. This causes pkg-config not to find "libgtk2.0", which is needed for OpenCV's highgui module and builds without highgui-support.
Some ideas for the toolchain are from here:
Cmake cross compile cant find library
Raspberry Pi Beginners Guide
Cross compile opencv project on Ubuntu for Raspberry Pi
build-toolchain.sh
#!/bin/bash
# apt -y install lib32z1 libstdc++6:i386 git qemu-user proot cmake pkg-config
MOUNT_DIR=/mnt/rasp-pi-rootfs
TEMP_DIR=/tmp/build
RASPBIAN_IMG_ZIP=raspbian-jessie.zip
TOOLCHAIN=gcc-linaro-arm-linux-gnueabihf-raspbian-x64
CMAKE_TOOLCHAIN_FILE=rpi-toolchain.cmake
############################################################################
RASPBIAN_IMG=${TEMP_DIR}/$(unzip -Z1 $RASPBIAN_IMG_ZIP)
rm -rf $TEMP_DIR
mkdir $TEMP_DIR
cd $TEMP_DIR
#unzip raspbian-img
unzip $RASPBIAN_IMG_ZIP
#resize raspbian-img
LOOP_DEVICE=$(losetup -f)
RASPBIAN_PARTITION_BOOT=${LOOP_DEVICE}p1
RASPBIAN_PARTITION_SYSTEM=${LOOP_DEVICE}p2
truncate -s +500M $RASPBIAN_IMG
losetup $LOOP_DEVICE $RASPBIAN_IMG
parted $LOOP_DEVICE resizepart 2 100%
#wait until auto-mounted
umount $RASPBIAN_PARTITION_BOOT
umount $RASPBIAN_PARTITION_SYSTEM
e2fsck -f $RASPBIAN_PARTITION_SYSTEM
resize2fs $RASPBIAN_PARTITION_SYSTEM
#mount raspbian-img
mkdir -p $MOUNT_DIR
mount $RASPBIAN_PARTITION_SYSTEM $MOUNT_DIR
#fix absolute links
proot -q qemu-arm -S $MOUNT_DIR<<EOF
apt update
apt -y install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev \
libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
libxvidcore-dev libx264-dev \
libgtk2.0-dev libatlas-base-dev gfortran
apt -y install symlinks
symlinks -cr /usr/lib
EOF
wget -O opencv.zip https://codeload.github.com/Itseez/opencv/zip/2.4.13
unzip opencv.zip
cd opencv-*
mkdir build
cd build
cmake -D CMAKE_TOOLCHAIN_FILE=$CMAKE_TOOLCHAIN_FILE -D PIROOT=$MOUNT_DIR -D TOOLROOT=$TOOLCHAIN -D CMAKE_BUILD_TYPE=Release -D WITH_V4L=ON -D WITH_OPENGL=ON ..
make -j $(nproc)
make install
sync; umount $MOUNT_DIR
losetup -d $LOOP_DEVICE
rpi-toolchain.cmake
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_C_COMPILER ${TOOLROOT}/bin/arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER ${TOOLROOT}/bin/arm-linux-gnueabihf-g++)
set(CMAKE_SYSROOT ${PIROOT})
set(CMAKE_FIND_ROOT_PATH ${PIROOT})
unset(ENV{PKG_CONFIG_DIR})
set(ENV{PKG_CONFIG_LIBDIR} "$ENV{PKG_CONFIG_LIBDIR}:${PIROOT}/usr/lib/arm-linux-gnueabihf/pkgconfig:${PIROOT}/usr/share/pkgconfig:${PIROOT}/usr/lib/pkgconfig")
set(ENV{PKG_CONFIG_SYSROOT_DIR} "${PIROOT}")
set(FLAGS "-isystem ${PIROOT}/usr/include/arm-linux-gnueabihf")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FLAGS}" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLAGS}" CACHE STRING "" 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)
set(CMAKE_INSTALL_PREFIX ${PIROOT}/usr/local)
Got it. The Problem was the setting of PKG_CONFIG-vars within the rpi-toolchain.cmake file, this does not work .They have to be added to the environment with export:
...
export PKG_CONFIG_SYSROOT_DIR=${MOUNT_DIR}
export PKG_CONFIG_LIBDIR=${MOUNT_DIR}/usr/lib/arm-linux-gnueabihf/pkgconfig:${MOUNT_DIR}/usr/share/pkgconfig:${MOUNT_DIR}/usr/lib/pkgconfig
export PKG_CONFIG_DIR=
cmake -D CMAKE_TOOLCHAIN_FILE=$CMAKE_TOOLCHAIN_FILE -D PIROOT=$MOUNT_DIR -D TOOLROOT=$TOOLCHAIN -D CMAKE_BUILD_TYPE=Release -D WITH_V4L=ON -D WITH_OPENGL=ON ..
make -j $(nproc)
edit: I don't remember what the actual problem was here, but setting pkg-config variables with ENV{} works and was not the culprit.