C++函数未导出。

发布于 2025-01-21 04:00:16 字数 2665 浏览 0 评论 0原文

我目前正在尝试定义一个共享库,我的目标是从 Python C++ 扩展以及普通 C++ 应用程序中使用它。

我设法构建了共享库,并尝试将一个简单的 C++ 应用程序链接到它以测试其功能,但共享库的函数之一被链接器视为未定义的引用。在检查了 nm --demangle --dynamic --define-only --extern-only libmylib.so 后,我意识到该函数没有被共享库导出,但我不知道为什么。

该函数的签名如下:

void bootstrap_mylib(std::vector<std::string> python_path,
        std::vector<std::string> python_scripts,
        std::string interface_module,
        std::function<void (pybind11::module_, pybind11::object)> interface_module_initializer);

如果我注释掉最后一个参数,一切都会顺利,因此问题似乎来自于我以某种方式声明与 pybind11 的依赖关系的方式。

以下是我的 CMakeLists.txt 的相关部分:

set(CMAKE_CXX_COMPILER /usr/bin/g++)

project(monilog LANGUAGES CXX VERSION 0.0.1)

set(PYBIND11_PYTHON_VERSION 3.8)
find_package(pybind11 REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})

add_library(mylib SHARED MyLib.cc MyLib.h)
set_property(TARGET mylib PROPERTY CXX_STANDARD 17)

target_link_libraries(mylib ${PYTHON_LIBRARIES})

set_target_properties(mylib PROPERTIES VERSION ${PROJECT_VERSION})
set_target_properties(mylib PROPERTIES SOVERSION 1)
set_target_properties(mylib PROPERTIES PUBLIC_HEADER MyLib.h)

知道我可能做错了什么吗?

编辑:最小工作示例

这是我的问题的一个最小示例,包含以下文件:

example.h

#include <pybind11/stl.h>

namespace Example
{
    void simple_func(std::string some_string);

    void pybind11_func(pybind11::function some_func);
}

example.cc

#include "example.h"

namespace Example
{
    
    void simple_func(std::string some_string)
    {
        std::cout << "Simple function" << '\n';
    }

    void pybind11_func(pybind11::function some_func)
    {
        std::cout << "Pybind11 function" << '\n';
    }
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
cmake_policy(SET CMP0074 NEW)

# SET VARIABLES
set(CMAKE_CXX_COMPILER /usr/bin/g++)
set(CMAKE_CXX_STANDARD 17)

project(example CXX)

set(PYBIND11_PYTHON_VERSION 3.8)
find_package(pybind11 REQUIRED)
# include_directories(${PYTHON_INCLUDE_DIRS})
include_directories(${pybind11_INCLUDE_DIRS})

add_library(example SHARED example.cc example.h)

target_link_libraries(example ${PYTHON_LIBRARIES})

当我构建项目时,如果我在公开的文件中搜索 func符号,我得到以下结果:

> nm -D libexample.so | grep "func"
00000000000039d9 T _ZN7Example11simple_funcENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

因此,pybind11_func 未导出,而 simple_func 已正确导出。

I am currently trying to define a shared library that I aim to use from a Python C++ extension as well as from vanilla C++ applications.

I managed to build the shared library, and tried to link a simple C++ application against it to test its functionalities, but one of the functions of the shared library is treated as an undefined reference by the linker. After checking with nm --demangle --dynamic --defined-only --extern-only libmylib.so, I realized the function is not being exported by the shared library, but I have no idea why.

The function's signature is as follows:

void bootstrap_mylib(std::vector<std::string> python_path,
        std::vector<std::string> python_scripts,
        std::string interface_module,
        std::function<void (pybind11::module_, pybind11::object)> interface_module_initializer);

Everything goes well if I comment out the last parameter, so the problem seems to be coming from the way I declare the dependencies with pybind11 somehow.

Here are the relevant parts of my CMakeLists.txt:

set(CMAKE_CXX_COMPILER /usr/bin/g++)

project(monilog LANGUAGES CXX VERSION 0.0.1)

set(PYBIND11_PYTHON_VERSION 3.8)
find_package(pybind11 REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})

add_library(mylib SHARED MyLib.cc MyLib.h)
set_property(TARGET mylib PROPERTY CXX_STANDARD 17)

target_link_libraries(mylib ${PYTHON_LIBRARIES})

set_target_properties(mylib PROPERTIES VERSION ${PROJECT_VERSION})
set_target_properties(mylib PROPERTIES SOVERSION 1)
set_target_properties(mylib PROPERTIES PUBLIC_HEADER MyLib.h)

Any idea of what I might be doing wrong?

Edit: minimal working example

Here is a minimal example of my problem, consisting of the following files:

example.h

#include <pybind11/stl.h>

namespace Example
{
    void simple_func(std::string some_string);

    void pybind11_func(pybind11::function some_func);
}

example.cc

#include "example.h"

namespace Example
{
    
    void simple_func(std::string some_string)
    {
        std::cout << "Simple function" << '\n';
    }

    void pybind11_func(pybind11::function some_func)
    {
        std::cout << "Pybind11 function" << '\n';
    }
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
cmake_policy(SET CMP0074 NEW)

# SET VARIABLES
set(CMAKE_CXX_COMPILER /usr/bin/g++)
set(CMAKE_CXX_STANDARD 17)

project(example CXX)

set(PYBIND11_PYTHON_VERSION 3.8)
find_package(pybind11 REQUIRED)
# include_directories(${PYTHON_INCLUDE_DIRS})
include_directories(${pybind11_INCLUDE_DIRS})

add_library(example SHARED example.cc example.h)

target_link_libraries(example ${PYTHON_LIBRARIES})

When I build the project, if I then search for func in the exposed symbols, I get the following result:

> nm -D libexample.so | grep "func"
00000000000039d9 T _ZN7Example11simple_funcENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

pybind11_func is thus not exported, while simple_func is correctly exported.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无力看清 2025-01-28 04:00:16

命名空间 pybind11 具有隐藏的可见性。

/usr/include/pybind11/detail/common.h:#    
   define PYBIND11_NAMESPACE pybind11 __attribute__((visibility("hidden")))

因此,与该命名空间相关的所有函数(例如具有其中的参数类型)也被隐藏。

您可以通过显式将可见性设置为默认值来覆盖此设置:

__attribute__((visibility("default"))) 
void bootstrap_mylib( ... )

Namespace pybind11 has hidden visibility.

/usr/include/pybind11/detail/common.h:#    
   define PYBIND11_NAMESPACE pybind11 __attribute__((visibility("hidden")))

so all functions that have anything to do with that namespace (like having an argument type from it) are also hidden.

You can override this by explicitly setting visibility to default:

__attribute__((visibility("default"))) 
void bootstrap_mylib( ... )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文