You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编译使用Assimp时无法找到utf8.h头文件的问题求助

问题描述

在Manjaro Linux x86_64(内核6.2.6-1)系统上从源码编译安装了静态库版本的Assimp,编写测试代码加载STL文件时,编译提示无法找到assimp/types.h引用的utf8.h头文件。该文件位于Assimp源码的contrib目录,但编译库时无论设置何种CMake参数,contrib及utf8.h都未被复制到系统include目录。

测试代码如下:

#include <assimp/Importer.hpp>
#include <assimp/postprocess.h>
#include <assimp/scene.h>

#include <iostream>
#include <memory>

#include <Eigen/Core>

#include <fcl/geometry/bvh/BVH_model.h>

std::shared_ptr<fcl::BVHModel<fcl::OBBRSSd>> loadSTL(const std::string &filename);

int main()
{
    // Load the STL mesh files
    std::shared_ptr<fcl::BVHModel<fcl::OBBRSSd>> Skeleton, Liver, Lungs;

    Skeleton = loadSTL("Skeleton.stl");
    Liver = loadSTL("Liver.stl");
    Lungs = loadSTL("Lungs.stl");

    return 0;
}

// Load an STL file into an FCL mesh object
std::shared_ptr<fcl::BVHModel<fcl::OBBRSSd>> loadSTL(const std::string &filename)
{
    // Create an Assimp importer
    Assimp::Importer importer;

    // Import the STL file -- "aiProcess_Triangulate" post-processing flag automatically triangulates all non-triangular faces during the import process
    const aiScene *scene = importer.ReadFile(filename, aiProcess_Triangulate);

    // Check if the import was successful
    if (!scene)
    {
        std::cerr << "Failed to import STL file: " << importer.GetErrorString() << std::endl;
        return nullptr;
    }

    // Extract the mesh from the scene
    const aiMesh *mesh = scene->mMeshes[0];

    // Create a BVHModel from the mesh
    auto model = std::make_shared<fcl::BVHModel<fcl::OBBRSSd>>();
    model->beginModel();

    for (size_t i = 0UL; i < mesh->mNumFaces; ++i)
    {
        const aiFace &face = mesh->mFaces[i];
        if (face.mNumIndices != 3)
        {
            std::cerr << "Error: non-triangular face in STL file." << std::endl;
            model->endModel();
            return nullptr;
        }

        fcl::Vector3d v1(mesh->mVertices[face.mIndices[0UL]].x, mesh->mVertices[face.mIndices[0UL]].y, mesh->mVertices[face.mIndices[0UL]].z);
        fcl::Vector3d v2(mesh->mVertices[face.mIndices[1UL]].x, mesh->mVertices[face.mIndices[1UL]].y, mesh->mVertices[face.mIndices[1UL]].z);
        fcl::Vector3d v3(mesh->mVertices[face.mIndices[2UL]].x, mesh->mVertices[face.mIndices[2UL]].y, mesh->mVertices[face.mIndices[2UL]].z);

        model->addTriangle(v1, v2, v3);
    }

    model->endModel();

    // Clean up the Assimp data structures
    importer.FreeScene();

    return model;
}
解决方案

1. 重新编译Assimp时开启contrib头文件安装

Assimp的CMake配置默认关闭了contrib目录的安装,需要手动开启ASSIMP_INSTALL_CONTRIB选项:

  • 进入Assimp源码目录,创建并进入build目录:
mkdir build && cd build
  • 执行CMake配置,开启contrib安装并指定静态库编译:
cmake -DASSIMP_INSTALL_CONTRIB=ON -DBUILD_SHARED_LIBS=OFF ..
  • 编译并安装:
make -j$(nproc)
sudo make install

完成后,utf8.h会被安装到/usr/include/assimp/contrib目录,满足编译时的头文件引用需求。

2. 手动复制contrib目录(临时方案)

如果不想重新编译,可直接将Assimp源码中的contrib目录复制到系统Assimp的include路径:

sudo cp -r /path/to/assimp-source/contrib /usr/include/assimp/

替换/path/to/assimp-source为你实际的Assimp源码存放路径。

原因说明

Assimp的contrib目录包含第三方依赖的头文件(如utf8cpp的utf8.h),默认CMake配置不会将这些文件安装到系统目录,仅当启用ASSIMP_INSTALL_CONTRIB选项时,才会自动完成安装。当代码触发了需要字符编码处理的逻辑时(如STL文件的元数据解析),就会出现utf8.h缺失的编译错误。

内容的提问来源于stack exchange,提问作者Filipe Pedrosa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 04:45:15