3

I'm trying to link the jemalloc library into my application at build time using it as a generic implementation. According to https://github.com/jemalloc/jemalloc/wiki/Getting-Started the linking flags to use are:

-L`jemalloc-config --libdir` -Wl,-rpath,`jemalloc-config --libdir` -ljemalloc `jemalloc-config --libs`

So I did the following CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.12.2)
project(widget)
include_directories(include)
file(GLOB SOURCES "src/*.cpp")
add_executable(widget ${SOURCES})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L`jemalloc-config --libdir` -Wl,-rpath,`jemalloc-config --libdir` -ljemalloc `jemalloc-config --libs`")

But when I do make I get the following errors:

Linking CXX executable widget
c++: error: `jemalloc-config: No such file or directory
c++: error: unrecognized command line option ‘--libdir`’
c++: error: unrecognized command line option ‘--libdir`’
c++: error: unrecognized command line option ‘--libs`’
make[2]: *** [widget] Error 1
make[1]: *** [CMakeFiles/widget.dir/all] Error 2
Karl Alexius
  • 313
  • 1
  • 6
  • 15

3 Answers3

4

For future generations, as this still comes up as one of the first links on Google.

Jemalloc comes with pkg-config setup, which can be used like this:

find_package(PkgConfig REQUIRED)
pkg_check_modules (JEMALLOC jemalloc)

pkg_search_module(JEMALLOC REQUIRED jemalloc)
include_directories(${JEMALLOC_INCLUDE_DIRS})

target_link_libraries(your_target_name ${JEMALLOC_LIBRARIES})
Martin Prazak
  • 1,476
  • 12
  • 20
0

execute_process() command is your friend. Use it to run jemalloc-config executable and then put its output into CMake variables.

arrowd
  • 33,231
  • 8
  • 79
  • 110
0
  1. find you root_dir of jemalloc. Mine is /Users/lion/homebrew/Cellar/jemalloc/5.2.1_1/lib/ (I install jemalloc by brew on macOS)
  2. link (soft link) all its lib to your local lib ln -s /Users/lion/homebrew/Cellar/jemalloc/5.2.1_1/lib/* /usr/local/lib

Then it works!

truth Zheng
  • 71
  • 1
  • 2