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

使用CMake链接mapnik 4.0.0失败,寻求解决方案

解决Mapnik 4.0.0编译链接错误问题

问题描述

安装Mapnik 4.0.0后,编译示例代码时出现链接错误,具体信息如下:

示例代码(map-renderer.cpp)

#include <mapnik/map.hpp>
#include <mapnik/load_map.hpp>
#include <mapnik/agg_renderer.hpp>
#include <mapnik/image.hpp>
#include <mapnik/image_util.hpp>

int main()
{
    mapnik::Map m(256,256);
    mapnik::load_map(m, "path/to/file.xml");
    m.zoom_all();
    mapnik::image_rgba8 im(256,256);
    mapnik::agg_renderer<mapnik::image_rgba8> ren(m, im);
    ren.apply();
    mapnik::save_to_file(im, "the_image.png");
    return 0;
}

CMakeLists.txt内容

cmake_minimum_required(VERSION 3.1.0)

project(MapnikTest)
find_package(mapnik REQUIRED)

add_executable(${PROJECT_NAME} map-renderer.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE mapnik::agg mapnik::mapnik)

编译报错信息

user@user-GL73-8RD:~/Documents/Projects/mapnik-tests/build$ make
[ 50%] Building CXX object CMakeFiles/MapnikTest.dir/map-renderer.cpp.o
[100%] Linking CXX executable MapnikTest
/usr/bin/ld: CMakeFiles/MapnikTest.dir/map-renderer.cpp.o: warning: relocation against `_ZN6mapnik22MAPNIK_GEOGRAPHIC_PROJB5cxx11E' in read-only section `.text'
/usr/bin/ld: CMakeFiles/MapnikTest.dir/map-renderer.cpp.o: in function `main':
map-renderer.cpp:(.text+0x29): undefined reference to `mapnik::MAPNIK_GEOGRAPHIC_PROJ[abi:cxx11]'
/usr/bin/ld: map-renderer.cpp:(.text+0x3e): undefined reference to `mapnik::Map::Map(int, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/MapnikTest.dir/build.make:114: MapnikTest] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/MapnikTest.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

解决方法

  • 统一C++标准与ABI:报错中的[abi:cxx11]表明代码编译的ABI与Mapnik库不匹配。在CMakeLists.txt中强制指定C++标准,确保和Mapnik编译时一致:

    set(CMAKE_CXX_STANDARD 17) # 根据Mapnik实际编译版本调整,如11/14/17
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
  • 补全链接的Mapnik组件:Mapnik 4.0.0的CMake目标可能需要链接更多核心组件,修改target_link_libraries为:

    target_link_libraries(${PROJECT_NAME} PRIVATE mapnik)
    

    或者显式添加所有必要组件:

    target_link_libraries(${PROJECT_NAME} PRIVATE mapnik::mapnik mapnik::agg mapnik::core)
    
  • 验证Mapnik安装的ABI兼容性:

    • 若通过包管理器安装Mapnik,查询其编译时的C++标准;
    • 若源码编译Mapnik,确保编译时指定与项目相同的C++标准:
      cmake .. -DCMAKE_CXX_STANDARD=17
      
  • 显式指定投影(可选):避免依赖默认的MAPNIK_GEOGRAPHIC_PROJ,修改代码中的Map构造:

    mapnik::Map m(256, 256, "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs");
    

内容的提问来源于stack exchange,提问作者syat-cd

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 03:35:21