why can CMake Build files not be generated correctly - c++

I am trying a simple test to see if CMake is working on my windows system correctly.
I keep getting a error.
Here is the command with the error.
cmake .
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19044.
-- Configuring done
CMake Error at CMakeLists.txt:5 (add_executable):
No SOURCES given to target: main.cpp
CMake Generate step failed. Build files cannot be regenerated correctly.
code for file named main.cpp
#include <iostream>
int main()
{
std::cout << "hello world\n" << "this is a test" <<std::endl;
}
and my CMake file
cmake_minimum_required(VERSION 3.10)
project(test)
add_executable(main.cpp)
I have used CMake in Linux before so not sure why this is failed.
I used Microsoft package manager to install it.
I am using the command line for this, i tried the GUI it also failed.
I have also deleted the CMake files and cache multiple times.
I have not been able to find anything online.

Can't add a comment since my reputation is too low, so I will write an answer instead. In the last line of your CMakeLists.txt file
add_executable(main.cpp)
you are missing the name of the executable
add_executable(name_exe main.cpp)
CMake is telling you that in the error message. CMake tries to create a target main.cpp without source files, since CMake suggests the name of the executable at the first place in the command add_executable().

Related

Installing package via vcpkg causing a "opencv2/opencv.hpp: No such file or directory" error

I have installed vcpkg and opencv using vcpkg to try import the opencv library in c++ and I keep getting the following error:
main.cpp:2:10: fatal error: opencv2/opencv.hpp: No such file or directory
2 | #include <opencv2/opencv.hpp>
This is my simple main.cpp:
#include <iostream>
#include <opencv2/opencv.hpp> //I have also tried using "" instead
using namespace cv; //I have tried not using this line
int main(int, char**) {
std::cout << "Hello, world!\n";
}
I have tested to see whether this was a problem with opencv, but when I installed and tried importing the booster library via vcpkg, the same issue persists.
I have a windows 10, and I am currently using VScode as my main editor, although I don't think it's a VScode problem because it doesn't matter whether use the run package in atom, vscode, or windows terminal, cygwin, or powershell the same error pops up.
I have tried many things including uninstalling and reinstalling vcpkg, adding the library, and bin files to the path in system environment variables, reinstalling everything in my C: drive instead of my D: drive, using cmake and even changing the #include statement to the following variations:
#include <opencv2>
#include <opencv>
However, I noted when I used the following import statement instead:
#include <C:\Users\nick-\DEV\opencv\build\include\opencv2\opencv.hpp>
I got this error:
C:\Users\nick-\DEV\opencv\build\include\opencv2\opencv.hpp:48:10: fatal error: opencv2/opencv_modules.hpp: No such file or directory
48 | #include "opencv2/opencv_modules.hpp"
Which originated in one of the files of the opencv library.
I'm using vcpkg because I had previously tried using cmake and the issue wasn't resolved, the other StackOverflow link is [here][1] in case it's somewhat helpful. (I tried what was suggested and I found and moved the opencv library to the right address and it didn't help)
EDIT: Minimal reproducible example as per #Alan Birtles's suggestion,
These are the steps that I did:
Install vcpkg by git cloning the [vcpkg repo][2]
Run the bootstrap command (.\vcpkg\bootstrap-vcpkg.bat)
installed opencv via vcpkg (vcpkg install opencv)
integrate vcpkg packages globally (vcpkg integrate install)
Created a main file (main.cpp) which you see above, I have made slight edits to it to make it minimal.
Then I compiled main.cpp (g++ main.cpp -o main) and got the error
Then I tried the above troubleshooting steps (as well as tons of others I can't remember) and remained with the issue at hand.
EDIT: Attempted reincorporating cmake
Following #Genjutsu's suggestion in the comment, I have done the following steps to re-setup cmake and get it working (it hasn't worked)
Installed the cmake, cmake tools, and cmake integration extensions on vscode
Run "cmake configure" as an executable
Added a handful of lines to my CMakeLists.txt file which I have shown below
cmake_minimum_required(VERSION 3.0.0)
project(fpc VERSION 0.1.0)
include(CTest)
enable_testing()
set(CMAKE_TOOLCHAIN_FILE "C:/Users/nick-/DEV/vcpkg/scripts/buildsystems/vcpkg.cmake")
set(CMAKE_PREFIX_PATH "C:/Users/nick-/DEV/vcpkg/installed/x64-windows")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(fpc main.cpp)
target_link_libraries(fpc ${OpenCV_LIBS})
message("OpenCV_INCLUDE_DIRS: " ${OpenCV_INCLUDE_DIRS})
message("OpenCV_LIBS: " ${OpenCV_LIBS})
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
Which give me the following output:
[main] Configuring folder: floorplan pathfinder cpp
[cmake] Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19043.
[cmake] OpenCV_INCLUDE_DIRS: C:/Users/nick-/DEV/opencv/build/include
[cmake] OpenCV_LIBS: opencv_calib3dopencv_coreopencv_dnnopencv_features2dopencv_flannopencv_gapiopencv_highguiopencv_imgcodecsopencv_imgprocopencv_mlopencv_objdetectopencv_photoopencv_stitchingopencv_videoopencv_videoioopencv_world
[cmake] Configuring done
[cmake] Generating done
And finally I performed the suggested actions to use vcpkg with cmake under "Visual Studio Code with CMake Tools", and set my workplace's settings.json to this:
{
"cmake.configureSettings": {
"CMAKE_TOOLCHAIN_FILE": "D:/Program Files/vcpkg/scripts/buildsystems/vcpkg.cmake"
}
}
EDIT: Added a manifest file (and also edited the cmake file)
So I added a vcpkg.json manifest file to the best of my abilities, which is shown below. And modified my CMakeLists.txt file with changes reflected above.
{
"name": "example",
"version-string": "0.0.1",
"dependencies": [
"OpenCV"
]
}
Simply said:
a) VS Code is not affect by vcpkg integrate install, only MSBuild is ...
b) setting the CMAKE_TOOLCHAIN_FILE only works before the very first project() call
c) setting CMAKE_TOOLCHAIN_FILE before project() via cmake.configureSettings or other means does not work if there is already a configured cmake cache.
d) don't modify CMAKE_PREFIX_PATH
So starting from a clean cmake build without any cache: Set CMAKE_TOOLCHAIN_FILE before project() by any means you want and it works. You might also need to set VCPKG_TARGET_TRIPLET.
(using #include <opencv2/opencv.hpp> seems fine)

How to print variables in CMake?

I want to use cmake to build my project, my CMakeLists.txt is very simple:
cmake_minimum_required(VERSION 3.20)
project(YuvParallelProcess)
message("$(PROJECT_SOURCE_DIR)")
include_directories($(PROJECT_SOURCE_DIR)/inc)
link_directories($(PROJECT_SOURCE_DIR)/lib/libyuv)
add_executable(YuvParallelProcess $(PROJECT_SOURCE_DIR)/src/main.cpp)
target_link_libraries(YuvParallelProcess libyuv.a)
my project files are organized as follows:
YuvParallelProcess
build
data
inc
lib
src
CMakeLists.txt
when I run cmake .. in build directory, the shell prints:
$(PROJECT_SOURCE_DIR)
-- Configuring done
CMake Error at CMakeLists.txt:10 (add_executable):
Cannot find source file:
$(PROJECT_SOURCE_DIR)/src/main.cpp
CMake Error at CMakeLists.txt:10 (add_executable):
No SOURCES given to target: YuvParallelProcess
I don't know why the shell just prints $(PROJECT_SOURCE_DIR) other than the value of PROJECT_SOURCE_DIR
How can I print the value of PROJECT_SOURCE_DIR
By the way, I can't figure out why cmake failed to compile my project
Cmake uses curly braces for getting values of variables:
${PROJECT_SOURCE_DIR}
I suggest to dump messages like below to not skip empty messages.
message(PROJECT_SOURCE_DIR="${PROJECT_SOURCE_DIR}")
Cmake failed to configure your project because it did not recognise variables in parentheses and considered $(PROJECT_SOURCE_DIR) to be a string literal as is.
There is CMakePrintHelpers Cmake module for dumping variables exactly like above.
include(CMakePrintHelpers)
cmake_print_variables(PROJECT_SOURCE_DIR)
Instead of using message, you can also use cmake_print_variables to display variables.
in this case, you must include CMakePrintHelpers
include(CMakePrintHelpers)
cmake_print_variables(PROJECT_SOURCE_DIR)
The ouput will be :
-- PROJECT_SOURCE_DIR="path/to/your/project"
With cmake_print_variables you can also display several variables, like this:
cmake_print_variables(PROJECT_SOURCE_DIR ANOTHER_VARIABLE)
The ouput will be :
-- PROJECT_SOURCE_DIR="path/to/your/project" ; ANOTHER_VARIABLE="other variable"

cmake + cpp: No such file or directory

I've I'm trying to build this "Hello World" wxWidgets example on Linux, using the following cmake script:
cmake_minimum_required (VERSION 2.6)
project (wxL)
find_package(wxWidgets 3.0.0 REQUIRED
COMPONENTS base core net xml html adv qa richtext
)
file(GLOB SOURCES "src/*.cpp")
add_executable(wxL ${SOURCES})
Building the project yields this error:
src/wxL.cpp:3:10: fatal error: wx/wxprec.h: No such file or directory
The file specified in the include, wx/wxprec.h can be found on disk at this location:
/usr/include/wx-3.0/wx/wxprec.h
Furthermore, another program that I have built from source includes the same file (also using cmake) and builds just fine.
So, how do I use cmake to tell the compiler that the file should be included from somewhere in the system directories?
I know I'm missing something basic, but I can't figure out what.
Although you've found the package, your executable does not know anything about it.
For the executable to compile correctly, it needs to find header files for your package together with the .so / .a files. Following example should get you started:
include_directories(${wxWidgets_INCLUDE_DIRS})
add_executable(wxL <add-source-files-here>)
target_link_libraries(wxL ${wxWidgets_LIBRARIES}) // links wxWidgets libraries to your executable
Please note that using glob is not a recommended way of adding source files to your project.

GLFW directory not found with CMake and vcpkg

I have been unable to figure out how to get CMake to find and set correct GLFW CMake constants when using CMake in VS2017. Any help will be greatly appreciated :).
I downloaded glfw3 through Microsoft's vcpkg tool. I have checked that files do physically exist in the directory that vcpkg puts them in (~\vcpkg\installed\x86-windows\include). I set up my CMakeSettings.json as per their docs here. I used that tutorial as a basis for getting GLFW to be set up correctly.
I then use find_package(glfw3 REQUIRED) to find the glfw3 library. This does not spit out any errors. Actually the CMakeLists.txt doesn't complain at all. It's at the compile stage where it complains.
After that I add glfw3 with target_link_libraries(exe ${GLFW3_LIBRARIES}) to the executable.
Then when I try and build a simple example (including the header file), the compilation fails because it cannot find GLFW/glfw3.h.
The error from MSVC:
fatal error C1083: Cannot open include file: 'GLFW/glfw3.h': No such file or directory
Here is my CMakeLists.txt for added reference:
cmake_minimum_required(VERSION 3.7)
project(learn-opengl)
find_package(glfw3 REQUIRED)
add_executable(learn-opengl main.cpp)
target_link_libraries(learn-opengl ${GLFW3_LIBRARIES})
GLFW3_LIBRARIES I got from glfw3Config.cmake by snooping around what vcpkg puts in the installed directory (~\vcpkg\installed\x86-windows\share\glfw3)
And just in case, the main.cpp:
#include <GLFW/glfw3.h>
int main()
{
return 0;
}
I have tried calling cmake from the command line as well, but to no avail that didn't work either.
Am I missing something? Did I perhaps misunderstand something in vcpkg documentation? I have really no idea what I am missing... :/ I should say, in addition, that I am fairly new to CMake as well.
Reformulating my previous comment as answer:
You should add the imported target glfw to your target_link_libraries command instead of ${GLFW3_LIBRARIES}.
The find_package(glfw3) generates an import target glfw. By making your target learn-opengl dependent on this imported target you specify both the library to link with and the include directories to use.

Using GPGME in Debian for C++ Application

I would like to use GPGME for key generation and encryption in my C++ application. However, while trying to get started, I got stuck with a problems:
I dowloaded the dev package for my debian system. Now I would like to tell my compiler (gcc in Qt Creator) where to find the library with cmake using the tool mentioned in the documentation. But I don't know how to add gpgme-config --cflags --libs to my compiler flags. This didn't work:
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} `gpgme-config --cflags --libs` ")
When I try to build the app the compiler can't find gpgme-config:
c++: error: $(gpgme-config: File or Directory not found
c++: error: unrecognized command line option ‘--cflags’
c++: error: unrecognized command line option ‘--libs)’
Anyway executing gpgme-config --cflags --libs on command line DOES give me a result:
-L/usr/lib/x86_64-linux-gnu -lgpgme -lassuan -lgpg-error
I know the documentation also mentions Automake and libtool to make this process easier. But I neither used Automake or libtool before.
UPDATE:
I also tried to use a FindGpgme.cmake file for GPGME. But the first file I used required several other cmake files, which I also downloaded. I put them in the same directory as FindGpgme.cmake. The main cmake file (FindGpgme.cmake) was found, but MacroEnsureVersion and MacroBoolTo01 not. My change to my CMakeLists.txt was the following:
include(cmake_modules/FindGpgme.cmake)
find_package(Gpgme)
I tried relative and absolute path to the other files in FindGpgme.cmake. Same problem - cmake can't find them. My second try was with the file I found on gitweb. The error was:
CMake Error at cmake_modules/FindGpgme.cmake:376 (set_package_properties):
Unknown CMake command "set_package_properties".
Call Stack (most recent call first):
CMakeLists.txt:7 (include)
I have absolutely no glue how to fix that set_package_properties problem.
UPDATE 2
I added
include(FeatureSummary)
to my CMakeLists.txt as proposed by kfunk. Now I get the following error:
CMake Warning at CMakeLists.txt:9 (find_package): By not providing
"FindGpgme.cmake" in CMAKE_MODULE_PATH this project has asked CMake
to find a package configuration file provided by "Gpgme", but CMake
did not find one.
Could not find a package configuration file provided by "Gpgme" with
any of the following names:
GpgmeConfig.cmake
gpgme-config.cmake
Add the installation prefix of "Gpgme" to CMAKE_PREFIX_PATH or set
"Gpgme_DIR" to a directory containing one of the above files. If
"Gpgme" provides a separate development package or SDK, be sure it
has been installed.
Even the message description seams pretty detailed I don't know how to add the FindGpgme.cmake to CMAKE_MODULE_PATH or how to add the requested prefix to CMAKE_PREFIX_PATH. The dev package however is definitely installed (using package manager)
I'd suggest to use a proper CMake find script to look up the GPGME installation:
Example here:
https://quickgit.kde.org/?p=kwallet.git&a=blob&h=7a092104ba0604b0606c4662750b8b32c5c3e2c6&f=cmake%2FFindGpgme.cmake&o=plain
Then something like this in your CMake code (untested):
find_package(Gpgme)
include_directories(${GPGME_INCLUDES})
target_link_libraries(YOURTARGET ${GPGME_VANILLA_LIBRARIES)