gpt4 book ai didi

c++ - 包含预编译二进制文件与手动编译二进制文件不同的头文件的正确方法

转载 作者:太空宇宙 更新时间:2023-11-04 10:29:24 24 4
gpt4 key购买 nike

所以我有一个使用 OpenBLAS 的程序,我使用 cmake 来制作它。

问题是我希望它在两种不同情况下顺利编译:

  1. 对于 Linux 系统,例如 Debian,可以使用 apt-get install libopenblas 安装预编译的 libopenblas,这对我的程序来说效果很好。这是因为我有包括:

    #include <openblas/cblas.h>

现在此选项适用于测试而不适用于实际交易。如果与用户编译的 OpenBLAS 版本一起使用,我的程序的性能会更好,因为 OpenBLAS 对每台计算机(取决于处理器)的编译方式不同。但是,如果您想试用该程序,这个预编译版本是不错的选择。

  1. 对于任何人自己编译的版本,OpenBLAS 安装的结构是:

    ├── bin
    ├── include
    │   ├── cblas.h
    │   ├── f77blas.h
    │   ├── lapacke_config.h
    │   ├── lapacke.h
    │   ├── lapacke_mangling.h
    │   ├── lapacke_utils.h
    │   └── openblas_config.h
    └── lib
    ├── libopenblas.a -> libopenblas_haswellp-r0.2.19.a
    ├── libopenblas_haswellp-r0.2.19.a
    ├── libopenblas_haswellp-r0.2.19.so
    ├── libopenblas.so -> libopenblas_haswellp-r0.2.19.so
    └── libopenblas.so.0 -> libopenblas_haswellp-r0.2.19.so

正如您在这里看到的,没有名为“openblas”的目录,这意味着我的包含应该是:

#include <cblas.h>

但这很危险,因为文件 cblas.h 不仅适用于 OpenBLAS,而且可以从 BLAS 的其他实现中获得。这导致文件可用,但我需要的函数 openblas_set_num_threads(int num_threads); 不可用。因此,如果我默认将目录 openblas 添加到我的 cmake 的包含列表中,如果用户没有自定义安装 OpenBLAS,则可能包含错误的文件。

所以我的问题是:您将如何正确处理这个问题,如果 OpenBLAS 的预编译版本不存在,则在没有的情况下执行正确的包含openblas 目录?

如果您需要更多详细信息,请告诉我。

最佳答案

所以我找到的解决方案是在找到本地编译库时在 cmake 中创建一个 DEFINE

if(${foundOpenBLAS})
message("I found OpenBLAS that you compiled.")
TARGET_LINK_LIBRARIES(mylibs -L"${OpenBLASPath}/lib")
else()
message("I couldn't find OpenBLAS that you compiled, so I'm assuming that you have it installed in your system.")
add_definitions(-DOPENBLAS_FROM_SYSTEM)
endif()

现在在 C++ 代码中,我们这样做:

#ifdef OPENBLAS_FROM_SYSTEM
#include <openblas/cblas.h>
#else
#include "include/cblas.h"
#endif

第一个版本是未找到 OpenBLAS 时,它是默认的 Debian 包含。另一种情况是,如果找到 OpenBLAS。这将包括正确的文件。

关于c++ - 包含预编译二进制文件与手动编译二进制文件不同的头文件的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40606212/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com