microsoft / microsoft/mimalloc

How to use object file with cmake?

Open
#532 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
13.4k
Forks
1.2k
Avg merge
4d 45m
Merged PRs (30d)
13

Description

Mimalloc docs say to link as the first object with an example of direct compiler invocation:

To ensure that the standard malloc interface resolves to the mimalloc library, link it as the first object file. For example:

> gcc -o myprogram mimalloc-override.o  myfile1.c ...

However its not clear how or if this can even be done in a cmake project.
I have specified it as the first source file like this:

    add_executable(MyApp);
    ...
    list(INSERT Sources 0 $<TARGET_OBJECTS:mimalloc-obj>) # Put mimalloc-obj before everything else in Sources.
    list(APPEND APP_LIBRARIES pthread) # not documented but required for linking to succeed.
    ...
    target_sources(MyApp PRIVATE ${Sources})
    target_link_libraries(MyApp PRIVATE ${APP_LIBRARIES})

However when I look at the link command with VERBOSE=1 make, it is not placed anywhere near first. Despite this, the build succeeds and seems to run without any errors, I'm just not certain it's technically correct and would work the same on other systems.

Another point of confusion for me is the documentation saying this is specifically for "Unix-like systems". I am guessing this includes any Linux or Mac OSX system, but would mingw-based builds on Windows count as well?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the CMake target_sources and target_link_libraries usage shown in the issue, then compare the generated VERBOSE=1 make link command on the supported platforms. Done means documenting whether the object-ordering approach is valid in CMake and how the Unix-like and MinGW cases should be handled.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cmake
Domain
build-system
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.