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

C++嵌入Python项目编译出现Py_DecodeLocale等未定义引用问题求助

问题

将Python嵌入C++项目时,使用官方示例代码,CMake日志显示已找到Python相关组件,但编译时出现Py_DecodeLocale、Py_SetProgramName等函数的未定义引用错误。相关配置及信息如下:

CMakeLists.txt

cmake_minimum_required(VERSION 3.20)
project(python_in_cpp)

set(CMAKE_CXX_STANDARD 11)

# sudo apt install libpython3.11-dev

find_package(PythonLibs 3.11 REQUIRED)

find_package(Python COMPONENTS Interpreter Development)

message("Python_FOUND:${Python_FOUND}")
message("Python_VERSION:${Python_VERSION}")
message("Python_Development_FOUND:${Python_Development_FOUND}")
message("Python_LIBRARIES:${Python_LIBRARIES}")

include_directories(${Python_INCLUDE_DIRS})

link_directories(
        ${Python3_LIBRARY_DIRS}
        ${Python3_RUNTIME_LIBRARY_DIRS}
)

link_libraries(${Python3_LIBRARIES})

add_executable(python_in_cpp main.cpp)

main.cpp

#define PY_SSIZE_T_CLEAN
#include <Python.h>

int
main(int argc, char *argv[])
{
    wchar_t *program = Py_DecodeLocale(argv[0], NULL);
    if (program == NULL) {
        fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
        exit(1);
    }
    Py_SetProgramName(program);  /* optional but recommended */
    Py_Initialize();
    PyRun_SimpleString("from time import time,ctime\n"
                       "print('Today is', ctime(time()))\n");
    if (Py_FinalizeEx() < 0) {
        exit(120);
    }
    PyMem_RawFree(program);
    return 0;
}

编译错误信息

main.cpp:(.text+0x1f): undefined reference to `Py_DecodeLocale'
/usr/bin/ld: main.cpp:(.text+0x63): undefined reference to `Py_SetProgramName'
/usr/bin/ld: main.cpp:(.text+0x68): undefined reference to `Py_Initialize'
/usr/bin/ld: main.cpp:(.text+0x7c): undefined reference to `PyRun_SimpleStringFlags'
/usr/bin/ld: main.cpp:(.text+0x81): undefined reference to `Py_FinalizeEx'
/usr/bin/ld: main.cpp:(.text+0x9e): undefined reference to `PyMem_RawFree'

已安装的Python库

$ apt list --installed | grep "lib*python"

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

libpython3-dev/stable,now 3.11.2-1+b1 amd64 [installed]
libpython3-stdlib/stable,now 3.11.2-1+b1 amd64 [installed,automatic]
libpython3.11-dev/stable-security,now 3.11.2-6+deb12u3 amd64 [installed]
libpython3.11-minimal/stable-security,now 3.11.2-6+deb12u3 amd64 [installed,automatic]
libpython3.11-stdlib/stable-security,now 3.11.2-6+deb12u3 amd64 [installed,automatic]
libpython3.11/stable-security,now 3.11.2-6+deb12u3 amd64 [installed,automatic]

解决方案

1. 修复CMake配置(核心修改)

问题根源在于混用了旧的PythonLibs模块和新的Python模块,且变量引用不匹配。替换为CMake推荐的现代配置:

cmake_minimum_required(VERSION 3.20)
project(python_in_cpp)

set(CMAKE_CXX_STANDARD 11)

# 仅使用新的Python模块,锁定3.11版本的开发组件
find_package(Python 3.11 REQUIRED COMPONENTS Interpreter Development)

# 可选:打印调试信息确认配置
message("Python_FOUND: ${Python_FOUND}")
message("Python_VERSION: ${Python_VERSION}")
message("Python_INCLUDE_DIRS: ${Python_INCLUDE_DIRS}")
message("Python_LIBRARIES: ${Python_LIBRARIES}")

# 为目标添加包含目录和链接库(现代CMake写法,避免全局污染)
add_executable(python_in_cpp main.cpp)
target_include_directories(python_in_cpp PRIVATE ${Python_INCLUDE_DIRS})
target_link_libraries(python_in_cpp PRIVATE ${Python_LIBRARIES})

2. 修改说明

  • 移除旧的find_package(PythonLibs)命令,统一使用CMake 3.14+支持的Python模块,确保版本匹配且变量一致。
  • 用target_include_directories和target_link_libraries替代全局命令,确保仅为当前可执行文件配置依赖,避免项目全局污染。
  • 变量统一使用Python_前缀(而非Python3_),新模块输出的变量不带版本号后缀。

3. 验证编译

  1. 清空旧构建目录:rm -rf build && mkdir build && cd build
  2. 重新生成构建文件:cmake ..
  3. 编译项目:make

修改后链接器将正确关联Python库,解决未定义引用错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 23:52:01