Skip to content

[C++] Platform.Memory.h includes memory_mapped_file.cpp and defines non-inline functions in headers (multiple definition link errors) #124

Backlog

Description

@konard

Problem

Platform.Memory.h does #include "memory_mapped_file.cpp". The headers also define non-inline functions and variables at namespace scope:

  • Internal::page_size() and Internal::PageSize (ResizableDirectMemoryBase.h);
  • Internal::as_bytes and Internal::ZeroBlock (HeapResizableDirectMemory.h).

Any program with two translation units that include Platform.Memory.h fails to link.

Reproduction

experiments/issue-94-cpp-odr:

b.cpp:(.text+0x0): multiple definition of `memory_mapped_file::mmf_granularity()'; a.cpp: first defined here
b.cpp:(.text+0x14): multiple definition of `memory_mapped_file::base_mmf::base_mmf()'
...
exit status of link: 1

Proposed solution

  • Mark all namespace-scope functions inline, and make PageSize an inline variable (or a function with a static local).
  • Stop including the .cpp: either make it header-only with inline, or remove the unused memory_mapped_file.* files, since mio is what is actually used.
  • Each header should be self-contained (#pragma once plus its own includes).
  • Add a two-TU test to the CMake test target.

Part of #94 (catch up with mem-rs). Analysis and evidence: case study.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcpp

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions