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

在AIX系统中使用CMake创建可动态加载共享归档的问题

AIX系统中CMake生成的共享库无法通过dlopen加载的问题

在AIX系统上尝试用CMake创建可动态加载的共享库,库文件能成功生成,但调用dlopen加载时失败。该代码在Linux系统上可正常运行,执行./direct和./dynamic ./libexample.so均能输出"Hello, World!";但在AIX上构建成功后,./direct可正常运行,执行./dynamic ./libexample.a时出现格式错误。

相关代码文件

func.cpp

#include <stdio.h>

extern "C" {
    void func(void) { 
        fprintf(stderr, "Hello, World!\n"); 
    }
}

direct.cpp

#include <stdio.h>

extern "C" {
    void func(void);
}

int main(void) {
    func();
    return 0;
}

dynamic.cpp

#include <dlfcn.h>
#include <stdio.h>

int main(int argc, char** argv) {
    void* lib = dlopen(argv[1], RTLD_LAZY);
    if (!lib) {
        fprintf(stderr, "ERROR: %s\n", dlerror());
        return 1;
    }
    typedef void (*func_t)(void);
    func_t func = (func_t)dlsym(lib, "func");
    if (!func) {
        fprintf(stderr, "ERROR: %s\n", dlerror());
        dlclose(lib);
        return 1;
    }
    func();
    dlclose(lib);

    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.26)

project(example)

if(CMAKE_SYSTEM_NAME STREQUAL "AIX" AND CMAKE_VERSION VERSION_GREATER_EQUAL "3.26" AND CMAKE_VERSION VERSION_LESS "3.27")
    string(REPLACE "objects.exp" "exports.exp" CMAKE_CXX_CREATE_SHARED_LIBRARY "${CMAKE_CXX_CREATE_SHARED_LIBRARY}")
endif()

add_library(example SHARED func.cpp)

add_executable(dynamic dynamic.cpp)

target_link_libraries(dynamic PRIVATE ${CMAKE_DL_LIBS})

add_executable(direct direct.cpp)

target_link_libraries(direct PRIVATE example)

报错信息

执行./dynamic ./libexample.a时出现如下错误:

ERROR:  0509-022 Cannot load module ./libexample.a.
        0509-026 System error: Cannot run a file that does not have a valid format.

参考环境

  • AIX 7.3
  • xlC_r 16.1.0编译器
  • CMake 3.26.4

CMake配置构建输出

$ cmake ..
-- The C compiler identification is XL 16.1.0
-- The CXX compiler identification is XL 16.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /opt/IBM/xlC/16.1.0/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /opt/IBM/xlC/16.1.0/bin/xlC - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (1.2s)
-- Generating done (0.1s)
-- Build files have been written to: /experiment/build_aix

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 13:23:22