I have a C++ project that is implemented in Linux with JetBrain CLion IDE. To get the executable, I used CMake which is fully matched with JetBrain CLion. The project uses MPI to handle multi-node processing, and the project works fine in Linux.
Now, I have to get an executable for Windows too. So, I installed the community version of Visual Studio 2019 along with Microsft MPI to build my project there too. I found that it might be easier to create the sln file for the project by CMake and then import the project into VS. But, when I tried this approach, I got an error for not finding MPI:
Could NOT find MPI_C (missing: MPI_C_LIB_NAMES MPI_C_HEADER_DIR MPI_C_WORKS)
Could NOT find MPI_CXX (missing: MPI_CXX_LIB_NAMES MPI_CXX_HEADER_DIR MPI_CXX_WORKS)
CMake Error at C:/Program Files/CMake/share/cmake-3.15/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
Could NOT find MPI (missing: MPI_C_FOUND MPI_CXX_FOUND)
Call Stack (most recent call first):
C:/Program Files/CMake/share/cmake-3.15/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
C:/Program Files/CMake/share/cmake-3.15/Modules/FindMPI.cmake:1687 (find_package_handle_standard_args)
CMakeLists.txt:35 (find_package)
The cmake code that works in Linux is:
find_package(MPI REQUIRED)
include_directories(${MPI_INCLUDE_PATH})
add_executable(prjt main.cpp)
add_subdirectory(sub1)
add_subdirectory(sub2)
target_link_libraries(prjt ${MPI_LIBRARIES})
set_property(TARGET prjt PROPERTY CXX_STANDARD 11)
After reading [1] and [2], I also tried:
set(CMAKE_PREFIX_PATH "C:/Program Files/Microsoft MPI")
find_package(MPI REQUIRED)
include_directories(${MPI_INCLUDE_PATH})
set(CMAKE_C_FLAGS "${CMAKE_FLAGS} ${MPI_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MPI_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${MPI_EXE_LINKER_FLAGS}")
add_executable(prjt main.cpp)
add_subdirectory(sub1)
add_subdirectory(sub2)
target_link_libraries(prjt ${MPI_LIBRARIES})
set_property(TARGET prjt PROPERTY CXX_STANDARD 11)
but still no success.
I also tried to link MPI directly via MSVS, as it is instructed in [3], but it did not work too. The problem within that approach is that when I call set MSMPI, I only get MSMPI_BIN=C:\Program Files\Microsoft MPI\Bin\ and I do not get the rest of the environment variables. In the Linker of MSVS also I manually added the Microst SDK address. (I also added the other required environment variables as [4] explains)
I appreciate any help or comment.
Related
I've been working in this project on CLion, seems nothing strange happened at all. But I was trying to run it on VS Code, or I can say, I want to run it manually without any
IDE's help.
Here is my CMakeList.txt
# Find OpenGL
find_package(OpenGL REQUIRED)
if (OpenGL_FOUND)
message(STATUS "Debug: Found OpenGL")
list(APPEND EXTRA_LIBS OpenGL::GL)
else()
message(STATUS "Debug: Could NOT find OpenGL")
endif ()
# Find GLEW
find_package(GLEW REQUIRED)
if (GLEW_FOUND)
message(STATUS "Debug: Found GLEW")
list(APPEND EXTRA_LIBS GLEW::glew)
else()
message(STATUS "Debug: Could NOT find GLEW")
endif ()
Build project in CLion, it run just fine
-- Debug: Found OpenGL
-- Debug: Found GLEW
-- Using Win32 for window creation
-- Debug: SUBDIRECTORY OpenGL::GL;GLEW::glew;glfw
-- Debug: MainWindow OpenGL::GL;GLEW::glew;glfw;SUBDIRECTORY
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Workplace/GameMaking/GLFW/GLFW-1st-project/cmake-build-debug
[ 72%] Built target glfw
[ 81%] Built target SUBDIRECTORY
[ 90%] Built target MainWindow
[100%] Built target My_Project
Build finished
But when I try to build it with command line in VS CODE, I create vs-build directory inside project folder just like CLion did (CLion create a folder called cmake-build-debug)
mkdir vs-build
cd vs-build
cmake ..
It cannot found GLEW
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.22000.
-- Debug: Found OpenGL
CMake Error at D:/Programs/CMake/share/cmake-3.21/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
Could NOT find GLEW (missing: GLEW_INCLUDE_DIRS GLEW_LIBRARIES)
Call Stack (most recent call first):
D:/Programs/CMake/share/cmake-3.21/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
D:/Programs/CMake/share/cmake-3.21/Modules/FindGLEW.cmake:215 (find_package_handle_standard_args)
CMakeLists.txt:28 (find_package)
-- Configuring incomplete, errors occurred!
Why would CLion can find my glew lib but my cmake manually cannot?
Did I make any mistakes in my cmake or used wrong cmake commands?
It seems like #AlanBirtles is right, I had run some codes which might added GLEW path to my project build before at CLion, then I rechecked and thought that those are redundant lines so I deleted them. So now my cmake need to find package manually.
I've managed to find GLEW package manually like this
# Find GLEW
find_package(GLEW QUIET)
if (GLEW_FOUND)
message(STATUS "Debug: Found GLEW")
list(APPEND EXTRA_LIBS GLEW::glew)
else()
message(STATUS "Debug: Could NOT find package GLEW automatically")
message(STATUS "Debug: Finding GLEW manually")
list(APPEND CMAKE_PREFIX_PATH "D:/Programs/glew")
find_package(GLEW REQUIRED)
list(APPEND EXTRA_LIBS GLEW::glew)
endif ()
It looks like you have glew installed to a custom location. This is fine and normal, but hard-coding absolute paths to your personal file system is very bad practice. Please do not make a habit of doing this.
You can hint to CMake where to find GLEW by setting -DGLEW_ROOT=D:/Programs/glew at the configure command line.
When searching for a dependency you should avoid doing anything beyond a simple find_package(GLEW REQUIRED). CMake already has a complex search procedure that is designed to be overridden by the build-system's users.
Every "convenient" behavior you hard code into your CMakeLists.txt will eventually become annoying instead. Keep this file to an absolute minimum and store your environment specific settings in CMakePresets.json
so I was trying to link to wxWidgets with CMake, and I couldn't get it to work. Here is my CMakeLists.txt.
cmake_minimum_required(VERSION 3.20.1)
set(CMAKE_CXX_STANDARD 20)
project(FormApplication)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
set(EXE_NAME run)
find_package(wxWidgets REQUIRED gl core base OPTIONAL_COMPONENTS net)
include(${wxWidgets_USE_FILE})
add_subdirectory(src) # creates an exe named ${EXE_NAME}
target_link_libraries(${EXE_NAME} PRIVATE ${wxWidgets_LIBRARIES})
I get the following error when running cmake -S . -G "Ninja" -B build
CMake Error at C:/Program Files/CMake/share/cmake-3.20/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
Could NOT find wxWidgets (missing: wxWidgets_LIBRARIES
wxWidgets_INCLUDE_DIRS gl core base)
Call Stack (most recent call first):
C:/Program Files/CMake/share/cmake-3.20/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
C:/Program Files/CMake/share/cmake-3.20/Modules/FindwxWidgets.cmake:1025 (find_package_handle_standard_args)
CMakeLists.txt:7 (find_package)
Why do I get this error? Every source I go to has a different answer, but none of them have worked so far. Some even say you can't link to wxWidgets with cmake.
Please help, I've been trying to get this to work for a couple days already. Thanks in advance.
I cannot get boost::asio to work with cmake in my c++ program. I have actually tried and googled for many hours, but I cannot get it to work!
I want to include boost::asio in my c++ Project under Ubuntu 18.04 with a cmake file.
So I installed the newest CMake (cmake version 3.19.4), and I downloaded boost version 1.74 and executed
./bootstrap.sh --prefix=/usr/
sudo ./b2 install
The install directory is /home/boost/boost_1_74_0. My CMake file looks like this:
cmake_minimum_required (VERSION 3.1.0)
# Project name
project (machine_tryout VERSION 1.0)
# Boost (header only)
#set(Boost_DEBUG 1)
set(BOOST_ROOT "$ENV{HOME}/boost/boost_1_74_0")
set(Boost_INCLUDE_DIR "$ENV{HOME}/boost/boost_1_74_0")
set(Boost_LIBRARY_DIR "$ENV{HOME}/boost/boost_1_74_0/libs")
find_package(Boost REQUIRED Components asio)
# Set Executable
add_executable(${PROJECT_NAME} source/tryout.cpp)
But everything I get is the following:
vm-umic#vm:~/Projects/tryout/build$ cmake ..
CMake Warning at /snap/cmake/775/share/cmake-3.19/Modules/FindBoost.cmake:2034 (message):
No header defined for asio; skipping header check (note: header-only
libraries have no designated component)
Call Stack (most recent call first):
CMakeLists.txt:27 (find_package)
CMake Error at /snap/cmake/775/share/cmake-3.19/Modules/FindPackageHandleStandardArgs.cmake:218 (message):
Could NOT find Boost (missing: asio) (found version "1.74.0")
Call Stack (most recent call first):
/snap/cmake/775/share/cmake-3.19/Modules/FindPackageHandleStandardArgs.cmake:582 (_FPHSA_FAILURE_MESSAGE)
/snap/cmake/775/share/cmake-3.19/Modules/FindBoost.cmake:2193 (find_package_handle_standard_args)
CMakeLists.txt:27 (find_package)
-- Configuring incomplete, errors occurred!
See also "/home/vm-umic/Projects/tryout/build/CMakeFiles/CMakeOutput.log".
What in the world am I doing wrong? Isnt CMake telling me that it found Boost 1.74? CMake does NOT throw any errors if I try find_package(Boost REQUIRED), but then linking does also not work. I explicitly tell CMake where to find the libraries, so why can't CMake finde Boost?
Try this.
cmake_minimum_required (VERSION 3.1.0)
# Project name
project (machine_tryout VERSION 1.0)
# Boost (header only)
#set(Boost_DEBUG 1)
set(BOOST_ROOT "$ENV{HOME}/boost/boost_1_74_0")
set(Boost_INCLUDE_DIR "$ENV{HOME}/boost/boost_1_74_0")
set(Boost_LIBRARY_DIR "$ENV{HOME}/boost/boost_1_74_0/libs")
find_package(Boost REQUIRED Components system)
# Set Executable
add_executable(${PROJECT_NAME} source/tryout.cpp)
target_link_libraries(${PROJECT_NAME}
${Boost_LIBRARIES})
I just had a similar problem, and found that I had missed a small but important step in the Boost installation: adding the installation directory (i.e. the PREFIX used in b2 install --prefix=PREFIX) to the PATH environment variable.
For me, this solved it – hopefully will for you too!
I'm trying to link GLEW to my CMake project with little success.
Apparently, it can't find GLEW_LIBRARIES.
I'm using CLion 2019.3.4 and MinGW.
So far I've tried these things:
Defining GLEW_LIBRARIES
Defining GLEW_STATIC_LIBRARIES and GLEW_SHARED_LIBRARIES, as the FindGLEW.cmake file documents that (Line 41 to 45).
Doing both of the above.
Doing 1 and 2 except instead of LIBRARIES it is LIBRARY.
I don't know what else to do.
Heres the CMake lists for reference:
cmake_minimum_required(VERSION 3.15)
project(myProject)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
set(CMAKE_MODULE_PATH "${CMAKE_HOME_DIRECTORY}/cmake_modules/")
# This part of the code is actually in a separate file,
# called LibrarySetup.cmake
#
# include(LibrarySetup.cmake)
if(WIN32)
set(LIB_PREFIX "")
set(LIB_SUFFIX ".dll")
elseif(UNIX)
set(LIB_PREFIX "lib")
set(LIB_SUFFIX ".lib")
endif()
set(GLFW_INCLUDE_DIR "include/glfw/include/")
set(GLFW_LIBRARY "include/glfw/lib/${LIB_PREFIX}glfw3${LIB_SUFFIX}")
set(GLEW_INCLUDE_DIR "include/glew/include/")
set(GLEW_SHARED_LIBRARIES "include/glew/lib/Release/Win32/glew32.lib")
set(GLEW_STATIC_LIBRARIES "include/glew/lib/Release/Win32/glew32s.lib")
set(GLEW_VERBOSE)
# And here the external file ends.
find_package(GLFW REQUIRED)
find_package(GLEW REQUIRED)
include_directories(${GLFW_INCLUDE_DIR} ${GLEW_INCLUDE_DIRS})
add_subdirectory(include/glfw)
add_executable(myProject main.cpp)
target_link_libraries(myProject ${GLFW_LIBRARY} ${GLEW_LIBRARIES})
And the errors:
~\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\193.6494.38\bin\cmake\win\bin\cmake.exe -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" "~\Documents\CLion Projects\myProject"
CMake Error at ~/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/193.6494.38/bin/cmake/win/share/cmake-3.15/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
Could NOT find GLEW (missing: GLEW_LIBRARIES)
Call Stack (most recent call first):
~/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/193.6494.38/bin/cmake/win/share/cmake-3.15/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
~/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/193.6494.38/bin/cmake/win/share/cmake-3.15/Modules/FindGLEW.cmake:207 (find_package_handle_standard_args)
CMakeLists.txt:12 (find_package)
-- Configuring incomplete, errors occurred!
See also "~/Documents/CLion Projects/myProject/cmake-build-debug/CMakeFiles/CMakeOutput.log".
[Failed to reload]
The version of CMake your script requires ships with FindGLEW which should do the work of finding the library for you (i.e. set up a Glew target, define include and library paths, etc). You can see the documentation for this module by running:
cmake --help-module findglew
Providing include paths and library definitions of GLEW to your executable should be as simple as:
find_package(GLEW REQUIRED)
add_executable(myProject main.cpp)
target_link_libraries(myProject GLEW::GLEW)
This will provide include and lib paths via the transitive dependency of the GLEW::GLEW target. You should not need to set the paths manually as your example does. The find module will search in the default system locations for the library. If it can't find it you can provide it with a hint via setting the GLEW_ROOT variable to point to your local install location.
set(GLEW_ROOT <my location of GLEW>)
How did you install GLEW? Can you provide an indication of where it is installed on your system and that might make it easier to see why the find module failed?
I've been trying for a few days to correctly setup OpenCV with CLion with little success, so asking on SO.
Here's what my CMakeLists looks like:
cmake_minimum_required(VERSION 3.12)
project(ocv_test)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(OpenCV REQUIRED)
set(SOURCE_FILES main.cpp)
add_executable(ocv_test ${SOURCE_FILES})
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(ocv_test ${OpenCV_LIBS})
Here's the error I get:
"C:\Program Files\JetBrains\CLion 2018.2.2\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" C:\Users\Owner\CLionProjects\ocv-test
Could not find OpenCV_CORE_INCLUDE_DIR
Could not find OpenCV_HIGHGUI_INCLUDE_DIR
Include dir: OFF
CMake Error at C:/Program Files/JetBrains/CLion 2018.2.2/bin/cmake/win/share/cmake-3.12/Modules/FindOpenCV.cmake:220 (MESSAGE):
OpenCV required but some headers or libs not found. Please specify it's
location with OpenCV_ROOT_DIR env. variable.
Call Stack (most recent call first):
CMakeLists.txt:6 (find_package)
-- Configuring incomplete, errors occurred!
See also "C:/Users/Owner/CLionProjects/ocv-test/cmake-build-debug/CMakeFiles/CMakeOutput.log".
I primarily followed these steps from another SO answer, but here are the steps:
Installed MinGW-64
Architecture: x86_64, Threads: posix, Exception: sjlj
Installed CMake 3.12.2 x64 msi
In System variables, set/create the following:
_CMAKE_HOME (C:\Program Files (x86)\CMake)
_MINGW_HOME (C:\mingw\mingw64)
Then, add the following to Path variable:
%_CMAKE_HOME%\bin
%_MINGW_HOME%\bin
Download OpenCV 3.4.3, and Extract to:
C:\opencv\opencv-3.4.3
Using CMake, Configure w/ MinGW Makefiles and specifying Native compilers:
C: C:/mingw/mingw64/bin/x86_64-w64-mingw32-gcc.exe
C++: C:/mingw/mingw64/bin/x86_64-w64-mingw32-g++.exe
Then, Generate (without Tests, Docs, Python, WITH_IPP, WITH_MSMF) to:
C:_dev_sw\opencv\opencv-3.4.3\build_mingw
Run mingw32-make, then mingw32-make install in C:_dev_sw\opencv\opencv-3.4.3\build_mingw
In System variables, set/create the following:
_OPENCV_HOME (C:\opencv\opencv-3.4.3\build_mingw\install\x64\mingw)
Then, add the following to Path variable:
%_OPENCV_HOME%\bin
Add FindOpenCV.cmake to:
C:\Program Files\JetBrains\CLion 2018.2.2\bin\cmake\win\share\cmake-3.12\Modules
Create new C++ executable project in CLion (ocv-test)
Update MakeLists.txt file (see above)
Reload MakeLists.txt and get errors shown above
I tried to update the CMakeLists as below, but still same errors:
cmake_minimum_required(VERSION 3.12)
project(ocv_test)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# Where to find CMake modules and OpenCV
set(OpenCV_DIR "C:\\opencv\\opencv-3.4.3\\build_mingw\\install")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(ocv_test main.cpp)
# add libs you need
set(OpenCV_LIBS opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs)
# linking
target_link_libraries(ocv_test ${OpenCV_LIBS})
Unlike this SO answer, I do not see OpenCV_DIR name in my CMake build. Also, I tried updating _OPENCV_HOME to OpenCV_ROOT_DIR (as error says), but that didn't work either.
Does anything seem off?
===
Edit 1:
FindOpenCV was the issue (so skip step 11). Setting the OPENCV_DIR var in CMakeLists fixed the errors, and built successfully (Thanks Tsyvarev!).
I'm not sure if setting OPENCV_DIR in CMakeLists will be an issue if the project is ran on another PC and/or OS, so I added OPENCV_DIR entry (pointing to /install directory) into CMake GUI, Repeated steps 6-8, created new but similar CLion project, and got the following error:
CMake Error at CMakeLists.txt:10 (find_package):
By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "OpenCV", but
CMake did not find one.
Could not find a package configuration file provided by "OpenCV" with any
of the following names:
OpenCVConfig.cmake
opencv-config.cmake
Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
"OpenCV_DIR" to a directory containing one of the above files. If "OpenCV"
provides a separate development package or SDK, be sure it has been
installed.
Again, this is fixed if I set the OPENCV_DIR variable. But how can it be avoided since it's already configured in GUI?