简体   繁体   English

在shm_open上的C ++ Boost库未定义参考:命令行缺少DSO

[英]C++ Boost Library Undefined Reference On shm_open: DSO Missing From Command Line

I am writing an application which is to play the role of a subscriber in a DDS system. 我正在编写一个在DDS系统中扮演订户角色的应用程序。 This subscriber will, upon receipt of data, write the value(s) to a shared memory location so that they can be read by another GUI application. 该订户将在接收到数据后将值写入共享内存位置,以便其他GUI应用程序可以读取它们。

I have written a version of the application which compiles and runs A-OK on Windows, but I am now faced with creating a version to be run on Ubuntu. 我已经编写了该应用程序的一个版本,该版本可以在Windows上编译并运行A-OK,但是现在我面临着创建要在Ubuntu上运行的版本的问题。 Being Windows native, this has proved to be troublesome for me. 作为Windows本地用户,这对我来说很麻烦。

To create the Ubuntu version, I have ported my codebase to a Ubuntu VM, and am now trying to compile it there, which is where I'm running into issues. 为了创建Ubuntu版本,我已将代码库移植到Ubuntu VM,现在正尝试在那里编译它,这是我遇到问题的地方。

For the most part, everything compiles fine and the process is wonderfully similar to that on Windows. 在大多数情况下,所有程序都可以正常编译,并且该过程与Windows上的过程极为相似。 Unfortunately, the final statement to link all of my generated .o files is what fails. 不幸的是,链接我所有生成的.o文件的最终声明失败了。 A bit of a description is below: 下面是一些描述:

I'm using the boost interprocess library to write a header file that will provide the shared memory read/write operations. 我正在使用boost-interprocess库编写一个头文件,该文件将提供共享内存的读/写操作。 The .cpp which achieves this is as below: MemWriterH.cpp 实现此目标的.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);
}

And its accompanying header: MemWriterH.h 及其随附的标头: MemWriterH.h

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

These are then compiled to a .o file using: 然后使用以下命令将它们编译为.o文件:

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

Inside the application's main file, I have a #include which allows the functions above to be used. 在应用程序的主文件中,我有一个#include,允许使用上述功能。

While there are various other elements to this application (as seen below), these all seem to compile, build, and link fine. 尽管此应用程序还有其他各种元素(如下所示),但这些元素似乎都可以进行编译,构建和链接。

This resulting MemWriterH.o file is then used in a final GCC statement which — I believe — links all the .o files together to produce the application's executable. 然后,此生成的MemWriterH.o文件将在最终的GCC语句中使用(我相信,该语句将所有.o文件链接在一起以生成应用程序的可执行文件)。 This final statement is: 最后的声明是:

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

which, unfortunately, gives me the error: 不幸的是,这给了我错误:

/usr/bin/ld: /home/bob/workspace_c/HID_LDM_DDS/HID_LDM_DDS_INCLUDES/MEM_MAPPING/MemWriterH.o: undefined reference to symbol 'shm_open@@GLIBC_2.2' / 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: error adding symbols: DSO missing from command line collect2: error: ld returned 1 exit status /lib/i386-linux-gnu/librt.so.1:添加符号时出错:命令行collect2中缺少DSO:错误:ld返回1退出状态

At which point, I feel completely lost. 在这一点上,我感到完全迷失了。 The .cpp compiles to a .o without complaint, so I'm not quite sure where else to go from here. .cpp会毫无争议地编译为.o,因此我不确定从这里还能去哪里。 What is a DSO, and how do I add it to the command line? 什么是DSO,如何将其添加到命令行?

EDIT: 编辑:

An update now the problem's solved. 现在已解决问题的更新。 As Sehe pointed out below, the library in which shm_open is defined was missing. 正如Sehe在下面指出的那样,缺少了定义shm_open的库。 This is included with the -lrt flag, which should be included after the .o files being linked ( see here for more explanation on that ). 这包含在-lrt标志中,该标志应在链接.o文件之后包含( 有关此内容的更多说明,请参见此处 )。

Solving this did, however, lead to another issue whereby the compiler was complaining at several occurrences of an error along the lines of: 'undefined reference to std::some_function'. 但是,解决此问题确实导致了另一个问题,即编译器抱怨以下几处错误:“未定义对std :: some_function的引用”。 This, it turns out, was owing to an issue in linking C and C++ code (the header) using the gcc command. 事实证明,这是由于使用gcc命令链接C和C ++代码(标头)时出现问题。 Adding a -lstdc++ flag, or using the g++ compile command ( see the answer here for a description of what's going on ) solves this, too, and all compiles successfully. 添加-lstdc ++标志或使用g ++编译命令( 有关正在发生的事情,请参见此处的答案 )也可以解决此问题,并且所有编译均成功。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 libtorch c++ 未定义参考:命令行中缺少 DSO - libtorch c++ undefined reference: DSO missing from command line C++ boost 库shared_memory_object 对&#39;shm_open&#39; 的未定义引用 - C++ boost libraries shared_memory_object undefined reference to 'shm_open' 使用 CMake 对“shm_open”的未定义引用 - undefined reference to `shm_open' using CMake 未定义的符号引用,命令行中缺少 DSO - Undefined reference to symbol, DSO missing from command line -L / lib -lrt -lpthread已经对`shm_open&#39;进行了未定义的引用 - undefined reference to `shm_open' already with -L /lib -lrt -lpthread PHP / C ++:共享内存时出现shm_open()错误 - PHP / C++: shm_open() error when sharing memory GLEW + cmake链接失败“未定义引用符号glDrawElements”+“DSO从命令行中丢失” - GLEW + cmake linking fails “undefined reference to symbol glDrawElements” + “DSO missing from command line” 未定义的符号引用 '<symbol> '...添加符号时出错:命令行中缺少 DSO(使用 CMake)</symbol> - Undefined Reference to Symbol '<Symbol>' ... Error Adding Symbols: DSO Missing From Command Line (With CMake) 错误:cv :: VideoCapture :: ~~ VideoCapture()的未定义引用符号,命令行中缺少DSO - Error: Undefined reference symbol to cv::VideoCapture::~VideoCapture(), DSO missing from command line C ++实现CNN错误添加符号:命令行缺少DSO - C++ implementing CNN Error adding symbols: DSO missing from command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM