How can I build a project with CMake? - build

I've downloaded a source of leptonica-1.74.4
I need lib, dll and *.h files for using with tesseract lib.
As I understood firstly I have to build this source with CMake and then I'll get VS files. (Or maybe lib and dll???)
I've never work with CMake. Have no idea how to run CMakeLists or whatever through CMake. What should I do?
I was trying to read documentation and it just made me confused.
OS Windows 8.

CMake ist not a build system but manages the build process within your native build environment - in your case (Win8 + VS) it'll create the project and solution files you can use in VisualStudio.
For your specific case it will be best to
Download, install and run CMake-GUI
Specify the source folder(where CMakeLists.txt is located)
Specify the build folder(where the libs / executables shall be build)
Press "Configure" - you will be asked for the generator you want to use - ideally you choose the VS version you have installed in your system.
Press "Generate" - cmake generates the .vcxproj and .sln files in your build folder corresponding to the VS version you have chosen.
Open the .sln file and start building leptonica or integrate the project into your own solution.
In addition - CMake allows you to directly trigger the build with your native compiler. But this needs to be done via the console.
more information here

Related

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

Compile GLEW with CMake and Visual Studio 2019?

I tried to compile (build) GLEW source code with CMake and Visual Studio 2019 for get binaries. In directory (disk) C:\ I created folder GL and inside it, placed unzipped glew-2.1.0. In C:\GL\, I created the folder build. Because I found the file "CMakeLists.txt" in 3 folders,
C:\GL\glew-2.1.0\build\conan\test_package
C:\GL\glew-2.1.0\build\cmake
C:\GL\glew-2.1.0\build\cmake\testbuild
I tested all 3 paths in "Browse Source..." text box in CMake. In "Browse Build..." text box I pasted C:/GL/build.
In all 3 tests CMake responded:
"CMake Error: The source
"C:/GL/glew-2.1.0/build/cmake/CMakeLists.txt"
does not match the source
"C:/GL/glew-2.1.0/build/cmake/testbuild/CMakeLists.txt"
used to generate cache. Re-run cmake with a different source directory."
that is "file CMakeLists.txt in 2nd path does not match file of same name in 3rd path".
If you change the top-level source directory on which CMake runs, you should first delete your CMake cache (File > Delete Cache in CMake GUI), and run CMake from scratch to avoid these errors.
Have you tried the CMake instructions on the GLEW Github here? Try running these commands from the command line:
> cd C:/GL/glew-2.1.0/build
> cmake ./cmake
Though this deviates from the typical out-of-source build CMake idiom, this is how GLEW intends the build system be created. So on your system:
Source directory: C:/GL/glew-2.1.0/build/cmake
Build directory: C:/GL/glew-2.1.0/build
Note, the GLEW build instructions warn about the use of CMake for building this repo:
The cmake build is mostly contributer maintained. Due to the multitude of use cases this is maintained on a best effort basis. Pull requests are welcome.
Thus, you may have better luck following the typical Windows build approaches recommended here.
According to Mr. squareskittles answer-comment to my comment to his answer, that I have tested successfully, correct answer is that the folder to accommodate binaries should be C:/GL/glew-2.1.0/build or any folder you have created in glew-2.1.0. It is peculiarity of glew though. With GLFW you can create accommodating binaries folder outside it. However according to Mr.squareskittles's edit in his answer which I have tested successfully, if you delete Cashe (in CMake GUI, click File > Delete Cashe) you can create accommodating binaries folder outside glew-2.1.0. Regards.

"CMakeLists.txt" seems to be missing

I was trying to build SFML in order to start developing 2d-games in c++. I put the source-code directory inside C: (the directory itself is called 'SFML'). while trying to generate .cbp files using CMake, I ran into an error:
"The source directory "C:/SFML" does not appear to contain CMakeLists.txt"
It seems like there is a missing file that CMake didn't create.
I am using Cmake 3.10.1, the installation file can be found in https://cmake.org/download/ under the title:"Windows win32-x86 Installer".
I tried to build SFML 2.4.2, using the generator of Codeblocks - MinGW makefiles. The version of codeblocks used is: codeblocks-16.01mingw-setup.exe, found in http://www.codeblocks.org/downloads/26
Did someone encounter the same problem before and knows how to overcome it? If not -
Is there an already-built, ready to use, libray of this version? (2.4.2)
If not.. do you know about a version of SFML that is usable in visual studio 2017?
And another question (That is the last one, I promise...) - maybe you can recommend me about another library for c++, that is usable to develop 2d-games?
edt:
note: I am completely new to all this building thing
the containments of SFML:
maybe it's the binary version?
lib contains some static libraries, bin contains some .dll s
You probably downloaded something wrong or potentially extracted the SFML source into a sub folder or something like that.
Since you've mentioned Visual Studio: SFML is 100% compatible with Visual Studio and you can build it from source any time without having to worry about dependencies (everything included in the repository).
The steps to compile are pretty easy. I'd recommend you install Git to clone the latest source from the SFML repository. In addition you'll need CMake, which you obviously got already.
Open a Command Prompt and change your directory to C:\SFML.
Clone the official repository using Git: git clone https://github.com/SFML/SFML.git source (note the trailing .)
Wait for the source to be checked out to the source sub directory.
Create a new directory called build: md build && cd build
Now invoke CMake, pointing to the source directory and also defining your install path: cmake -DCMAKE_INSTALL_PREFIX=C:/SFML ../source
Build and install a debug build of SFML: cmake --build . --target install --config debug
Build and install a release build of SFML: cmake --build . --target install --config release
Alternatively you can just open the generated Visual Studio solution and build the INSTALL project on your own.
(You shouldn't have to define a build tool or anything; CMake should notice Visual Studio being installed and pick it up automatically.)
I have experienced this using cmake version 3.10 when using the -S command line option. On cmake version 3.15.4 it behaves as expected - so if you update cmake then it might just work.
Note that the -S command line option was not supported before version 3.13:
https://cmake.org/cmake/help/v3.13/release/3.13.html#command-line
Normally cmake is called from a "build" subdirectory of a project root, the project root contains CMakelists.txt. So you would create a "build" directory, change to it, and submit "cmake ..". This would pull in CMakelists.txt from the project root, and use the "build" subdirectory that you are in as the build directory. We say "build", what it actually does is generate makefiles which can then used by the "native" build system (e.g. "make" or Visual Studio) to perform the actual compilation and linking. This arrangement is not always convenient, hence the introduction of the -S and -B command line options.

OpenCV made by Cmake does not contain any libaries

I am trying to setup OpenCV for Visual Studio 2015, x64, in windows.
I have followed this guide to get OpenCV up and running, however after the CMake Part, i am not sure if everything has gone well.
1- my build file does not contain any lib folder
2- i am confused as where should i set the environment variable for it
3- I assume in visual studio i have to set path for the libraries, but i
don't see any in my build
4- my include folder has a weird install file
pic1: here is a picture of my OpenCV file that cMake built
bin folder is empty
pic2:include folder contents
CMake does not build your library. It sets your build environment, such as designating the install directory for the OpenCV.
So you need to actually build your OpenCV library, then you'll have your include and lib files at the place you've designated as the install directory (cmake_install_prefix in the CMake option).
If you've done your CMake step successfully, you can build your OpenCV by building ALL_BUILD project in your Visual Studio project explorer, and building INSTALL project after that.

Where is the lib folder (or its replacement) in the current OpenCV?

I'm following a book written for the older version of OpenCV (OpenCV 2 Computer Vision, by PACT) and it tells me to include the lib folder in my Visual Studio 2013 Property Manager when creating a new property sheet.
I don't see a directory called lib in the current GitHub version (opencv-master, which is OpenCV 3.1.x). Has this folder been replaced by something else?
I built OpenCV with cmake. I found a 4 year old unanswered question wherein someone was also looking for this folder. They tried building the library from another directory that no longer exists, but that didn't work for them anyway...
Another OpenCV user just told me that GitHub doesn't include the libraries, so you have to cmake them locally. I'm still not clear on where / how I can cmake them.
I realize the pre-built binaries have this, but I'm avoiding them because I need the SURF functions in opencv_contrib, so I needed to build it from source.
The pre-built binaries will have a library folder in the corresponding path
Local System Path(Opencv Folder)-> build->x64/x86->vc10/vc11/vc12->lib.
As you mentioned that you don't wish to use it then the only option left for you is to build it locally which is a much better option if you plan to use Opencv libraries for varied functions and projects as it resolves many build errors that you might face later.
I used the Cmake Graphical user interface to build opencv, following are the steps I followed to successfully build the libraries on my system .
So, you would need to create a new folder that will contain all
the Makefiles generated.
Please refer to this image for clearer understanding:
In GUI you define source directory path where OpenCVConfig.cmake is present, according to your image it's the
current folder in your image opencv-master.
Similarly, define the path to the new directory you created where all the build files would be stored.
Make sure to uncheck Build_Examples to avoid configuration errors.
Then click Configure at the bottom when configuring is done .(you may need to configure it twice)
After click the tab adjacent to configure, Generate to create the solution file.It will ask you for the compiler name
select the
compiler installed on your system from the list of choices. After
generation is done.
Go to this path Build_New_Directory(the directory you created) you will find OpenCV.sln build this project, it will take around 10-15 minutes depending on your processor, wait patiently .If you get a build error at this point don't invest your time in debugging on Visual Studio go back to Cmake GUi and configure it again and this time give the path to dependent libraries on your system that it could not find .Repeat the process it should be successfully build now.
After it is successfully build you can now locate the path of all opencv libraries build on your system as follows.
Build_New_Directory(the directory you created)->
install->x64->vc10(compiler I used)->lib.
As an update to Nikita's awnser:
There is a cmake build bug where the x64 folder will not be created if OpenCV_RUNTIME is not set. This will happen if you build an old OpenCV (e.g. <= 3.2.0) with a newer Visual Studio Version than was available at that time (e.g. Visual Studio 2017)
To fix this, add the correct MSVC_VERSION elseif-cases in both ./cmake/OpenCVDetectCXXCompiler.cmake and the ./cmake/templates/OpenCVConfig.root-WIN32.cmake.in (or ./cmake/OpenCVConfig.cmake in < v3.2.0) files:
[...]
elseif(MSVC_VERSION EQUAL 1900)
set(OpenCV_RUNTIME vc14)
# old version ends here with endif()
elseif(MSVC_VERSION GREATER 1909 AND MSVC_VERSION LESS 1920)
set(OpenCV_RUNTIME vc15)
elseif(MSVC_VERSION GREATER 1919 AND MSVC_VERSION LESS 1930)
set(OpenCV_RUNTIME vc16)
endif()
[...]