pybind/python_example

extra_compile_args seemingly ignored by custom BuildExt

mivade opened this issue · 3 comments

Trying to adapt this to a project and add -fopenmp, I found that I get warnings about ignoring #pragma omp parallel for. Inspecting the actual compiler call, -fopenmp doesn't show up even though the extra_compile_args=['-fopenmp'] is given to the Extension.

Workaround: in BuildExt.build_extensions, add:

opts.append('-fopenmp')

A better way to do this might be to note use a custom build_ext class at all, but instead just do all the platform/compiler checking logic at the top level and passing the results in to extra_compile_args.

adding two lines fixed this:

ct = self.compiler.compiler_type              
opts = self.c_opts.get(ct, [])                
link_opts = self.l_opts.get(ct, [])           
for arg in ext_modules[0].extra_compile_args: 
┊   opts.append(arg)                          

Trying to adapt this to a project and add -fopenmp, I found that I get warnings about ignoring #pragma omp parallel for. Inspecting the actual compiler call, -fopenmp doesn't show up even though the extra_compile_args=['-fopenmp'] is given to the Extension.

Workaround: in BuildExt.build_extensions, add:

opts.append('-fopenmp')

A better way to do this might be to note use a custom build_ext class at all, but instead just do all the platform/compiler checking logic at the top level and passing the results in to extra_compile_args.

what is the opts variable? I need to add openmp, also found that the -fopenmp don't show up in the actual compile call.

Oh, I see, in Pybind11Extension: add two lines:

extra_compile_args = ["-fopenmp"],
extra_link_args = ["-fopenmp"],

in

ext_modules = [
    Pybind11Extension("python_example",
        ["src/main.cpp"],
        # Example: passing in the version to the compiled code
        define_macros = [('VERSION_INFO', __version__)],
        extra_compile_args = ["-fopenmp"],
        extra_link_args = ["-fopenmp"],
        cxx_std = 11,
        language = 'c++',
        ),
]

It works out.