Skip to content

pybind11 incorrectly adds "-fvisibility" flag to nvcc #1710

Closed
@supervacuus

Description

@supervacuus

Since nvcc is a compiler driver and not a compiler in itself, it doesn't accept flags like -fvisibility directly, but passes them on via phase-options like -Xcompiler=. If you pass the flag directly, nvcc will return with an error (nvcc fatal : Unknown option 'fvisibility').

When you set up a typical pybind11 project that uses a mix of C++ and CUDA with

cmake_minimum_required(VERSION 3.9)
project(py_cuda LANGUAGES CXX CUDA)
add_subdirectory(pybind11)
pybind11_add_module(...)

and the module consists of C++ (cpp/h) and CUDA files (cu/cuh), nvcc will return the error mentioned above during the build.

I know that pybind11 doesn't officially support nvcc, but the fix for this issue probably very small. The culprit for the behaviour is the following line: https://github.com/pybind/pybind11/blob/master/CMakeLists.txt#L108

target_compile_options doesn't discriminate on the language being compiled or the compiler in use, which is probably why you placed it inside NOT MSVC condition. Interestingly pybind11_add_module already implements this correctly using set_target_properties and the respective target-property: https://github.com/pybind/pybind11/blob/master/tools/pybind11Tools.cmake#L158

Maybe those approaches could be merged? I would simply replace the line in the CMakeLists.txt with

set_target_properties(module PROPERTIES CXX_VISIBILITY_PRESET "hidden")
set_target_properties(module PROPERTIES CUDA_VISIBILITY_PRESET "hidden")

Am I missing something?

This happens in the following environment:
cmake version 3.10.2
The CXX compiler identification is GNU 7.3.0
The CUDA compiler identification is NVIDIA 9.1.85

But i think it will happen in all Linux environments and maybe even on MacOS.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions