Windows 使用 Clion 编译 (Python 调用 C++)方法总结

@[TOC](Windows 使用 Clion 编译 (Python 调用 C++)方法总结)

环境

  • Windows 10
  • Clion
  • GCC 8.2.0 (MinGW.org GCC-8.2.0-5)
  • Boost 1.17.0

参考阅读:

安装 GCC (MinGW)

  • 参考文章: Windows环境下MinGW/gcc安装和环境配置
  • 为了提高工作效率,有必要在windows环境下搭建一套gcc,gdb,make环境。
  • MinGW就是windows下gcc的版本。
  • 下载地址 https://sourceforge.net/projects/mingw/files/
  • 进入网址后点击下载mingw-get-setup.exe安装包
  • 打开 MinGW-Installation-Manager 运行界面, gcc,gdb,make相关软件包即可。
  • 要正常使用MinGW,还需要设置环境变量。
    桌面-》鼠标右键单击我的电脑-》属性-》高级-》环境变量
      –将C:\MinGW\bin加入PATH-----------------这是寻找gcc编译器的路径。
      –将C:\MinGW\include加入INCLUDE---------这是Include查找头文件的路径。
      –将C:\MinGW\lib加入LIB---------------------这是标准库存放的路径。
验证GCC, GDB, MAKE安装成功(Windows)

打开CMD在命令提示符下输入gcc –v,看到gcc版本信息,gcc安装OK
打开CMD在命令提示符下输入gdb –v,看到gdb版本信息,gdb安装OK
打开CMD在命令提示符下输入make –v,看到make版本信息,make安装OK

Boost 安装

  • 下载地址 Boost Binaries on Source Forge, 注意需要下载对应的64bits版本(boost_1_71_0-msvc-14.0-64.exe) 千万千万不要弄错,注意 Boost 静态库 / 动态库的命名规则

以 Boost.python 为例,如果编译的是静态库(link=static),将会生成单个 .lib 文件:

libboost_python37-vc142-mt-x64-1_71.lib
而如果编译的是动态库(link=shared),将会生成两个文件(.lib 和 .dll):

boost_python37-vc142-mt-x64-1_71.lib. libboost_python37-vc142-mt-x64-1_71.lib
动态库虽然也生成 .lib 文件,但它与静态库的 .lib 文件差别很大。动态库的 .lib 更像是对 .dll 的声明,二者的关系类似于 .h 与 .cpp 的关系。因此,动态库中的 .lib 文件要比静态库的 .lib 文件小得多。

下面以静态库的命名规则为例进行分析:

libboost_python37-vc142-mt-sgd-x64-1_71.lib
静态库以 lib 开头,动态库开头没有 lib。
所有的库都含有 boost 前缀。

Boost 库名称,本例中为 python37。

编译器名称及其版本,vc142 指的是 msvc-14.2,对应 Visual Studio 2019。

有 mt 代表 threading=multi,没有则代表 threading=single。

有 s 代表 runtime-link=static,没有则代表 runtime-link=shared。

有 gd 代表 debug 版本,没有则代表 release 版本。

目标位数,x32 代表 32 位,x64 代表 64 位。

Boost 库的版本号,1_71 代表 Boost 1.71 版本。

  • 安装完成后,运行 bootstrap.bat(双击即可)
  • 运行完成后,目录下会多出 b2.exe 网上有很多教程都停留在 bjam.exe 版本, 这个是因为 b2.exe 是比较新的版本,两者的用途都一样,如果想知道具体的使用可以运行 PowerShell 并输入
b2.exe --help
  • 在进行 Boost 的编译以前我们需要定义 Python 的版本,默认下会自动搜索并使用 $PATH 中的路径, 只需要将 Boost 根目录下的 tools/build/example/user-config.jam 作为模板复制到 用户的目录下 (C:\Users\xxx\user-config.jam) 即可,下面是 user-config.jam 内容:
# ------------------
# GCC configuration.
# ------------------

# Configure gcc (default version).
# using gcc ;

# Configure specific gcc version, giving alternative name to use.
# using gcc : 3.2 : g++-3.2 ;


# -------------------
# MSVC configuration.
# -------------------

# Configure msvc (default version, searched for in standard locations and PATH).
# using msvc ;

# Configure specific msvc version (searched for in standard locations and PATH).
# using msvc : 8.0 ;


# ----------------------
# Borland configuration.
# ----------------------
# using borland ;


# ----------------------
# STLPort configuration.
# ----------------------

#   Configure specifying location of STLPort headers. Libraries must be either
# not needed or available to the compiler by default.
# using stlport : : /usr/include/stlport ;

# Configure specifying location of both headers and libraries explicitly.
# using stlport : : /usr/include/stlport /usr/lib ;


# -----------------
# QT configuration.
# -----------------

# Configure assuming QTDIR gives the installation prefix.
# using qt ;

# Configure with an explicit installation prefix.
# using qt : /usr/opt/qt ;

# ---------------------
# Python configuration.
# ---------------------

# Configure specific Python version.
using python : 3.7 : "C:/Python37/python.exe" : "C:/Python37/include" : "C:/Python37/Lib" ;

主要是最后一行的 Configure specific Python version, 千万要正确!!.
关于具体的写法: 链接

在完成了此内容以后,在此目录使用 cmd 或 PowerShell (推荐) 运行:

.\b2.exe
.\b2.exe install

大概 5~15 分钟后,编译完成

随后添加一个系统变量,设置 BOOST_ROOT 值为 Boost 的根目录并(D:\Engineering_software\boost_1_71_0)添加到 path 中,
至此 Boost 的安装结束

Clion 的编译与测试

  1. CMakeList.txt
cmake_minimum_required(VERSION 3.16)
project(pythonbinding)

set(CMAKE_CXX_STANDARD 14)


include_directories("C:/Python37/include")
include_directories("D:/Program/boost_1_71_0")

link_directories("C:/Python37/libs")
link_directories("C:/Python37/Lib")
link_directories("D:/Program/boost_1_71_0/stage/lib")
link_directories("D:/Program/boost_1_71_0/lib64-msvc-14.2")

add_library(pythonbinding SHARED hello.cpp)
  1. hello.cpp
#define BOOST_PYTHON_STATIC_LIB
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;


char const* greet(){
    return "hello, world";
}


int Add(const int x, const int y)
{
 return x + y;
}

int Del(const int x, const int y)
{
 return x - y;
}

BOOST_PYTHON_MODULE(hello_ext)
{
    def("greet", greet);
    def("Add", Add);
    def("Del", Del);

}
  • 随后,打开 Settings-> Build, Execution, Deployment -> Toolchains 中设置编译器为 Visual Studio,一般来说无需过多配置,Clion 会自动识别,只需要选择生成编译的架构位数即可,这里选择 amd64

  • 设置完成后,开始编译

  • 定位到编译结果目录,将 pythonbinding.dll 重命名为 hello_ext.pyd,并打开 PowerShell 运行

python
import hello_ext
print(hello_ext.greet())
>> hello, world
>>> print (hello_ext.Add(1,2))
3
>>> print (hello_ext.Del(1,2))
-1

看到结果,就代表成功了。总的来说 Boost Python 就是将 C++ 编译成了可供 Python 运行的库 (dll, so…) ,并将其后缀更改为 .pyd 即可在 Python 中运行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值