c++cmakejemalloc

Manage different version of jemalloc in cmake


I have compiled two versions of jemalloc, a release version and one with profiling enabled(--enable-prof). The version with profiling is configured with a suffix(--suffix="-prof").

As a result, there are two libraries installed in the system:

libjemalloc.so and libjemalloc-prof.so

Can you tell me how to select a particular library in cmake?


Solution

  • In your CMakeLists.txt, you can add an option to enable profiling and then link to jemalloc-prof if it's enabled, else link to jemalloc. Make sure you are calling find_library or find_package whichever is suitable before linking it. Something like below:

    # ...
    option(ENABLE_JEMALLOC_PROFILING "Enable profiling for Jemalloc" FALSE)
    
    # ...
    
    if (ENABLE_JEMALLOC_PROFILING)
        find_library(JEMALLOC_PROF jemalloc-prof HINTS PATH_HINT1 PATH_HINT2 REQUIRED)
        target_link_libraries(YOUR_TARGET PRIVATE jemalloc-prof)
    else()
        find_library(JEMALLOC jemalloc HINTS PATH_HINT1 PATH_HINT2 REQUIRED)
        target_link_libraries(YOUR_TARGET PRIVATE jemalloc)
    endif()