1

I'm using CMake 3.16, with a CMakeLists.txt file specifying a minimum CMake version of 3.9. In my file, I have:

find_package(CUDA 8.0 REQUIRED)
find_package(OpenCL REQUIRED)
# etc. etc.
target_link_libraries(my_executable
    PRIVATE
    cuda # The NVIDIA CUDA driver API
    ${CUDA_LIBRARIES}
    OpenCL::OpenCL
    )

Now, in the CMake generation phase, I get the error:


 CMake Warning at CMakeLists.txt:44 (add_executable):
   Cannot generate a safe runtime search path for target my_executable because
   files in some directories may conflict with libraries in implicit
   directories:

     runtime library [libOpenCL.so.1] in /usr/lib64 may be hidden by files in:
       /usr/local/cuda/lib64

   Some of these libraries may not be found correctly.

Now, I do want the libOpenCL.so.1 from the CUDA directories; and building does produce an executable with the correct dependency. How can I tell CMake that this masking is ok, and not have it print the warning message?

Note: Working with CUDA in CMake has changed a lot over the 3.x series of releases. So whatever was happening before 3.8 is irrelevant, and also things changed significantly in 3.17 with a few more non-trivial changes afterwards. Answers about pre-3.8 and 3.17-or-later are, well, fine - but not what I need.

4
  • Can you post the full output of the CMake configure step? It looks to me like you have two incompatible copies of OpenCL installed and it's finding the system-wide one, rather than the cuda-vendored one. May 11, 2021 at 18:08
  • @AlexReinking: I'll be able to do that probably on Thursday. For now I'll say, that it's quite common to have some kind of CPU-targeted version of OpenCL installed as part of an OS distribution package, and then an independently-installed CUDA version which comes with its own OpenCL. IIRC, different OpenCL platforms should somehow play nice with each other so that you can select amongst them regardless of which one you linked against, but I'm not sure how/if that even works.
    – einpoklum
    May 11, 2021 at 19:57
  • You might try setting OpenCL_ROOT to /usr/local/cuda May 11, 2021 at 19:59
  • @AlexReinking: I could do that personally, but people building my library can't be expected to. I need CMakeLists.txt to work without these warnings even without that setting.
    – einpoklum
    May 11, 2021 at 20:06

2 Answers 2

2

I know this doesn't answer your question precisely, but as of CMake 3.17+, the CUDA OpenCL libraries are loaded by the FindCUDAToolkit module. It is used like so:

cmake_minimum_required(VERSION 3.17)
project(my_proj LANGUAGES C CXX CUDA)

find_package(CUDAToolkit 8.0 REQUIRED)

# ...

target_link_libraries(
  my_executable
  PRIVATE
    CUDA::cuda_driver
    CUDA::cudart
    CUDA::OpenCL
)

I hope this answer will help other readers who are using up-to-date CMake, because whatever answer works on 3.9 will not be quite as nice.

5
  • This doesn't answer my question, because the way CUDA is handled in CMake changed significantly around v3.17 (after the last major change in 3.8). Things are indeed much nicer now, but I need to support users with older CMake versions in their distributions.
    – einpoklum
    May 12, 2021 at 8:11
  • 1
    Like I said in the answer, "I know this doesn't answer your question precisely". It does answer a slight variant of your question where your minimum version is 3.17 instead of 3.9. May 12, 2021 at 16:20
  • ... but that is a non-issue when you can find_package(CUDAToolkit); no users would need our guidance in that case, since the problem wouldn't come up.
    – einpoklum
    May 12, 2021 at 16:31
  • 1
    ... but people using newer versions of CMake might not know about FindCUDAToolkit because of the ocean of outdated CMake information out there. Take a look through the "new" CMake questions on this site and take note of how many (don't) use imported targets, for instance. May 13, 2021 at 2:21
  • I have the same problem using find_package(CUDAToolkit). It seems that enable_language(CUDA) does not use the same paths as find_package(CUDAToolkit). Interestingly even if you find_package(CUDAToolkit) then enable_language(CUDA) might not be successfull. So having multiple versions might still be an issue.
    – VojtaK
    Dec 19, 2022 at 17:11
0

As @AlexReinking suggests in a comment, you can avoid this warning by giving CMake a (strong) hint regarding which OpenCL location you want to use. Before running CMake, set your OpenCL_ROOT environment variable to /usr/local/cuda; with a CMake version higher than 3.12, the find_package() command will use that variable, preferring to locate OpenCL there if possible - and not warn you about the alternative location.

3
  • Furthermore, you can do set(OpenCL_ROOT ${OpenCL_ROOT};${CUDA_TOOLKIT_ROOT_DIR}) in CMake before find_package(OpenCL) to make it locate the OpenCL library within the CUDA toolkit. This might also be useful if you want to require the OpenCL version separately.
    – Haim
    Oct 31, 2023 at 10:36
  • @Haim: Such settings within the CMake files are inappropriate... also - how would your suggestion differ from mine?
    – einpoklum
    Oct 31, 2023 at 17:26
  • I suggested to set OpenCL_ROOT automatically according to the result of find_package(CUDA), instead of setting it in the environment.
    – Haim
    Nov 1, 2023 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.