Skip to content

Commit feb420b

Browse files
committed
简化CMake中的Python链接
1 parent 258e7d8 commit feb420b

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

CMakeLists.txt

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
cmake_minimum_required(VERSION 3.28)
2-
project(untitled11)
2+
project(pybind_test)
33

4+
#************************************基础配置***********************************************************
45
# 添加子目录 pybind11
56
add_subdirectory(extern/pybind11)
67

7-
# 加入cpp文件
8-
pybind11_add_module(example SHARED example.cpp)
9-
10-
# 设置输出文件后缀名为 .pyd
11-
set_target_properties(example PROPERTIES SUFFIX ".pyd")
12-
138
# 添加包含目录
149
include_directories(
1510
"E:\\DataBuffer\\hurleykane\\miniconda3\\include"
@@ -22,8 +17,14 @@ find_library(PYTHON3_LIB python3 PATHS ${PYTHON_LIB_DIR})
2217
find_library(PYTHON311_LIB python311 PATHS ${PYTHON_LIB_DIR})
2318
link_directories(${PTTHON_LIB_DIR})
2419

25-
# 添加依赖项
26-
target_link_libraries(example PUBLIC
27-
${PYTHON3_LIB}
28-
${PYTHON311_LIB}
29-
)
20+
# 添加依赖项函数
21+
function(link_to_python target_module)
22+
target_link_libraries(${target_module} PUBLIC
23+
${PYTHON3_LIB}
24+
${PYTHON311_LIB}
25+
)
26+
endfunction()
27+
28+
#******************************访问子文件CMakeLists.txt*********************************************
29+
add_subdirectory(core/add)
30+

core/add/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# 加入cpp文件
2+
pybind11_add_module(add SHARED add.cpp)
3+
4+
# 设置输出文件后缀名为 .pyd
5+
set_target_properties(
6+
add PROPERTIES SUFFIX ".pyd"
7+
)
8+
9+
# 添加依赖项
10+
link_to_python(add)
File renamed without changes.

0 commit comments

Comments
 (0)