I'm trying to use external libraries (specifically opencv) with C++ but I'm having some trouble. It seems like using vcpkg is the easiest way to go about this. So far I've followed these tutorials: https://www.youtube.com/watch?v=b7SdgK7Y510 and https://www.youtube.com/watch?v=iZeK3Ie5Fz0 but I keep getting these errors when I try to include a file:
cannot open source file "opencv2/core.hpp"
'opencv2/core.hpp' file not found
The quick fix provided by vscode says to install opencv through vcpkg but I've already done that. I've linked vcpkg according to the tutorials and included the path to the cmake toolchain file in my settings.json file.
Here is the change I made to my settings.json file:
{
"cmake.configureSettings": {
"CMAKE_TOOLCHAIN_FILE": "/Users/oliverpasquesi/coding/dev/vcpkg/scripts/buildsystems/vcpkg.cmake"
}
}
Here is the CMakeLists.txt file (it is the same as the one from the 2nd tutorial listed):
cmake_minimum_required(VERSION 3.0.0)
project(photoText.cpp VERSION 0.1.0)
add_executable(photoText.cpp main.cpp)
Here is the includePath portion of the c_cpp_properties.json file (including the 2nd path doesn't make any difference in the errors being thrown):
"includePath": [
"${default}",
"/Users/oliverpasquesi/coding/dev/vcpkg/installed/x64-osx/include/opencv2/**"
]
If it's useful, I'm using vscode 1.49.2 on a mac running Darwin x64 21.5.0.
I appreciate the help!
You need to reference your library in the CMakeLists.txt file as well, since that's the file CMake uses to prepare your project for a build.
A modern way to do this is to add two lines:
A find_package() line to find the libraries you are using. As you already referenced the vcpkg.cmake toolchain file, CMake should be able to find any libraries already installed by vcpkg when you add that line. See https://cmake.org/cmake/help/latest/command/find_package.html for documentation.
A target_link_libraries() line that lists libraries to link against your project. See the CMake documentation here: https://cmake.org/cmake/help/latest/command/target_link_libraries.html
As you are using the opencv library, you may want to look at the answers to this question as well: Linking Opencv in a project using cmake
Specifically, try adding these lines to your CMakeLists.txt:
find_package(OpenCV REQUIRED)
target_link_libraries(photoText.cpp ${OpenCV_LIBS})
vcpkg documentation on its CMake integration (for additional reference): https://vcpkg.io/en/docs/users/buildsystems/cmake-integration.html
Hopefully this helps!
Disclaimer: I work on the vcpkg tool.
Related
I'm using CLion as my IDE for C++ development and I'm trying to get Eigen included.
How do I do this?
I've downloaded and unzipped Eigen and placed it in C:/ (Which I've read online is the path where CMake looks for libs when you use find_library())
In the CMakeLists.txt I've added
find_library(Eigen3 3.4 REQUIRED NO_MODULE)
add_executable(Lecture03 main.cpp)
target_link_libraries (Lecture03 Eigen3::Eigen)
But then it can't find Eigen, I get the following error when reloading my CMakeLists:
CMake Error at CMakeLists.txt:6 (find_library):
Could not find Eigen3 using the following names: 3.4
My question is, what did I do wrong? Did I place the Eigen folder in the wrong directory? Can I change where CMake looks for libs in CLion?
Eigen (https://eigen.tuxfamily.org/index.php?title=Main_Page) is a template library. That means it is header only, there is nothing to link against.
For CMake that means you can use find_path to find the header file
The solution ended up being to use
include_directories(C:/CPP_Libs/Eigen3)
in my CMakeLists.txt, and
#include <Eigen/Dense>
in whichever file needs it
I currently have libsodium installed and working, i.e. If I run
gcc -lsodium foo.c -o foo from my command line, the compiler successfully compiles and links the executable. However, I don't know how to include libsodium in my CMakeLists.txt file and although I read the documentation, I don't know how to follow the instructions. Verbatim, the instructions from the docs are:
"Projects using CMake can include the Findsodium.cmake file from the Facebook Fizz project in order to detect and link the library."
I found the Findsodium.cmake file on github (https://github.com/facebookincubator/fizz/blob/main/build/fbcode_builder/CMake/FindSodium.cmake) but I don't know how to "include" it in my CMakeLists.txt and I haven't found any help anywhere on previous stackoverflow questions or on the docs anywhere.
The line in my CMakeLists.txt file which is throwing the error is:
find_package(sodium REQUIRED) and the error is as follows:
CMake Error at CMakeLists.txt:5 (find_package):By not providing "Findsodium.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "sodium", but
CMake did not find one.
Could not find a package configuration file provided by "sodium" with any
of the following names:
sodiumConfig.cmake
sodium-config.cmake
Add the installation prefix of "sodium" to CMAKE_PREFIX_PATH or set
"sodium_DIR" to a directory containing one of the above files. If "sodium"
provides a separate development package or SDK, be sure it has been
installed.
Any help on fixing this error and successfully helping me include libsodium in my project would be much appreciated. I'm relatively new to CMake so forgive the ignorance if this has an obvious fix. If you answer or attempt to answer this question, thank you for your time.
You need to install pkg-config on any Unix like system such as Linux. Sodium does not properly support detection via cmake without pkg-config to identify it's library and include paths.
On Debian or Ubuntu:
apt install pkg-config
You can see the checks looking for this on line 52 of FindSodium.cmake:
https://github.com/facebookincubator/fizz/blob/a009078d01f476c19a05d3ebc74055a64563d33a/build/fbcode_builder/CMake/FindSodium.cmake#L52
if (UNIX)
# import pkg-config
find_package(PkgConfig QUIET)
if (PKG_CONFIG_FOUND)
pkg_check_modules(sodium_PKG QUIET libsodium)
endif()
i successfully built ITK 4.13 (as 'Release') on Windows 10 with the latest Visual Studio version (Visual Studio 15 2017 Win64 compiler). Then i tried to built the ITKHelloWorld example using the following CMakeLists.txt (as shown in the example by ITK):
#CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(HelloWorld)
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
add_executable(HelloWorld HelloWorld.cxx)
target_link_libraries(HelloWorld ${ITK_LIBRARIES})
Built runs without errors or warnings. Running the generated HelloWorld.exe file result in an ITKCommon-4.13.dll not found error.
The ITKCommon-4.13.dll was built by ITK, thus, i think i have to change something in the CMakeList file, but don't know what.
Any guess or solution?
Edit: The include(${ITK_USE_FILE}) expression in CMakeLists searches for UseITK.cmake, but this file is located in the source of ITK not in the build folder. Could this be a source of the error?
Edit2: Running a VTK Example also shows errors caused by not finding required .dlls.
There is no problem with your project. Windows can use dll libraries on runtime only if they are in PATH location. So there are two options: either you add bin/Release directory of ITK library to Windows's PATH system variable (the directory that contains all the ddls), or you just copy all the dlls from that directory to the one that contains your new application.
Precisely, you don't need to copy all the dlls, you can check by e.g. Dependency Walker, which ones are really needed.
I am using OpenCV with gcc and cmake. And I found a tutorial https://docs.opencv.org/3.4.0/db/df5/tutorial_linux_gcc_cmake.html .In the file CMakeLists.txt, there are some variables such as OpenCV and OpenCV_INCLUDE_DIRS.
cmake_minimum_required(VERSION 3.9)
project(VideoRecord)
set(CMAKE_CXX_STANDARD 11)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(VideoRecord main.cpp)
target_link_libraries(VideoRecord ${OpenCV_LIBS})
I want to know where to find these variables definition.
EDIT
Thanks #qbranchmaster's answer. I tried to search FindOpenCV.cmake but failed.
First try.
➜ ~ cmake --help-module-list | grep "FindOpen"
FindOpenACC
FindOpenAL
FindOpenCL
FindOpenGL
FindOpenMP
FindOpenSSL
FindOpenSceneGraph
FindOpenThreads
Another try.
➜ / find . "FindOpenCV.cmake"
In addition, my os is osx and I install cmake with brew. I comiple and install OpenCV manually.
These variables are part of the package config script shipping with OpenCV.
Note that find_package is a two-headed beast. The classic mode of operation is finding libraries through find-scripts. This is still the approach being used today for third-party libraries that are not aware of CMake. However, if your dependency is itself being built with CMake, it can provide a package config file instead, which allows for a more powerful mode of operation.
The idea here is that instead of you telling CMake how to find a dependency, the dependency itself tells CMake how clients can find it. This is the approach that is taken by libraries like OpenCV and Qt.
To answer your question, those variables are being set by the package config file in your local OpenCV installation, the template of which can be found in the OpenCV source code under cmake/templates/OpenCVConfig.cmake.in.
They are defined in CMake OpenCV module. CMake has numerous modules that aid in finding various libraries like OpenCV (FindOpenCV.cmake module).
Using this command you can get a list of modules that your CMake supports:
cmake --help-module-list
Some libraries come with their own *.cmake modules which should be installed in some system path. If you are using Ubuntu, your cmake modules should be localised in:
/usr/share/cmake/Modules/
If not, just search system for file FindOpenCV.cmake. In that file you will find these variables.
In general, you get variable names from the documentation or source code of the package you want to find.
Often you can derive the name to put into find_package from the provided FindFoo.cmake module file name, because "Foo" would be the name. The find module is either part of CMake or comes with the third-party library.
If there is no find module, some modules provide FooConfig.cmake files, where "Foo" is again the string to put into find_package.
If you have neither a find nor a config file, you need to find the library by other means, e.g., FindPkgConfig or find_library / find_file.
I was using Boost 1.54.0 and it was located in "/usr/include". We blew that away and installed Boost 1.57.0. It got installed in "/usr/local/include".
Now, my CLion project, which uses CMake cannot find the Boost library. Here is my CMakeLists.txt file:
And here are my errors:
I have no idea how to make CMake look in the correct place for Boost.
According to the FindBoost documentation (http://www.cmake.org/cmake/help/v3.1/module/FindBoost.html), you can set a CMake variable BOOST_ROOT to give CMake a hint about where to look.
In your CMakeLists.txt file, you can add the following before the find_package(Boost...) line:
set(BOOST_ROOT /usr/local)
Update:
I agree with the comments that putting machine specific configuration parameters directly in CMakeLists.txt is not best practice.
As an alternative to directly setting this variable, you can pass options like this to the cmake process in CLion by doing the following:
Navigate to File -> Settings... -> Build, Execution, and Deployment -> CMake. Under Generation, add -DBOOST_ROOT=/usr/local to CMake options.