Qt 使用 Numcpp(c++ 版的 Numpy)

本文最后更新于:2026年8月29日 晚上

Qt 使用 Boost_Numcpp 库

下载boost_1.75.0 库文件 下载Numcpp: C++ implementation of the Python Numpy library

非中文路径下新建文件夹 D:_Numcpp,将 boost_1.75.0 文件夹中的 boost 文件夹复制进刚刚新建的文件夹中,并将 Numcpp 中的 includes 文件夹中的 NumCpp 和 NumCpp.hpp 复制进刚刚新建的文件夹中(无需添加环境变量)

新建文件夹 -20220208125025

在 Qt 控制台项目的配置文件.pro 中,修改 CONFIG += c++14,并添加库文件路径,配置文件.pro 内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
QT -= gui

CONFIG += c++14 console
CONFIG -= app_bundle

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
main.cpp

TRANSLATIONS += \
Boost_Numcpp_Test_zh_CN.ts

# Boost 库文件路径添加
INCLUDEPATH += D:\Boost_Numcpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
boost::timer tmr;
qDebug() << tmr.elapsed() << "S" << endl;
qDebug() << BOOST_VERSION << endl;
qDebug() << BOOST_LIB_VERSION << endl;
qDebug() << BOOST_PLATFORM << endl;
qDebug() << BOOST_COMPILER << endl;
qDebug() << BOOST_STDLIB << endl;

nc::NdArray<float> m1 = {{1, 2}, {3, 4}};
nc::NdArray<float> m2 = {{1, 2}, {3, 4}};
nc::NdArray<float> m3 = m1 * m2;
std::cout << m3 << std::endl;
return a.exec();

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!