Basic issue about Cmake - c++

Im trying to use OpenCV but I keep on getting this error on Cmake:
CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles"
I know what I have to do is : "cmake is having a hard time finding make and GCC, add the mingw bin dir to your PATH environment variable."
But I do not know:
how to add the mingw
where is the PATH environment variable?

This link describes how to add entries to the PATH environmental variable. You need to add the directory where your MinGW is installed there.
You can also consult wiki if you want some information what PATH is actually for.

Related

How to install a cpp library using cmake on Windows x64?

I'm using CLion with MinGW-GCC on the Windows-x64 platform - This is the background of the problem.
I was trying to install gtest before. But a lot of confusion arose in the middle.
First time I ran those commands(in googletest-release-1.12.1\) according to the instructions of googletest-release-1.12.1\googletest\README.md:
mkdir build
cd build
cmake ..
But I got error messages like:
CMake Error at CMakeLists.txt:51 (project):
Failed to run MSBuild command:
C:/Windows/Microsoft.NET/Framework/v4.0.30319/MSBuild.exe
to get the value of VCTargetsPath:
Then I changed my last command to
cmake -G "MinGW Makefiles" ..
because I use make provided by MinGW. I don't know whether it's right but, it ran properly.
then I called
make
make install
make ran smoothly. But when I ran make install, I got these messages:
Install the project...
-- Install configuration: ""
-- Installing: C:/Program Files (x86)/googletest-distribution/include
CMake Error at googlemock/cmake_install.cmake:41 (file):
file INSTALL cannot make directory "C:/Program Files
(x86)/googletest-distribution/include": No such file or directory.
Call Stack (most recent call first):
cmake_install.cmake:42 (include)
make: *** [Makefile:109: install] Error 1
I have no idea at all this time. So I changed my way. According to this answer, I copied the whole library into my project and edited CMakeLists.txt like this:
cmake_minimum_required(VERSION 3.23)
project(gtest_study)
set(CMAKE_CXX_STANDARD 20)
add_subdirectory(googletest-release-1.12.1)
include_directories(googletest-release-1.12.1/googletest/include)
include_directories(googletest-release-1.12.1/googlemock/include)
add_executable(gtest_study main.cpp)
target_link_libraries(gtest_study gtest gtest_main)
target_link_libraries(gtest_study gmock gmock_main)
So my questions are:
Is there any difference between the two which build it using make and cmake metioned firstly, and just use commands like include_directories and target_link_libraries in CMakeLists.txt? (maybe like .h and .dll file? Or just completely the same? I don't know)
When I use make install to install a library on Windows, what should I do in particular? Specify some directory (I don't know which one) or what?
Although in my system environment I use MinGW-makefile, in CLion which the libraries are eventually used, I use ninja as the generator for CMake (it just comes with CLion, not installed for the system). Do I have to specify it and how? (-G "Ninja"doesn't work in my native env)
The difference between
cmake ..
and
cmake -G "MinGW Makefiles" ..
Is the choice of generator: The former uses the default generator, the latter uses the generator you specified. (cmake --help should put a * next to the default generator.)
Based on the error message I assume this is a visual studio generator and you may not be able to run that one properly from within a MinGW terminal.
In the latter case the default install directory seems to be based on the target OS (Windows) but does not seem to incorporate the fact that you're running from a MinGW terminal where the default install path (C:/Program Files (x86)/googletest-distribution) is not valid.
You could try to fix this by providing it during cmake configuration (passing -D 'CMAKE_INSTALL_PREFIX=/c/Program Files (x86)/googletest-distribution' before the source dir) or by providing the install directory during the installation.
The following process should allow you to install the lib. I'm using my preferred way of building here, i.e. not using build system dependent commands, but using cmake to run the build/install commands. I assume the working directory to be the root directory of the gtest sources:
cmake -G "MinGW Makefiles" -S . -B build
cmake --build build
cmake --install build --prefix '/c/Program Files (x86)/googletest-distribution'
The last command needs to be run with admin privileges, the first 2 I don't recommend running as admin. You could instead install to a directory where you do have the permissions to create directories even without admin privileges.
The difference between using the process described above and using add_subdirectory is that the former results in a installation on the system which can be used via find_package and the google test libs won't be rebuilt for every project where you do this.
...
project(gtest_study)
...
# you may need to pass the install location via -D CMAKE_PREFIX_PATH=<install_location> during configuration for this to work
find_package(GTest REQUIRED)
target_link_libraries(gtest_study PRIVATE GTest::gtest_main GTest::gmock)
The latter builds the google test project as part of your own project build and for every project where you use this approach a seperate version of the google test libs is built. Note: there should be no need to specify the include dirs yourself, since this kind of information is attached to the cmake target and gets applied to the linking target automatically:
#include_directories(googletest-release-1.12.1/googletest/include)
#include_directories(googletest-release-1.12.1/googlemock/include)
add_executable(gtest_study main.cpp)
target_link_libraries(gtest_study PRIVATE gtest_main gmock)
As for 3.: The CMake generator used for building GTest should be independent of the generator of the project using it. The thing that's important is that the compilers used by the build systems are compatible. I cannot go into detail about this, since I've never used CLion and therefore have too little knowlege about the compilers used by it. (Personally I'm working with Visual Studio on Windows.)

Unable to generate makefile from CMake

I'm interested in CMake, so decided to learn about it. I followed the CMake Tutorial until the point where it states "That is all there is to it. At this point you should be able to build the tutorial", so I thought, let's try it!
Alas, it seems it wasn't all so simple. I initially attempted to run cmake -A "Unix Makefiles" after consulting cmake --help, but no luck there either. Rather, it gives me the following:
CMake Error at CMakeLists.txt:2 (project):
Generator
Unix Makefiles
does not support platform specification, but platform
Unix Makefiles
was specified.
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
I began searching around on the Internet and found this, but it gave me the exact same error. Other answers I found seemed to be Windows-related, but myself I'm on Fedora 29.
I want to generate a makefile using CMake, any suggestions? (I have gcc and g++ installed, by the way)
On Fedora, generating Unix Makefiles (e.g. Makefile, which GNU Make will be able to use) is probably the default. In general, don't specify a generator: then you'll get what makes the most sense on your installed OS. Some OS do pick a different default: FreeBSD uses Ninja, for instance.
When running CMake you may need to specify the source-directory. If CMake has been run before, successfully, you can leave off the directory. When there are no other options or arguments to CMake, you'll need to give the directory.
If you've copy-pasted the CMakeLists and C++ files from the tutorial page, this will create whatever build-system comes from the default generator:
cmake .
Do note that if you don't give CMake any arguments at all, then it gives you a usage string. That's why there's that . there, to build from the current directory. If you want to specify a particular generator,
cmake -G "Unix Makefiles" .
If you are building in a separate directory (generally a good idea)
cmake /path/to/the/sources
Your original error message, by the way, tells you that CMake is, by default, using the generator Unix Makefiles, which doesn't support setting a platform. The no-arguments-at-all behavior seems to have tripped you up afterwards.
After a discussion in the comments, I realised the issue was that I had done it wrong from the start. The solution ultimately was to remove all CMake-related files (except for CMakeLists.txt and do it all over again, this time running cmake .. on its own (without any arguments) from a subdirectory.

Clion "Cannot load CMake project: CMake executable is incorrect" issue

I ran into a problem while trying to use Clion.
I imported a project, yet all off the headers and .cpp files are greyed out. The error shown is as in the title: "Cannot load CMake project: CMake executable is incorrect".Here is a screenshot of it.
It's worth noting that I'm working with kubuntu and have downloaded all of the following: gcc, g++, make, cmake, gdb.
I would love some help with solving this problem so I could start working with Clion.
Thanks in advance.
I just ran into this. Deleting the project's .idea directory and having clion recreate it fixed the issue for me.
Clion also reported that Bundled Cmake does not work in this enviroment
And when setting the actual path to the cmake binary it said it wasn't there when it was.
make a c executable project.
go to file->settings
Look for a setting where you can configure the path of Cmake on your system (maybe /usr/bin/cmake).
Clion hasn't found cmake executable
first case cmake isn't installed :
1.1. ubuntu : sudo apt-get install cmake
1.2. Go to Settings->Build,Execution,Deployement-> Toolchains:
set cmake executable to custom , and type : which cmake in your console
set field right to custom to dir of cmake
do same thing for gdb the apply
second case cmake is install then go to (1.2)
I am using WSL (wsl2 to be correct) and I had the same issue. Somehow CLion seems to have a problem with /usr/bin/cmake only being a link to the real cmake file. So to resolve this, follow each link till you hit the location of the executable
for windows users being new to linux:
which cmake (most probably shows /usr/bin/cmake)
cd /usr/bin/cmake
ls -la | grep cmake (shows something like 20 Mar 26 09:54 cmake -> /opt/cmake/bin/cmake)
do this till there is no link anymore
pass this path to clion
I already added this as a bug: https://youtrack.jetbrains.com/issue/CPP-20644

Netbeans project imported from existing cmake application fails to build with filesystem error on Windows

I am attempting to import a manually-created cmake project that I had been using in a different IDE into Netbeans 8.0.2 on Windows 7. Needless to say, my cmake configuration worked fine there.
Netbeans seems to import the directory fine. I imported it in "automatic" (cmake) mode. However, when I attempt to build the project, I get a rather cryptic (Java?) error message:
Makefile:76: recipe for target 'all' failed
process_begin: CreateProcess(NULL, /C/MinGW/bin/make.exe -f CMakeFiles/Makefile2 all, ...) failed.
make (e=2): The system cannot find the file specified.
Knowing very little about Java, I am not sure how to interpret this error. The first directory (/C/MinGW/bin/make.exe) stands out to me as not being in Windows-format, but I am not sure if that's incorrect. I do indeed have a file by that name, as I copied the longer-named mingw make binary so I would only need to type "make".
Presuming this is being run in the project root, and that the first directory is formatted correctly, I don't see any problem with finding these files.
My CMakeLists.txt is:
cmake_minimum_required(VERSION 2.8.4)
set(Project_Name "Test")
set(Test_VERSION_MAJOR 1)
set(Test_VERSION_MINOR 0)
project(${Project_Name})
include_directories(
"${CMAKE_CURRENT_SOURCE_DIR}/inc"
"${CMAKE_CURRENT_SOURCE_DIR}/inc/SDL"
"C:/Users/Bakaiya/Documents/ogre/OgreMain/include"
)
file(GLOB SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
link_directories(${CMAKE_CURRENT_SOURCE_DIR} ${OPENGL_LIBRARIES})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_executable(${Project_Name} ${SOURCE_FILES})
target_link_libraries(${Project_Name} SDL2main SDL2 OgreMain) #Ogre
Running the "generate makefile" command in the IDE completes without issue, but does not fix the problem. Additionally, clean fails, but "help" does work.
This is a problem within the IDE, it seems, because if I run make from the command line in the project root, it builds without issue.
Also, I fiddled with the file path mode setting under C/C++ -> Project Options, and it did nothing. Even set to absolute, what seems to be a relative path (CMakeFiles/Makefile2) is still in the failed command. I'm not sure if that option is expected to change that sort of reference or not.
What could be wrong with this imported project to cause this issue?
However, when I attempt to build the project, I get a rather cryptic (Java?) error message:
This is an error shown by netbeans to tell you, that it was unable to execute make command successfully. Usually this indicates a wrong setting of your (mingw-) tools.
Here are some points you can check:
Don't use make from mingw/bin, you have to use the one from mingw/msys/... There's a mingw make within mingw's msys folder, usually C:\<Path to MSYS>\<Version>\bin\make.exe - this bin-path must also be set in PATH environment variable! If MSys wasn't installed with your mingw installation, please install it.
Please check the tools set in Tools -> Options -> C/C++ -> Build Tools; you can test them by clicking Versions....
(If existing) Clean the CMake generated files and clean the cmake's cache. If not done yet, please use an out-of-source build as described here.
Can you build your project from terminal (without netbeans)?
The first directory (/C/MinGW/bin/make.exe) stands out to me as not being in Windows-format, but I am not sure if that's incorrect.
This is ok and intended by mingw - it uses linux / unix like paths.
Update
Which make program should I use?
Many MinGW users have a problem because they use mingw32-make.exe from
the MinGW installation. While this seems like the right choice, it
actually breaks the build. The problem is that this is a non-Posix
implementation of the Unix make program and doesn't work well at all.
In fact, thats why the MinGW people renamed it! They've also made a
FAQ entry explaining why you should not use mingw32-make.exe. Instead,
you should use the make.exe program from the MSYS package.
As of NetBeans 6.1, the Build Tools panel no longer allows a user to
select mingw32-make. If you choose a MinGW compiler collection it will
default to make in MSYS. If MSYS is not found, it will tell you no
make program has been found.
(http://wiki.netbeans.org/MinGWInCCDevelopmentPack)

C++ cmake errors BOOST_ROOT DOXYGEN

I am trying to build a c++ implementation of hidden markov models - downloaded from
http://www.cs.au.dk/~asand/?page_id=152
I am compiling this on an ubuntu 12.04 with a g++ 4.6 compiler.
Following the instructions mentioned on the webpage, on typing
cmake .
I get the following errors,
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
CMake Error at CMakeLists.txt:101 (message):
The Boost C++ libraries was not found. Get Boost from
http://www.boost.org/ or set the environment variable BOOST_ROOT to point
to the root of boost directory.
Could someone help me resolve these issues out.
My boost folder is situated at
/usr/local/boost_1_52_0
It's telling you to set BOOST_ROOT environment variable. So just do it:
BOOST_ROOT=/usr/local/boost_1_52_0 cmake
(prefixing a command with setting of an environment variable in posix shell sets it for just that command; cmake will remember the value in CMakeCache.txt afterwards)
I suppose the fact it didn't find doxygen does not matter. You will should still be able to build the library, you just won't be able to generate nice documentation for it, but that probably exists on the web somewhere or you can read it in the headers directly anyway.