Failed to build with setuptools

Hatem: <p>I am trying to bind a custom function to python using setup tools and following the <a href="https://pytorch.org/tutorials/advanced/cpp_extension.html#writing-a-c-extension" rel="noopener nofollow ugc">tutorial</a> but I get the following error message although i was able to build it using cmake and worked successfully.<br>
I would appreciate any help, thank you.</p>
<blockquote>
<p>Blockquote<br>
running install<br>
running bdist_egg<br>
running egg_info<br>
creating custom_conv2d_cpp.egg-info<br>
writing custom_conv2d_cpp.egg-info/PKG-INFO<br>
writing dependency_links to custom_conv2d_cpp.egg-info/dependency_links.txt<br>
writing top-level names to custom_conv2d_cpp.egg-info/top_level.txt<br>
writing manifest file ‘custom_conv2d_cpp.egg-info/SOURCES.txt’<br>
reading manifest file ‘custom_conv2d_cpp.egg-info/SOURCES.txt’<br>
writing manifest file ‘custom_conv2d_cpp.egg-info/SOURCES.txt’<br>
installing library code to build/bdist.linux-x86_64/egg<br>
running install_lib<br>
running build_ext<br>
building ‘custom_conv2d_cpp’ extension<br>
creating /home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/build/temp.linux-x86_64-3.6<br>
Emitting ninja build file /home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/build/temp.linux-x86_64-3.6/build.ninja…<br>
Compiling objects…<br>
Allowing ninja to set a default number of workers… (overridable by setting the environment variable MAX_JOBS=N)<br>
[1/1] c++ -MMD -MF /home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/build/temp.linux-x86_64-3.6/conv_cpp_bind.o.d -pthread -B /home/hatem/anaconda3/envs/bind/compiler_compat -Wl,–sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include -I/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include/torch/csrc/api/include -I/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include/TH -I/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include/THC -I/home/hatem/anaconda3/envs/bind/include/python3.6m -c -c /home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/conv_cpp_bind.cpp -o /home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/build/temp.linux-x86_64-3.6/conv_cpp_bind.o -std=c++14 -DTORCH_API_INCLUDE_EXTENSION_H ‘-DPYBIND11_COMPILER_TYPE=“_gcc”’ ‘-DPYBIND11_STDLIB=“_libstdcpp”’ ‘-DPYBIND11_BUILD_ABI=“_cxxabi1011”’ -DTORCH_EXTENSION_NAME=custom_conv2d_cpp -D_GLIBCXX_USE_CXX11_ABI=0<br>
FAILED: /home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/build/temp.linux-x86_64-3.6/conv_cpp_bind.o<br>
c++ -MMD -MF /home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/build/temp.linux-x86_64-3.6/conv_cpp_bind.o.d -pthread -B /home/hatem/anaconda3/envs/bind/compiler_compat -Wl,–sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include -I/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include/torch/csrc/api/include -I/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include/TH -I/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include/THC -I/home/hatem/anaconda3/envs/bind/include/python3.6m -c -c /home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/conv_cpp_bind.cpp -o /home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/build/temp.linux-x86_64-3.6/conv_cpp_bind.o -std=c++14 -DTORCH_API_INCLUDE_EXTENSION_H ‘-DPYBIND11_COMPILER_TYPE=“_gcc”’ ‘-DPYBIND11_STDLIB=“_libstdcpp”’ ‘-DPYBIND11_BUILD_ABI=“_cxxabi1011”’ -DTORCH_EXTENSION_NAME=custom_conv2d_cpp -D_GLIBCXX_USE_CXX11_ABI=0<br>
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++<br>
In file included from /home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include/ATen/Parallel.h:140,<br>
from /home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include/torch/csrc/api/include/torch/utils.h:3,<br>
from /home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include/torch/csrc/api/include/torch/nn/cloneable.h:5,<br>
from /home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include/torch/csrc/api/include/torch/nn.h:3,<br>
from /home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/conv_cpp_bind.cpp:5:<br>
/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/include/ATen/ParallelOpenMP.h:83: warning: ignoring <span class="hashtag-raw">#pragma</span> omp parallel [-Wunknown-pragmas]<br>
83 | <span class="hashtag-raw">#pragma</span> omp parallel for if ((end - begin) &gt;= grain_size)<br>
|<br>
/home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/conv_cpp_bind.cpp: In function ‘at::Tensor custom_conv2d_forward(at::Tensor, at::Tensor, int64_t, int64_t, int64_t)’:<br>
/home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/conv_cpp_bind.cpp:32:38: warning: unused variable ‘ch_in’ [-Wunused-variable]<br>
32 |                                 auto ch_in = input.size(1);<br>
|                                      ^~~~~<br>
/home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/conv_cpp_bind.cpp:34:38: warning: unused variable ‘iw’ [-Wunused-variable]<br>
34 |                                 auto iw = input.size(3);<br>
|                                      ^~<br>
/home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/conv_cpp_bind.cpp:37:38: warning: unused variable ‘kch_i’ [-Wunused-variable]<br>
37 |                                 auto kch_i = weight.size(1);<br>
|                                      ^~~~~<br>
/home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/conv_cpp_bind.cpp: At global scope:<br>
/home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/conv_cpp_bind.cpp:74:16: error: expected constructor, destructor, or type conversion before ‘(’ token<br>
74 | PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {<br>
|                ^<br>
ninja: build stopped: subcommand failed.<br>
Traceback (most recent call last):<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/utils/cpp_extension.py”, line 1673, in _run_ninja_build<br>
env=env)<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/subprocess.py”, line 438, in run<br>
output=stdout, stderr=stderr)<br>
subprocess.CalledProcessError: Command ‘[‘ninja’, ‘-v’]’ returned non-zero exit status 1.</p>
</blockquote>
<p>The above exception was the direct cause of the following exception:</p>
<blockquote>
<p>Blockquote<br>
Traceback (most recent call last):<br>
File “setup.py”, line 10, in <br>
‘build_ext’: BuildExtension<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/setuptools/<strong>init</strong>.py”, line 153, in setup<br>
return distutils.core.setup(**attrs)<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/core.py”, line 148, in setup<br>
dist.run_commands()<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/dist.py”, line 955, in run_commands<br>
self.run_command(cmd)<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/dist.py”, line 974, in run_command<br>
cmd_obj.run()<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/setuptools/command/install.py”, line 67, in run<br>
self.do_egg_install()<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/setuptools/command/install.py”, line 109, in do_egg_install<br>
self.run_command(‘bdist_egg’)<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/cmd.py”, line 313, in run_command<br>
self.distribution.run_command(command)<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/dist.py”, line 974, in run_command<br>
cmd_obj.run()<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/setuptools/command/bdist_egg.py”, line 164, in run<br>
cmd = self.call_command(‘install_lib’, warn_dir=0)<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/setuptools/command/bdist_egg.py”, line 150, in call_command<br>
self.run_command(cmdname)<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/cmd.py”, line 313, in run_command<br>
self.distribution.run_command(command)<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/dist.py”, line 974, in run_command<br>
cmd_obj.run()<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/setuptools/command/install_lib.py”, line 11, in run<br>
self.build()<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/command/install_lib.py”, line 107, in build<br>
self.run_command(‘build_ext’)<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/cmd.py”, line 313, in run_command<br>
self.distribution.run_command(command)<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/dist.py”, line 974, in run_command<br>
cmd_obj.run()<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/setuptools/command/build_ext.py”, line 79, in run<br>
_build_ext.run(self)<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/command/build_ext.py”, line 339, in run<br>
self.build_extensions()<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/utils/cpp_extension.py”, line 708, in build_extensions<br>
build_ext.build_extensions(self)<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/command/build_ext.py”, line 448, in build_extensions<br>
self._build_extensions_serial()<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/command/build_ext.py”, line 473, in _build_extensions_serial<br>
self.build_extension(ext)<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/setuptools/command/build_ext.py”, line 202, in build_extension<br>
_build_ext.build_extension(self, ext)<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/distutils/command/build_ext.py”, line 533, in build_extension<br>
depends=ext.depends)<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/utils/cpp_extension.py”, line 538, in unix_wrap_ninja_compile<br>
with_cuda=with_cuda)<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/utils/cpp_extension.py”, line 1359, in _write_ninja_file_and_compile_objects<br>
error_prefix=‘Error compiling objects for extension’)<br>
File “/home/hatem/anaconda3/envs/bind/lib/python3.6/site-packages/torch/utils/cpp_extension.py”, line 1683, in _run_ninja_build<br>
raise RuntimeError(message) from e<br>
RuntimeError: Error compiling objects for extension</p>
</blockquote>

ptrblck: <p>Your custom extension seems to have trouble here:</p>
<pre><code class="lang-auto">/home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/conv_cpp_bind.cpp: At global scope:
/home/hatem/Projects/pytorch_cpp/custom_conv/cpp_bind/conv-extension/conv_cpp_bind.cpp:74:16: error: expected constructor, destructor, or type conversion before ‘(’ token
</code></pre>
<p>Did you change anything in the code or are you just rebuilding the tutorial?</p>

Hatem: <p>Thank you for your reply <a class="mention" href="/u/ptrblck">@ptrblck</a> . I managed to make it work after your advice. Please delete the post since it is not important for anyone.</p>