GithubHelp home page GithubHelp logo

smrfeld / cmake_cpp_pybind11_tutorial Goto Github PK

View Code? Open in Web Editor NEW
119.0 119.0 22.0 623 KB

Tutorial for wrapping C++ library into Python using pybind11 and CMake

CMake 60.70% C++ 36.66% Python 2.64%

cmake_cpp_pybind11_tutorial's People

Contributors

fornof avatar smrfeld avatar tengomucho avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

cmake_cpp_pybind11_tutorial's Issues

use pybind11 package in VSCODE

Hello, @smrfeld ,Thanks for sharing this pybind11-cmake tutorial! Actually, This is not a issue but a request.

I am a beginner of pybind11 and this project helps me a lot. However, I am wondering how to let VSCODE automatically highlight pybind package like ordinary python pacakge, as well as automatically complete args and function names. Should I add py.in files?

Setting up the Python wrapper cmake error

I'm having the following error while I try to run make at the setting up the Python wrapper section. May I have some help on finding out what is missing?
The output is pasted below.

(base) asanci@asanci:~/python/build$ make
Scanning dependencies of target automobile
make[2]: *** No rule to make target 'CMakeFiles/automobile.dir/build'.  Stop.
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/automobile.dir/all' failed
make[1]: *** [CMakeFiles/automobile.dir/all] Error 2
Makefile:129: recipe for target 'all' failed
make: *** [all] Error 2`

Build fails with multiple errors

The configure step runs without error. The build has so many errors, and such odd ones, that my first guess is that I need to update something. But what?

Python 3.11, Visual Studio 17.6.3.

Would you like me to try the latest pybind11.h before you look into this?


C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial>cmake --build build
MSBuild version 17.6.3+07e294721 for .NET Framework

Checking Build System
Building Custom Rule C:/Users/beryl/source/repos/cmake_cpp_pybind11_tutorial/CMakeLists.txt
cl : command line warning D9002: ignoring unknown option '-O3' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutoria
l\build\automobile.vcxproj]
motorcycle.cpp
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\ostream(774,1): warning C4530
: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc [C:\Users\beryl\source\repos\cmake_cp
p_pybind11_tutorial\build\automobile.vcxproj]
C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\cpp\src\motorcycle.cpp(16,15): message : see reference to funct
ion template instantiation 'std::basic_ostream<char,std::char_traits> &std::operator <<<std::char_traits>(s
td::basic_ostream<char,std::char_traits> &,const char *)' being compiled [C:\Users\beryl\source\repos\cmake_cpp_p
ybind11_tutorial\build\automobile.vcxproj]
cl : command line warning D9002: ignoring unknown option '-O3' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutoria
l\build\automobile.vcxproj]
automobile.cpp
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\xmemory(154,5): error C2039:
'_invalid_parameter': is not a member of 'global namespace'' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\ build\automobile.vcxproj] C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\xmemory(154,5): error C3861: '_invalid_parameter': identifier not found [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build\automobile.vc xproj] C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\xmemory(164,5): error C2039: '_invalid_parameter': is not a member of 'global namespace'' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial
build\automobile.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\xmemory(164,5): error C3861:
'_invalid_parameter': identifier not found [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build\automobile.vc
xproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\xmemory(1330,9): error C2039:
'invalid_parameter': is not a member of 'global namespace'' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial \build\automobile.vcxproj] C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\xmemory(1330,1): error C3861: '_invalid_parameter': identifier not found [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build\automobile.v cxproj] C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\xpolymorphic_allocator.h(135, 13): error C2039: '_invalid_parameter': is not a member of 'global namespace'' [C:\Users\beryl\source\repos\cmake_cpp
pybind11_tutorial\build\automobile.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\xpolymorphic_allocator.h(135,
1): error C3861: '_invalid_parameter': identifier not found [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\bu
ild\automobile.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\xpolymorphic_allocator.h(142,
13): error C2039: 'invalid_parameter': is not a member of '`global namespace'' [C:\Users\beryl\source\repos\cmake_cpp
pybind11_tutorial\build\automobile.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\xpolymorphic_allocator.h(142,
1): error C3861: '_invalid_parameter': identifier not found [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\bu
ild\automobile.vcxproj]
C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build_deps\pybind11-src\include\pybind11\detail/internals.h(18
7,1): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc [C:\Users\beryl\so
urce\repos\cmake_cpp_pybind11_tutorial\build\automobile.vcxproj]
C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build_deps\pybind11-src\include\pybind11\cast.h(442,36): error
C2027: use of undefined type '_frame' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build\automobile.vcxpro
j]
c:\Users\beryl\AppData\Local\Programs\Python\Python311\include\pytypedefs.h(22,16): message : see declaration of '_fram
e' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build\automobile.vcxproj]
C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build_deps\pybind11-src\include\pybind11\cast.h(444,29): error
C2027: use of undefined type '_frame' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build\automobile.vcxpro
j]
c:\Users\beryl\AppData\Local\Programs\Python\Python311\include\pytypedefs.h(22,16): message : see declaration of '_fram
e' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build\automobile.vcxproj]
C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build_deps\pybind11-src\include\pybind11\cast.h(445,26): error
C2027: use of undefined type '_frame' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build\automobile.vcxpro
j]
c:\Users\beryl\AppData\Local\Programs\Python\Python311\include\pytypedefs.h(22,16): message : see declaration of '_fram
e' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build\automobile.vcxproj]
C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build_deps\pybind11-src\include\pybind11/pybind11.h(2032,49):
error C2039: 'frame': is not a member of 'ts' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build\automobil
e.vcxproj]
c:\Users\beryl\AppData\Local\Programs\Python\Python311\include\cpython/pystate.h(82,8): message : see declaration of '

ts' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build\automobile.vcxproj]
C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build_deps\pybind11-src\include\pybind11/pybind11.h(2033): err
or C2027: use of undefined type '_frame' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build\automobile.vcxp
roj]
c:\Users\beryl\AppData\Local\Programs\Python\Python311\include\pytypedefs.h(22,16): message : see declaration of '_fram
e' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build\automobile.vcxproj]
C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build_deps\pybind11-src\include\pybind11/pybind11.h(2034,1): e
rror C2027: use of undefined type '_frame' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build\automobile.vc
xproj]
c:\Users\beryl\AppData\Local\Programs\Python\Python311\include\pytypedefs.h(22,16): message : see declaration of '_fram
e' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build\automobile.vcxproj]
C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build_deps\pybind11-src\include\pybind11/pybind11.h(2037,18):
error C2027: use of undefined type '_frame' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build\automobile.v
cxproj]
c:\Users\beryl\AppData\Local\Programs\Python\Python311\include\pytypedefs.h(22,16): message : see declaration of '_fram
e' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build\automobile.vcxproj]
C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build_deps\pybind11-src\include\pybind11/pybind11.h(2037,30):
error C2027: use of undefined type '_frame' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build\automobile.v
cxproj]
c:\Users\beryl\AppData\Local\Programs\Python\Python311\include\pytypedefs.h(22,16): message : see declaration of '_fram
e' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build\automobile.vcxproj]
C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build_deps\pybind11-src\include\pybind11/pybind11.h(2036,33):
error C2660: 'PyDict_GetItem': function does not take 1 arguments [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutor
ial\build\automobile.vcxproj]
c:\Users\beryl\AppData\Local\Programs\Python\Python311\include\dictobject.h(22,24): message : see declaration of 'PyDic
t_GetItem' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build\automobile.vcxproj]
C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build_deps\pybind11-src\include\pybind11/pybind11.h(2036,33):
message : while trying to match the argument list '()' [C:\Users\beryl\source\repos\cmake_cpp_pybind11_tutorial\build\a
utomobile.vcxproj]

suggestion

In the step "Testing the C++ library", the current version of your code throws an error because "test" is a reserved word while running make on Ubuntu. The issue is resolved after I changed the test names to tests. You may like to update naming with a safer word.

Building with make

I am learning this for the first time, hence my use of this tutorial, so this is likely a result of my inexperience. But maybe someone else has the same issue..

I am running cmake 3.19.7 and GNU Make 3.81 on macOS 10.14.6.

When I've run cmake .. from the cpp/build directory i get the output

-- Configuring done
-- Generating done
-- Build files have been written to: /Users/(...)/pybind_tutorial/cpp/build

Then, when i run make, i get make: *** No targets specified and no makefile found. Stop.
Running make install returns make: *** No rule to make target 'install'. Stop..

My project structure is as described in the tutorial:

cpp/build
cpp/CMakeLists.txt
cpp/include/automobile
cpp/include/automobile_bits/motorcycle.hpp
cpp/src/motorcycle.cpp

After running the cmake .. command, the cpp/build directory holds the files

build.ninja
cmake_install.cmake
CMakeCache.txt
compile_commands.json
CPackConfig.cmake
CPackSourceConfig.cmake
CTestTestfile.cmake
DartConfiguration.tcl

The cpp/build/CMakeFiles directory contains

cmake.check_cache
CMakeOutput.log
rules.ninja
TargetDirectories.txt
automobile.dir/src (empty directory)
3.19.7 (what looks like standard version-specific cmake files)

As said, I feel like this is a product of my inexperience, but any help getting this to work would be greatly appreciated.

Edit:
Following this tutorial worked, although i can't seem to understand why that worked and not your tutorial, I'm guessing it has something to do with the different CMakeLists.txt files? I prefer written tutorials to videos, but that video is the only thing I've come across in over a day that ended up working for me.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.