Visual Studio's CMake with vcpkg: Error gdal is not found - c++

I have been trying to build a cmake c++ project. More specifically I am trying to use the gdal library in this project. In the CMakeLists.txt it says find_library(GDAL gdal) after doing some research i found, that visual studio can open cmake files by default as mention in this thread: https://learn.microsoft.com/en-us/cpp/build/cmake-projects-in-visual-studio?view=vs-2019.
Moreover, visual studio should also automatically include the gdal library once i have set it up with vcpkg correctly. I've already downloaded the x64-windows version of the library (vcpkg install gdal:x64-windows) in order to build for the right architecture and made it available via vcpkg integrate install on a user-wide scope.
After some trial and error, everything works fine now, the toolchain gets included accordingly and the library is found automatically, resulting in a configuration like that:
However, when trying to include the header files (or anything else; see code snippet), visual studio does not seem to link the library correctly as it will result in the error message: cannot open source file "gdal/gdal.h".
#include <gdal/ogrsf_frmts.h>
#include <gdal/gdal.h>
#include <gdal>
Where should I further investigate?

As others have said vcpkg integrate install and vcpkg.cmake don't work together the reason being:
set_target_properties(${name} PROPERTIES VS_USER_PROPS do_not_import_user.props)
set_target_properties(${name} PROPERTIES VS_GLOBAL_VcpkgEnabled false)
this deactivates the integration. The reason to deactivate the integration is so that you don't write an incomplete CMakeLists.txt (e.g. missing the include directory or not linking all required libraries).
As such replace find_library(GDAL gdal) with find_package(GDAL REQUIRED) and target_link_libraries against the target GDAL::GDAL (https://cmake.org/cmake/help/v3.17/module/FindGDAL.html)

Related

C++, setting up include with Cmake

I want to use the ICU package and I tried installing with and without vcpkg, and with vcpkg I do not get a directory for where it was installed like I see in tutorials, so I had to manually find it nor did I get a Cmake toolchain paste line from installing with vcpkg. As for the Cmake file, I try following this tutorial
https://www.youtube.com/watch?v=FeBzSYiWkEU
but when I go to the configuration manager in Visual Studio I am brought to a json file with no option to change the toolchain, and when I try to go to the settings json file in Visual Studio Code instead, I instead find three different settings json files, none of which have the toolchain file option to change like with Visual Studio. I have had similar difficulties with the Cmake file when trying to install other packages for C++ and have no idea what I am missing from tutorials that seems to no matter what have the Cmakefile.txt never work for me.
I would like to just be able to use the ICU package without having to use Visual Studio and just be able to statically link the package with my .cpp file in the same folder that VS Code uses, and just g++ compile from cmd, however I am in over my head every time I try to get the CMake file to work. If all I know for sure is that I have the latest ICU package downloaded with vcpkg, how would I go about setting up the CMake file?
cmake_minimum_required(VERSION 3.24)
project(MyICUProject)
find_package(ICU REQUIRED)
add_library(MyLibUsingICU lib.cpp)
target_link_libraries(MyLibUsingICU PRIVATE ICU::i18n) # or whatever you use from icu. check https://cmake.org/cmake/help/latest/module/FindICU.html
have a manifest (vcpkg.json) along the sources
{
"name": "MyICUProject",
"description": "<description>",
"dependencies": ["icu"]
}
Invoke with (if you are in the source dir):
cmake -G "Ninja" -B "build" -S . -DCMAKE_TOOLCHAIN_FILE=<path_to_vcpkg_toolchain>/vcpkg.cmake

Compiling and Linking to Visual Studio 2022 using OpenCV source code built as Win32 from CMake C++

I'm trying to use OpenCV with Dear ImGui in Visual Studio 2022. I'm new to C/C++ libraries and building in general, so I'm unsure if I'm doing anything right. ImGui uses 32-bit architecture and I've used Cmake gui to compile the source code as Win32. I think I have the compiled source code, but it seems to be different than downloading the pre-built libraries. File Explorer Screenshot. I've added the bin to PATH environmental variable, and in Visual Studio tried adding \include to Include Directories, \lib or \lib\Debug to Library Directories, and opencv_world460d.lib to Additional Dependencies. The program still runs, but it doesn't seem to include anything related to OpenCV in the #include files. I found a few .dll files in bin\Debug, but I'm not sure if I should bother with that. I think I could move the source code into the project, but I'm fairly certain that isn't the proper way to do it. Any help would be appreciated.
I needed to run the install target:
You may have built the project, but probably you didn't run the install target. Try running cmake --build <build_dir> --config Release and then cmake --install <build_dir> --config Release, where <build_dir> is a placeholder for the path to the build dir shown in the screenshot. The latter command probably requires admin privileges. Probably best to check the docs of the lib, if there's a step by step instruction for building & installing the whole thing. –
fabian

Using aws-iot-device-sdk-cpp-v2 with CMake

I'm attempting to build an application using the AWS IOT device sdk for C++.
I was able to clone, build and install the library with CMake and Visual Studio. Now I'm trying to include the IotShadow library into my test application through CMake.
I've included the package in my CMakeLists.txt file as follows and included the path the library install through the CMAKE_PREFIX_PATH variable.
find_package(IotShadow-cpp REQUIRED)
target_link_libraries(TestApp IotShadow-cpp)
CMake runs fine and things seem to be setup. However when I attempt to build, the IotShadow header files are not found. I've confirmed that IotShadow-cpp_DIR is correct and that the header files do exist. I'm not sure where to go from here. Has anyone successfully used the SDK on windows?
Looks like the target for find_package is correct but the target for target_link_libraries needs to be AWS::IotShadow-cpp.
The following seemed to work for me:
find_package(IotShadow-cpp REQUIRED)
target_link_libraries(TestApp AWS::IotShadow-cpp)

Using libraries in a CMake project in Visual Studio 2017

I am working on a small DLL based on an existing cmake project. Now I want to additionally use the libjpeg-library. The libjpeg-project compiles correctly and I select to install the library. When I return to my main project and try to use the library.
My problem is that cmake can't find the libjpeg library. When I try to have CMake check for the library with this command:
include (${CMAKE_ROOT}/Modules/FindJPEG.cmake)
It returns:
Could NOT find JPEG (missing: JPEG_LIBRARY)
How can I force CMake/Visual Studio to find the libary? Just for fun, I copied the lib-file in every single folder of the CMake project with the same result...

Using GDCM via CMake

I downloaded source of GDCM 2.4.1 and used CMake 2.8.12.1 and Visual Studio 2010 to build the libraries. I want to use GDCM in my C++ project.
Unfortunately, it seems that after building the GDCM solution with VS, there are only the lib-files in the bin folder, but no typical "include" folder with the header files, what I usually expect. Thus, I can't integrate GDCM libaries in my own project (I tried to use FindPackage(GDCM) in my own CMakeproject, but header files can't be found).
I do not want to copy the header files manually or target the source directory of GDCM.
Does anybody know help?
I found out that the problem has something to do with VTK, I also use the this library in my project. So I need to enable "GDCM_USE_VTK" in CMake of GDCM. That option leads to a compiler error when I try to build the "INSTALL" subproject in the GDCM solution:
CMake Error at cmake_install.cmake:31 (FILE): file INSTALL cannot find "D:/Libs/VTK_5.6.0/BIN/bin/vtkCommon.dll".
I took a look in the VTK directory and found out that the path mentioned above does not exists. Instead the dll is located in:
D:\Libs\VTK_5.6.0\BIN\bin\Release\vtkCommon.dll or
D:\Libs\VTK_5.6.0\BIN\bin\Debug\vtkCommon.dll
That means GDCM solution does not know the dll is located in a special debug or release folder. If I disable "GDCM_USE_VTK" everything works fine and all files will be copied to the target folder. But I do need the VTK dll.
Any thoughts?
Michael
For anyone who ever touches the same problem, here's the final solution. Build "ALL_BUILD" in the GDCM solution, then integrate GDCM libs in your CMakeLists.txt the following way:
FIND_PACKAGE(GDCM REQUIRED)
IF(GDCM_FOUND)
INCLUDE(${GDCM_USE_FILE})
SET(GDCM_LIBRARIES
gdcmcharls
gdcmCommon
gdcmDICT
gdcmDSED
gdcmexpat
gdcmgetopt
gdcmIOD
gdcmjpeg12
gdcmjpeg16
gdcmjpeg8
gdcmMEXD
gdcmMSFF
gdcmopenjpeg
gdcmzlib
socketxx
vtkgdcm
Rpcrt4)
ELSE(GDCM_FOUND)
MESSAGE(FATAL_ERROR "Cannot find GDCM, did you set GDCM_DIR?")
ENDIF(GDCM_FOUND)