【问题标题】:C++ Boost Library Undefined Reference On shm_open: DSO Missing From Command Lineshm_open 上的 C++ Boost 库未定义参考:命令行中缺少 DSO
【发布时间】:2015-11-05 07:11:31
【问题描述】:

我正在编写一个应用程序,它在 DDS 系统中扮演订阅者的角色。此订阅者将在收到数据后将值写入共享内存位置,以便其他 GUI 应用程序可以读取它们。

我已经编写了一个可以在 Windows 上编译和运行 A-OK 的应用程序版本,但我现在面临创建一个要在 Ubuntu 上运行的版本。作为 Windows 原生,这对我来说很麻烦。

为了创建 Ubuntu 版本,我已将代码库移植到 Ubuntu 虚拟机,现在正尝试在那里编译它,这是我遇到问题的地方。

在大多数情况下,一切都可以正常编译,并且该过程与 Windows 上的过程非常相似。不幸的是,链接我所有生成的 .o 文件的最后一条语句失败了。下面略作说明:

我正在使用 boost 进程间库来编写一个头文件,该文件将提供共享内存读/写操作。实现这一点的.cpp如下:MemWriterH.cpp

#define BOOST_DATE_TIME_NO_LIB
#define CPP

#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <stdio.h>
#include <MemWriterH.h>

using namespace boost::interprocess;

extern "C" int writeToSharedMemory(char *data)
{
    //Remove shared memory on construction and destruction
    struct shm_remove
    {
        shm_remove()
        {
            //shared_memory_object::remove("MySharedMemory");
        }
        ~shm_remove()
        {
            //shared_memory_object::remove("MySharedMemory");
        }
    }remover;

    //Create a shared memory object.
    shared_memory_object shm(open_or_create, "MySharedMemory", read_write);
    //Set size
    shm.truncate(1000);

    //Map the whole shared memory in this process
    mapped_region region(shm, read_write);

    //Write given data to the mapped region
    //std::memset(region.get_address(), 'h', region.get_size());
    std::memcpy(region.get_address(), data, region.get_size());

    return 0;
}

extern "C" void readFromSharedMemeory(char toWriteIn[])
{
    //Open already created shared memory object.
    shared_memory_object shm(open_only, "MySharedMemory", read_only);

    //Map the whole shared memory in this process
    mapped_region region(shm, read_only);

    //Read data from shared memory into parameter
    //char *mem = static_cast<char*>(region.get_address());

    std::memcpy(toWriteIn, region.get_address(), region.get_size());
    printf("Read data OK");
    printf("\n%s", toWriteIn);
}

及其随附的标头:MemWriterH.h

#ifdef CPP
    extern "C" int writeToSharedMemory(char *);
    extern "C" void readFromSharedMemeory(char *);
#else
    int writeToSharedMemory( char *);
    void readFromSharedMemeory(char* toWriteIn);
#endif

然后使用以下代码将它们编译为 .o 文件:

g++ -c -I/$HOME/workspace_c/HID_LDM_DDS/HID_LDM_DDS_INCLUDES/MEM_MAPPING/ *.cpp

在应用程序的主文件中,我有一个#include,它允许使用上述功能。

虽然此应用程序还有各种其他元素(如下所示),但这些元素似乎都可以正常编译、构建和链接。

生成的 MemWriterH.o 文件随后用于最终的 GCC 语句——我相信该语句将所有 .o 文件链接在一起以生成应用程序的可执行文件。最后的声明是:

gcc HID_LDM_DDS_SUB.o $HOME/workspace_c/HID_LDM_DDS/HID_LDM_DDS_HELPER/HID_LDM_DDS_HELPER.o $HOME/workspace_c/HID_LDM_DDS/HID_LDM_DDS_INCLUDES/CheckStatus.o $HOME/workspace_c/HID_LDM_DDS/HID_LDM_DDS_INCLUDES/MEM_MAPPING/MemWriterH.o $HOME/workspace_c/HID_LDM_DDS/HID_LDM_DDS_IDL/LDMSacDcps.o $HOME/workspace_c/HID_LDM_DDS/HID_LDM_DDS_IDL/LDMSplDcps.o $OSPL_HOME/lib/libddskernel.so $OSPL_HOME/lib/libdcpssac.so

不幸的是,这给了我错误:

/usr/bin/ld: /home/bob/workspace_c/HID_LDM_DDS/HID_LDM_DDS_INCLUDES/MEM_MAPPING/MemWriterH.o: 对符号“shm_open@@GLIBC_2.2”的未定义引用

/lib/i386-linux-gnu/librt.so.1:添加符号时出错:DSO 缺失 命令行 collect2:错误:ld 返回 1 个退出状态

在这一点上,我感到完全迷失了。 .cpp 编译为 .o 没有任何抱怨,所以我不太确定从这里可以去哪里。什么是 DSO,如何将其添加到命令行?

编辑:

现在问题解决了。正如下面的Sehe 所指出的,缺少定义 shm_open 的库。这包含在 -lrt 标志中,应该包含在被链接的 .o 文件之后 (see here for more explanation on that)。

然而,解决这个问题确实导致了另一个问题,即编译器在多次出现错误时抱怨:“未定义对 std::some_function 的引用”。事实证明,这是由于使用 gcc 命令链接 C 和 C++ 代码(标头)时出现的问题。添加 -lstdc++ 标志,或使用 g++ 编译命令 (see the answer here for a description of what's going on) 也可以解决此问题,并且所有编译都成功。

【问题讨论】:

    标签: c++ linux ubuntu gcc boost


    【解决方案1】:

    您应该完全按照消息的提示添加缺少的库:链接器选项末尾的-lrt 就足够了

    【讨论】:

    • 谢谢!是的,我昨天搬过去了,谢谢。然而,这确实导致了另一个问题,即编译器在几个错误实例中抱怨:“未定义对 std::some_function 的引用”。事实证明,这是由于使用 gcc 命令链接 C 和 C++ 代码(标头)时出现的问题。添加 -lstdc++ 标志,或使用 g++ 编译命令(see the answer here 用于说明发生了什么)也解决了这个问题,并且所有编译成功。
    猜你喜欢
    • 2014-06-02
    • 2016-01-20
    • 2019-01-12
    • 2016-11-17
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多