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

使用C++读取Apache Arrow文件时遇未定义引用错误求助

Apache Arrow C++读取文件时未定义引用错误解决

错误信息

链接阶段出现以下未定义引用错误:

read_arrow.cpp:22: undefined reference to `__imp__ZN5arrow19default_memory_poolEv'

代码实现

#include <iostream>
#include <memory>
#include <string>

#include <arrow/io/api.h>
#include <arrow/ipc/api.h>
#include <arrow/api.h>
#include <arrow/csv/api.h>
#include <parquet/arrow/reader.h>
#include <parquet/arrow/writer.h>
#include <arrow/status.h>

using namespace arrow;

arrow::Status GenInitialFile()
{

  std::shared_ptr<arrow::io::ReadableFile> file;
  const std::string file_path = "userdata_to_arrow.arrow";
  ARROW_ASSIGN_OR_RAISE(file, arrow::io::ReadableFile::Open(file_path, arrow::default_memory_pool()));

  std::shared_ptr<arrow::ipc::RecordBatchFileReader> reader;
  ARROW_ASSIGN_OR_RAISE(auto ipc_reader, arrow::ipc::RecordBatchFileReader::Open(file));

  std::shared_ptr<arrow::RecordBatch> record_batch;
  arrow::Status status;
  while (true)
  {
    std::shared_ptr<arrow::RecordBatch> rbatch;
    ARROW_ASSIGN_OR_RAISE(rbatch, ipc_reader->ReadRecordBatch(0));
    std::cout << "Read record batch with " << record_batch->num_rows() << " rows." << std::endl;
  }

  return arrow::Status::OK();
}

int main()
{
  
  arrow::Status st = GenInitialFile();

  if (!st.ok())
  {
    std::cerr << st << std::endl;
    return 1;
  }

  return 0;
}

当前c_cpp_properties.json配置

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "C:\\msys64\\mingw64\\**",
                "${vcpkgRoot}/x86-windows/include",
                "${default}",
                "C:\\msys64\\mingw64\\include\\arrow",
                "C:\\msys64\\mingw64\\include\\arrow-flight-glib",
                "C:\\msys64\\mingw64\\bin",
                "C:\\msys64\\mingw64\\include\\arrow-flight-sql-glib",
                "C:\\msys64\\mingw64\\include\\arrow-flight-glib",
                "C:\\msys64\\mingw64\\include\\arrow-glib",
                "C:\\msys64\\mingw64\\include\\arrow-dataset-glib",
                "C:\\msys64\\mingw64\\include\\grpc",
                "C:\\msys64\\mingw64\\include\\grpc++"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.19041.0",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "${default}",
            "compilerPath": "C:\\msys64\\mingw64\\bin\\g++.exe"
        }
    ],
    "version": 4
}

问题分析与解决

这个错误属于链接阶段错误:编译时能找到头文件(所以没有编译错误),但链接器找不到arrow::default_memory_pool()对应的库实现。你的c_cpp_properties.json仅配置了IntelliSense的头文件路径,并未设置编译链接所需的库链接参数。

解决步骤

  1. 配置tasks.json添加链接参数
    在VS Code中创建或修改.vscode/tasks.json,添加Arrow相关库的链接选项:

    {
      "version": "2.0.0",
      "tasks": [
        {
          "type": "cppbuild",
          "label": "C/C++: g++.exe 生成活动文件",
          "command": "C:\\msys64\\mingw64\\bin\\g++.exe",
          "args": [
            "-fdiagnostics-color=always",
            "-g",
            "${file}",
            "-o",
            "${fileDirname}\\${fileBasenameNoExtension}.exe",
            // 链接Arrow核心库、IPC库和Parquet库
            "-larrow",
            "-larrow_ipc",
            "-lparquet",
            // 如果库不在默认搜索路径,添加库目录
            "-LC:\\msys64\\mingw64\\lib"
          ],
          "options": {
            "cwd": "${fileDirname}"
          },
          "problemMatcher": ["$gcc"],
          "group": {
            "kind": "build",
            "isDefault": true
          },
          "detail": "编译器: C:\\msys64\\mingw64\\bin\\g++.exe"
        }
      ]
    }
    
  2. 验证库兼容性
    确保你的Apache Arrow库是用Mingw编译的——Windows下MSVC和Mingw生成的库无法交叉链接,你当前使用Mingw的g++编译器,必须对应使用Mingw编译的Arrow库。

  3. 修复代码中的潜在问题
    你的代码存在两处问题:

    • 循环中record_batch未赋值,应使用读取到的rbatch;
    • 死循环会重复读取第0个batch,需改为遍历所有batch:
      int batch_idx = 0;
      while (true) {
        std::shared_ptr<arrow::RecordBatch> rbatch;
        auto status = ipc_reader->ReadRecordBatch(batch_idx, &rbatch);
        if (!status.ok()) {
          if (status.IsEndOfFile()) {
            break; // 读取完毕
          }
          return status;
        }
        std::cout << "Read record batch " << batch_idx << " with " << rbatch->num_rows() << " rows." << std::endl;
        batch_idx++;
      }
      

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 14:49:51