Karl Alexius
Karl Alexius

Reputation: 353

How to link jemalloc shared library using cmake

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

Upvotes: 5

Views: 5098

Answers (3)

truth Zheng
truth Zheng

Reputation: 81

  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!

Upvotes: 0

Martin Prazak
Martin Prazak

Reputation: 1605

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})

Upvotes: 9

arrowd
arrowd

Reputation: 34411

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

Upvotes: 1

Related Questions