cmake setting for cuda&visual studio - cmake

How to set up custom CUDA directory for enable_language(CUDA)?
From cmake version 3.8~ ,I can use enable_language(CUDA) command.
In my custom cmake file, I set CUDA_TOOLKIT_ROOT_DIR same as cuda custom directory(not an default directory).
But when enable_language(CUDA)command is called, cmake default modules are trying to find CUDA in default directory(somewhere in c:\progrmafiles).
I don't want make my teammate install CUDA toolkit separately.
So i want to include all necessary component and tools for using CUDA in custom directory.
How can i make it done?
Target Environment Info
OS windows 7 x64 (only)
IDE visual studio 2013
cmake version 3.11.4
CUDA version 9.2

If I look at CMakeDetermineCUDACompiler.cmake you can select a specific compiler for enable_language(CUDA) via:
The environment variable CUDACXX (for non-"Visual Studio" environments)
> cmake -E env CUDACXX="<your CUDA path here>" cmake ..
Simply set global variable CMAKE_CUDA_COMPILER
> cmake -D CMAKE_CUDA_COMPILER:FILEPATH="<your CUDA path here>" ..
or in your CMakeLists.txt before your project() or enable_language()
call
if (NOT CMAKE_CUDA_COMPILER)
set(CMAKE_CUDA_COMPILER "${CMAKE_SOURCE_DIR}/<your CUDA relative path here>")
endif()

Related

clion wsl "CMake 3.20 or higher is required. You are running version 3.16.3"

so I just downloaded wslusing the wsl --install command using PowerShell
now I'm trying to connect it to Clion which works
i cant add images so here is a link to it
but when i'm tying to build the project i get this error
"CMake Error at CMakeLists.txt:1 (cmake_minimum_required):
CMake 3.20 or higher is required. You are running version 3.16.3"
my cmake file:
cmake_minimum_required(VERSION 3.20)
project(ex2 C)
set(CMAKE_C_STANDARD 99)
add_executable(ex2
ex2.c ex2.h
main1.c
main2.c)
i tried updating wsl using wsl --update (in powershell)
The CMake installation inside the WSL is used. Unfortunately currently snap doesn't seem to be available in WSL, but installing the latest CMake version isn't too complicated nonetheless:
(optional) uninstall the old cmake installation in WSL; personally I don't see any benefit in multiple CMake installations, but there may be valid reasons for this. Most likely this will just makes the use of cmake more complex, since you need to remember which cmake version is used.
Download the appropriate cmake version from the cmake website ( https://cmake.org/download/ ). The version to choose is the tar.gz file under binary distributions for the x86_64 target. To get version 3.21.4 (currently the latest release), you can download the file from https://github.com/Kitware/CMake/releases/download/v3.21.4/cmake-3.21.4-linux-x86_64.tar.gz (Identical to the link on the CMake download page).
Unpack the archive from WSL. Navigate to the directory where you want the cmake installation to recide. This will add the cmake-3.21.4-linux-x86_64 directory containing all the files required to work with cmake to the current working directory. Let's assume you want to add the cmake files to the /opt directory. Furthermore let's assume the windows user name to be fabian, C: to be the primary hard drive and the download to be saved to the Downloads directory in the user directory:
cd /opt
tar -xf /mnt/c/Users/fabian/Downloads/cmake-3.21.4-linux-x86_64.tar.gz
(optional) make CMake available without specifying the path; this could be done as described here: https://unix.stackexchange.com/questions/3809/how-can-i-make-a-program-executable-from-everywhere ; don't do this, if an existing cmake installation is already available in the command line assuming you did install cmake to /opt, the cmake binary recides at /opt/cmake-3.21.4-linux-x86_64/bin
You should now be able to use cmake specifying either the full path to the executable (/opt/cmake-3.21.4-linux-x86_64/bin/cmake assuming you used the /opt directory) or directly via a command after opening the WLS commandline again (provided you followed step 4).
Now the only thing left to do should be telling CLion about the location of the cmake executable. Note that I haven't tested this, since I don't use this IDE. It's working fine using Visual Studio Code though...

Setting Cmake Library paths without Editing CmakeLists

Goal: To set Cmake Paths without having to edit the CMakeLists.txt file.
In Linux, using find_library() in Cmake is enough to find a library installed with apt.
In Windows, Cmake is unable to find it automatically.
Note: Please treat sqlite3 as any library.
What is the proper method to set the library paths which cmake searches for my library files without having to edit CMakeLists?
Note: I have samlib compiled to a static library file and placed in some directory away from the project itself.
I have tried the following on Linux and Windows.
cmake_minimum_required(VERSION 3.0.0)
project(SampleProj VERSION 0.1.0)
find_library(samlib NAMES samplelib)
add_executable(SampleProj src/main.cpp)
target_link_libraries(SampleProj ${samlib})```
Try to use CMAKE_LIBRARY_PATH
Semicolon-separated list of directories specifying a search path for the find_library() command. By default it is empty, it is intended to be set by the project. See also CMAKE_SYSTEM_LIBRARY_PATH and CMAKE_PREFIX_PATH.
src: https://cmake.org/cmake/help/latest/variable/CMAKE_LIBRARY_PATH.html
set CMAKE_GENERATOR=Visual Studio 15 2017 Win64
cmake -H. -Bbuild -G "%CMAKE_GENERATOR%" "-DCMAKE_PREFIX_PATH=path_to_samlib"

How to choose OpenCL implementation with CMake?

I have a Windows OpenCL application that uses some of the AMD extensions. Additionally, my application has some optional CUDA components. When building the visual studio solution with CMake, the FindOpenCL module picks the Nvidia OpenCL implementation instead of AMD's. Is there an easy way to make CMake use the AMD version?
I tried commenting the Nvidia sections in the FindOpenCL module code, but that did not work. Is there some registry setting I can modify? I installed CUDA after installing AMD's SDK. Would the installation order make a difference?
In the end, what worked was to remove the NVIDIA environment variables from the PATHS suggestions in the find_library and find_path commands and adding the NO_DEFAULT_PATH. The issue was that, even though I removed the suggestions, CMake was adding the default path which included NVIDIA's OpenCL implementation. Both commands ended up like shown below:
find_path(OpenCL_INCLUDE_DIR
NAMES
CL/cl.h OpenCL/cl.h
NO_DEFAULT_PATH
PATHS
ENV "PROGRAMFILES(X86)"
ENV AMDAPPSDKROOT
ENV ATISTREAMSDKROOT
PATH_SUFFIXES
include
OpenCL/common/inc
"AMD APP/include")
find_library(OpenCL_LIBRARY
NAMES OpenCL
NO_DEFAULT_PATH
PATHS
ENV "PROGRAMFILES(X86)"
ENV AMDAPPSDKROOT
ENV ATISTREAMSDKROOT
PATH_SUFFIXES
"AMD APP/lib/x86_64"
lib/x86_64
lib/x64
OpenCL/common/lib/x64)

Using HDF5 Libraries with CMake on Windows (Error: "Could NOT find HDF5")

I want to use the HDF5 libraries in my C++ program. I am using the VS 2010 x64 compiler and CMake 3.8.0rc2 on Windows 7. The HDF5 version I installed is 1.8.10 (installed by running the official "Installer").
In my CMakeLists file, I added the following lines:
FIND_PACKAGE ( HDF5 REQUIRED )
INCLUDE_DIRECTORIES (${HDF5_INCLUDE_DIRS})
SET (HDF5_LIBS ${HDF5_LIBS} ${HDF5_LIBRARIES})
...
target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES} ${HDF5_LIBS})
CMake shows the following error message upon Configuring:
Could NOT find HDF5 (missing: HDF5_LIBRARIES)
I also added the environment variables HDF5_DIR and HDF5_ROOT which both point to my HDF5 installation folder C:\Develop\HDF5\1.8.10.
What am I missing to have CMake recognize the HDF5 installation?
hdf5 can now be installed on Windows via vcpkg (https://github.com/Microsoft/vcpkg)
Jean
I was using an outdated HDF5 version; the current version is HDF5-1.8.18.
Unfortunately, the VS 2010 x64 generator is missing in the CMake-hdf5-1.8.18 archive. There are only .bat files for VS 2012, 2013 and 2015. It is possible to add other generators though:
Download CMake archive from the HDF5 download website, i.e. from this website. Make sure to take the version you want to install.
Open file HDF5config.cmake
Search for CTEST_CMAKE_GENERATOR
Add another elseif for your desired generator, i.e. for Visual Studio 2010 x64:
elseif(${BUILD_GENERATOR} STREQUAL "VS201064")
set(CTEST_CMAKE_GENERATOR "Visual Studio 10 2010 Win64")
Create another .bat file, i.e. build-VS2010-64.bat
Replace the BUILD_GENERATOR value with the one that you chose in the HDF5config.cmake file, i.e. ctest -S HDF5config.cmake,BUILD_GENERATOR=VS201064 -C Release -V -O hdf5.log
Run the .bat file
The built HDF5 installation will be available in the folder .\build\_CPack_Packages\win64
After that I changed the CMakeLists lines shown in the original question, as shown in the USING_HDF5_CMake.txt thats created while compilation. Note that I changed C to CXX in the component list because I have C++ project.
set (LIB_TYPE STATIC) # or SHARED
string(TOLOWER ${LIB_TYPE} SEARCH_TYPE)
find_package (HDF5 NAMES hdf5 COMPONENTS CXX ${SEARCH_TYPE})
# find_package (HDF5) # Find non-cmake built HDF5
INCLUDE_DIRECTORIES (${HDF5_INCLUDE_DIR})
set (LINK_LIBS ${LINK_LIBS} ${HDF5_CXX_${LIB_TYPE}_LIBRARY})

How to set CLion custom CMake executable to a version installed with Homebrew?

I'm trying to make CLion use the same version of CMake that I'm using from the command line. In Preferences > Build, Execution, Deployment > Toolchains, I tried setting a custom CMake executable path to /usr/local/Cellar/cmake/3.7.0/bin, but CMake displays a "not found" error on the same page.
For now, I had brew install the same version of CMake that CLion is using: brew switch cmake 3.6.2
But is there a way to make CLion use the version of cmake installed with brew?
Yes, you can set your own cmake binary for use in Clion.
You're on the right track, go to Preferences > Build, Execution, Deployment > Toolchains just as you did, and set CMake executabl -> Custom to
/usr/local/Cellar/cmake/3.7.0/bin/cmake
Note, your're supposed to put the absolute name of your own cmake here, not only the path to the directory containing cmake.