Skip to content

Commit fccbda9

Browse files
committed
添加Python打包方法
1 parent f81da11 commit fccbda9

17 files changed

+165
-42
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
/extern
22
/cmake-build-release-visual-studio
3-
/.idea
3+
/cmake-build-debug-visual-studio
4+
/.idea
5+
/src/build

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
cmake_minimum_required(VERSION 3.28)
22
project(pybind_test)
33

4+
set(CMAKE_EXE_LINKER_FLAGS "-static") # 解决exe文件无法打开的错误
5+
46
#************************************pybind基础配置***********************************************************
57
# 添加子目录 pybind11
68
add_subdirectory(extern/pybind11)
@@ -25,5 +27,9 @@ function(link_to_python target_module)
2527
)
2628
endfunction()
2729

30+
# 添加字符设置
31+
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
32+
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
33+
2834
#******************************访问pybind子文件CMakeLists.txt*********************************************
2935
add_subdirectory(src)

include/class_cat.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// Created by HurleyKane on 2024/5/8.
3+
//
4+
5+
#ifndef PYBIND_TEST_CLASS_CAT_H
6+
#define PYBIND_TEST_CLASS_CAT_H
7+
#include <iostream>
8+
class test11 {
9+
public:
10+
explicit test11(std::string& name);
11+
void setName(const std::string& name_);
12+
const std::string& getName();
13+
private:
14+
std::string name;
15+
};
16+
17+
#endif //PYBIND_TEST_CLASS_CAT_H

include/struct_test.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
//
2-
// Created by HurleyKane on 2024/5/8.
3-
//
4-
51
#ifndef PYBIND_TEST_STRUCT_TEST_H
62
#define PYBIND_TEST_STRUCT_TEST_H
73

84
#include <string>
9-
struct Pet{
10-
Pet(const std::string& name);
11-
void setName(const std::string& name_);
12-
const std::string& getName();
5+
6+
struct Pet {
137
std::string name;
8+
9+
// 构造函数,使用 std::move 来优化字符串的移动
10+
explicit Pet(std::string& name);
11+
12+
// 设置宠物的名字
13+
void setName(const std::string& name_);
14+
15+
// 获取宠物的名字
16+
const std::string& getName() const;
1417
};
15-
#endif //PYBIND_TEST_STRUCT_TEST_H
18+
19+
#endif // PYBIND_TEST_STRUCT_TEST_H

src/CMakeLists.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
# 测试文件
2+
add_executable(main main.cpp function_add.cpp)
3+
target_include_directories(main PRIVATE ${PROJECT_SOURCE_DIR}/include)
4+
15
# 加入cpp文件
2-
pybind11_add_module(function SHARED binding.cpp add.cpp struct_test.cpp)
3-
# 添加头文件目录
6+
file(GLOB SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp") # 找出改文件下所有cpp文件
7+
pybind11_add_module(function SHARED ${SOURCE_FILES})
48
target_include_directories(function PRIVATE ${PROJECT_SOURCE_DIR}/include)
9+
510
# 添加依赖项
6-
link_to_python(function)
11+
link_to_python(function)

src/README

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# 提供了两种打包方式的对比
2+
3+
setup.py : 为Python利用pybind对cpp文件进行打包的示例
4+
setup_pybind.py : 为setuptool + pybind模块 进行打包的示例
5+
6+
CMakelists.txt : 为C++中CMake对cpp文件打包的示例

src/binding.cpp

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/class_cat.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//
2+
// Created by HurleyKane on 2024/5/8.
3+
//
4+
#include <class_cat.h>
5+
#include <string>
6+
7+
test11::test11(std::string &name) : name(name) {}
8+
9+
void test11::setName(const std::string &name_) { name = name_;}
10+
11+
const std::string &test11::getName() {return name;}

src/function.cp311-win_amd64.pyd

139 KB
Binary file not shown.

src/function.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def add(i, j):
2+
pass
3+
4+
class Pet:
5+
"""
6+
# test
7+
"""
8+
def __init__(self, name):
9+
self.name = name
10+
pass
11+
pass

0 commit comments

Comments
 (0)