How to link jemalloc shared library using cmake - makefile

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

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

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

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

Related

gcc/gfortran 11 cannot find headers/libraries in default locations

After upgrading python 3.8.6 to 3.9.10 using homebrew, my Cython extensions no longer without explicitly adding /usr/local/include (for my Intel MacBook) or /opt/homebrew/include to the include_dirs of my extension.
My setup.py.in:
import os, sys
from numpy.distutils.core import setup, Extension
from Cython.Build import cythonize
link_arguments = []
extra_include_dirs = []
if (sys.platform == 'darwin'):
link_arguments.append("-Wl,-rpath")
link_arguments.append("-Wl,#loader_path/")
if os.path.exists('/opt/homebrew/'):
extra_include_dirs.append("/opt/homebrew/include/")
else:
extra_include_dirs.append("/usr/local/include/")
else:
link_arguments.append("-Wl,-rpath=${CMAKE_SOURCE_DIR}/lib/")
pynwp_extension = Extension(
name="pynwp",
sources=["${CMAKE_CURRENT_SOURCE_DIR}/lambert.f90", "${CMAKE_CURRENT_SOURCE_DIR}/pynwp.f90", "${CMAKE_CURRENT_SOURCE_DIR}/readAtmosphereGen.f90", "${CMAKE_CURRENT_SOURCE_DIR}/ptogrot.f", "${CMAKE_CURRENT_SOURCE_DIR}/bilin1.f", "${CMAKE_CURRENT_SOURCE_DIR}/fl2pres_f.f","${CMAKE_CURRENT_SOURCE_DIR}/message.c","${CMAKE_CURRENT_SOURCE_DIR}/gridWindDirCorrection.F"],
libraries=["HirlamUtils_fPIC", "eccodes_f90", "jasper"],
library_dirs=["${PROJECT_BINARY_DIR}", "${CMAKE_SOURCE_DIR}/build${CMAKE_BUILD_TYPE}/src/libHirlamUtils/", "/opt/homebrew/lib/"],
extra_link_args = link_arguments,
include_dirs=["${CMAKE_SOURCE_DIR}/include", "/usr/lib64/gfortran/modules/",
"${CMAKE_SOURCE_DIR}/build${CMAKE_BUILD_TYPE}"] + extra_include_dirs,
extra_f90_compile_args=["-DLINUX", "-DIS_LITTLE_ENDIAN", "-DUSEWALLTIME", "-DHAS_BLAS", "-DHAS_LAPACK", "-DGRIB32", "-DTIMING", "-DPREC32", "-fno-whole-file", "-g", "-fbounds-check"]
#compiler_directives={'language_level' : "3"}
)
setup(name="pynwp",
author="me",
author_email="me!me.com",
version="1.0.1",
description="Python wrapper for pynwp",
package_dir={"": "${CMAKE_CURRENT_SOURCE_DIR}"},
url="http://emaddc.eu",
license="MIT License",
ext_modules=[pynwp_extension]
)
In the file above, I have hardcoded the location (temporarily) of the homebrew library dir and added some functionality for the include dir based on /opt/homebrew being found. If I remove this from the file, compilation fails as eccodes.mod cannot be found, see the output when I run the command generate by python/CMake manually:
buildDebug git:(master) ✗ /opt/homebrew/bin/gfortran -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops -I<project_dir>//include -I/usr/lib64/gfortran/modules/ -I<project_dir>//buildDebug -Ibuild/src.macosx-12-arm64-3.9/build/src.macosx-12-arm64-3.9 -I/opt/homebrew/lib/python3.9/site-packages/numpy/core/include -Ibuild/src.macosx-12-arm64-3.9/numpy/distutils/include -I/opt/homebrew/opt/python#3.9/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c -c <project_dir>//src/pynwp/readAtmosphereGen.f90 -o build/temp.macosx-12-arm64-3.9<project_dir>//src/pynwp/readAtmosphereGen.o -DLINUX -DIS_LITTLE_ENDIAN -DUSEWALLTIME -DHAS_BLAS -DHAS_LAPACK -DGRIB32 -DTIMING -DPREC32 -fno-whole-file -g -fbounds-check
f951: Warning: Nonexistent include directory '/usr/lib64/gfortran/modules/' [-Wmissing-include-dirs]
f951: Warning: Nonexistent include directory 'build/src.macosx-12-arm64-3.9/build/src.macosx-12-arm64-3.9' [-Wmissing-include-dirs]
f951: Warning: Nonexistent include directory 'build/src.macosx-12-arm64-3.9/numpy/distutils/include' [-Wmissing-include-dirs]
<project_dir>//src/pynwp/readAtmosphereGen.f90:3:7:
3 | use eccodes
| 1
Fatal Error: Cannot open module file 'eccodes.mod' for reading at (1): No such file or directory
compilation terminated.
This extension is part of a larger project that is build using CMake. When running the gfortran command manually, the same errors indicating that eccodes.mod cannot be found. The file is however located on a default location on the gfortran/gcc search path:
locate eccodes.mod
/opt/homebrew/Cellar/eccodes/2.24.2/include/eccodes.mod
/opt/homebrew/include/eccodes.mod
And the search path for gfortran:
gfortran -E -Wp,-v -
#include <...> search starts here:
/opt/homebrew/include
/opt/homebrew/Cellar/gcc/11.2.0_3/bin/../lib/gcc/11/gcc/aarch64-apple-darwin21/11/include
/opt/homebrew/Cellar/gcc/11.2.0_3/bin/../lib/gcc/11/gcc/aarch64-apple-darwin21/11/include-fixed
/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk/usr/include
/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk/System/Library/Frameworks
End of search list.
Another project uses gcc/ld and has a similar problem. I need to explicitly add LINK_DIRECTORIES(/opt/homebrew/lib) to CMakeList.txt in order for gcc to find the eccodes library. Without that, I get:
gcc-11: warning: this compiler does not support X86 (arch flags ignored)
ld: library not found for -leccodes
collect2: error: ld returned 1 exit status
make[2]: *** [src/smoothModeS-v51/smoothModeS-v51.x] Error 1
make[1]: *** [src/smoothModeS-v51/CMakeFiles/smoothModeS-v51.x.dir/all] Error 2
make: *** [all] Error 2
Adding the paths to CPATH and LIBRARY_PATH had no effect.
This method works but seems hard to maintain. What am I missing and what has changed that gfortran/gcc no longer search in the default paths for but libraries and headers/modules?
EDIT
Just found that the standalone executable that uses similar code as the python extension and also uses eccodes has a similar issue. If I do not include INCLUDE_DIRECTORIES(/opt/homebrew/include) in CMakeLists.txt , I get:
cd <project_dir>/buildDebug/src/collocEHS && /opt/homebrew/bin/gfortran -I<project_dir>/buildDebug -I<project_dir>/include -I<project_dir>/src/readASTERIX2 -I<project_dir>/src/geomag70_linux -I<project_dir>/src/libDTG -fallow-argument-mismatch -ffpe-trap=invalid,zero,overflow -DPREC32 -DLINUX -DIS_LITTLE_ENDIAN -DUSEWALLTIME -DHAS_BLAS -DHAS_LAPACK -DGRIB32 -DTIMING -ffixed-line-length-none -g -fcheck=all -Wall -fcheck=bounds -O0 -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk -c <project_dir>/src/pynwp/readAtmosphereGen.f90 -o CMakeFiles/collocEHSv2.dir/__/pynwp/readAtmosphereGen.f90.o
<project_dir>/src/pynwp/readAtmosphereGen.f90:3:7:
3 | use eccodes
| 1
Fatal Error: Cannot open module file 'eccodes.mod' for reading at (1): No such file or directory
compilation terminated.
make[2]: *** [src/collocEHS/CMakeFiles/collocEHSv2.dir/__/pynwp/readAtmosphereGen.f90.o] Error 1
make[1]: *** [src/collocEHS/CMakeFiles/collocEHSv2.dir/all] Error 2
make: *** [all] Error 2
So I guess the problem is unrelated to python but more with gcc/gfortran (gcc version 11.2.0 (Homebrew GCC 11.2.0_3).
EDIT 2
A reboot of the laptop fixed the issue with the python extension build and setup.py requiring additional directories. For normal builds using CMake, I still require the extra INCLUDE_DIRECTORIES and LINK_DIRECTORIES order for gcc/gfortran to find libraries installed by brew in /opt/homebrew (or /usr/local for intel MacBook).
As per homebrew devs, this is desired behaviour: /opt/homebrew and /usr/local are "special" directories to be manually added in e.g., CMake projects. This is explained in my bug report on home-brew's GitHub, see https://github.com/Homebrew/homebrew-core/issues/95561.
I haven't been able to confirm this with documentation.

How to fix undefined reference LLVM error while linking CXX executable

I was trying to build a llvm-slicer from:
https://github.com/IAIK/ios-analysis-llvmslicer
and I follow the instructions:
cd llvm-slicer
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE="Release" -DLLVM_TARGETS_TO_BUILD="AArch64;X86" -DLLVM_ENABLE_EH=YES -DLLVM_ENABLE_RTTI=ON ..
make -j4 opt
make -j4 llvm-slicer
But when I execute the last command make -j4 llvm-slicer, I got an error:
[100%] Built target LLVMAnalysis
[100%] Linking CXX executable ../../bin/llvm-slicer
/usr/bin/ld: ../../lib/libLLVMSlicer.a(FunctionStaticSlicer.cpp.o): in function `llvm::Pass* llvm::callDefaultCtor<(anonymous namespace)::FunctionSlicer>()':
FunctionStaticSlicer.cpp:(.text._ZN4llvm15callDefaultCtorIN12_GLOBAL__N_114FunctionSlicerEEEPNS_4PassEv+0x1c): undefined reference to `vtable for (anonymous namespace)::FunctionSlicer'
collect2: error: ld returned 1 exit status
make[3]: *** [tools/llvm-slicer/CMakeFiles/llvm-slicer.dir/build.make:116: bin/llvm-slicer] Error 1
make[2]: *** [CMakeFiles/Makefile2:9647: tools/llvm-slicer/CMakeFiles/llvm-slicer.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:9654: tools/llvm-slicer/CMakeFiles/llvm-slicer.dir/rule] Error 2
make: *** [Makefile:2740: llvm-slicer] Error 2
I have no idea about how to fix it and I couldn't find any similar issues on Google, I hope someone can help me to figure it out, many thx.
There can be several scenarios for this issue, but in my opinion, here the scenario is for the given information that your default compiler (_ZN...something errors mostly because of compilers or linkers) is earlier version of gnu (gcc for c, g++ for c++) in your host pc (or maybe another compiler other than clang) and struggling about resolving a c++ feature anonymous namespace, as, a part of given error says:
/usr/bin/ld: ../../lib/libLLVMSlicer.a(FunctionStaticSlicer.cpp.o): in function `llvm::Pass* llvm::callDefaultCtor<(anonymous namespace)::FunctionSlicer>()':
FunctionStaticSlicer.cpp:(.text._ZN4llvm15callDefaultCtorIN12_GLOBAL__N_114FunctionSlicerEEEPNS_4PassEv+0x1c): undefined reference to `vtable for (anonymous namespace)::FunctionSlicer'
For this, there are several things you can do:
1-) You can change your compiler for building. In command window, you can export clang and clang++ as your compiler before cmake configuration. Here I assume that you installed new versions of clang compiler from anywhere, even github clone, I can give several examples here how to export and you can adapt one of them to your case easily:
export CC=clang
export CXX=clang++
another version:
export CC=clang-11
export CXX=clang++-11
another version:
export CC=/usr/bin/clang
export CXX=/usr/bin/clang++
another version:
export CC=/usr/bin/clang-12
export CXX=/usr/bin/clang++-12
another github clone version:
export CC=~/llvm/llvm-project/build/bin/clang
export CXX=~/llvm/llvm-project/build/bin/clang++
Even if this does not change your compiler, you can change your compiler while configuring with cmake by -DCMAKE_C_COMPILER={your-c-compiler} and -DCMAKE_CXX_COMPILER={your-c++-compiler} cmake flags. Try either with gcc or clang. Here is an example configuration in your case:
cmake \
-DCMAKE_C_COMPILER=clang \
-DCMAKE_CXX_COMPILER=clang++ \
-DCMAKE_BUILD_TYPE="Release" \
-DLLVM_TARGETS_TO_BUILD="AArch64;X86" \
-DLLVM_ENABLE_EH=YES \
-DLLVM_ENABLE_RTTI=ON ..
2-) Error can be because of c++ standard, you can add -DCMAKE_CXX_STANDARD=14 (Default C++ standard using by LLVM) flag as cmake configuration like:
cmake -DCMAKE_BUILD_TYPE="Release" -DLLVM_TARGETS_TO_BUILD="AArch64;X86" -DLLVM_ENABLE_EH=YES -DLLVM_ENABLE_RTTI=ON -DCMAKE_CXX_STANDARD=14 ..
3-) Even if didn't work above cases, you can remove anonymous namespace from FunctionStaticSlicer.cpp file (given in the error message) and try to build again. You can find these lines like this:
// some code here and do not delete
namespace { // delete this line
// as you can see there is no name of namespace that's why called 'anonymous'
// if it was it would be like 'namespace nmspcName {'
// some code here and do not delete
} // and delete this line, too
// some code here and do not delete
But this is really bad practice, even if it can solve your problem, I do not recommend.
Also you can try to do similar changes in CMakeLists.txt file or adding other cmake configuration flags, however in my opinion, you could solve your problem easily by 1. option which is changing your default compiler.

Create a shared lib that depends on dlib using Cmake

I'm building a shared library in C++ that depends on dlib using CMake.
While it has been possible for me to build and install a shared dlib using make and make install, so far I haven't figured out the way to link to this shared dlib library.
The examples of usage given in the DLib website always link to a static library.
This is what I have so far:
cmake_minimum_required(VERSION 2.8.12)
project(face_align)
set(CMAKE_CXX_STANDARD 11)
find_package(dlib)
add_library(face_align SHARED src/mylib.cpp)
target_link_libraries(face_align dlib::dlib)
The linker complains like so:
/usr/bin/ld: cannot find -ldlib::dlib
collect2: error: ld returned 1 exit status
CMakeFiles/face_align.dir/build.make:94: recipe for target 'libface_align.so' failed
make[2]: *** [libface_align.so] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/face_align.dir/all' failed
make[1]: *** [CMakeFiles/face_align.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
If I remove the SHARED from
add_library(face_align SHARED src/mylib.cpp) the project builds successfully. This makes me think that dlib:dlib is pointing to the static library. I see that there is a dlib:dlib_shared but no success linking to this one either.
The answer seems to be (1) linking to dlib as a static library (2) making the code linking to dlib position independent:
cmake_minimum_required(VERSION 2.8.12)
project(face_align)
set(CMAKE_CXX_STANDARD 11)
# Unfortunately, this only links dlib as static library
add_subdirectory(libs/dlib dlib_build)
add_library(face_align SHARED src/mylib.cpp)
# This makes the target position independent, allowing to link an static library to a dynamic one.
set_target_properties(face_align PROPERTIES POSITION_INDEPENDENT_CODE ON) # This made it work
# Link Dlib
target_link_libraries(face_align dlib::dlib)
EDIT:
Also, in Linux (I haven't tested this in other S.O.s) CMake does some magic and, after installing dlib library in shared mode:
$ cd $DLIB
$ mkdir build && cd build
$ cmake ../dlib
$ make && make install
Is possible to simply add the library using target_link_libraries as follows:
cmake_minimum_required(VERSION 2.8.12)
project(face_align)
set(CMAKE_CXX_STANDARD 11)
add_library(face_align SHARED src/mylib.cpp)
# Link Dlib
target_link_libraries(face_align dlib)
Then, in your code, include headers as follows:
#include <dlib/your_dlib_header_here.h>

error compiling uClibc (__NR_or1k_atomic undeclared)

I am following http://openrisc.net/toolchain-build.html to build gcc toolchain for openrisc or32.
I'm doing 'building by hand' flow and had passed
binutils
stage 1 gcc
install linux headers
and was to do 'compile uClibc' which is composed of commands below.
$ git clone git://openrisc.net/jonas/uClibc
$ cd uClibc
$ make ARCH=or32 defconfig
$ make PREFIX=${SYSROOT}
$ make PREFIX=${SYSROOT} install <br>
when I run 'make ARCH=or32 defconfig', I get this error.
CC libpthread/linuxthreads.old/attr.o
In file included from libpthread/linuxthreads.old/internals.h:30:0,
from libpthread/linuxthreads.old/attr.c:26:
./libpthread/linuxthreads.old/sysdeps/or32/pt-machine.h: In function 'testandset':
./libpthread/linuxthreads.old/sysdeps/or32/pt-machine.h:41:8: error: '__NR_or1k_atomic' undeclared (first use in this function)
./libpthread/linuxthreads.old/sysdeps/or32/pt-machine.h:41:8: note: each undeclared identifier is reported only once for each function it appears in
In file included from libpthread/linuxthreads.old/../linuxthreads.old_db/proc_service.h:20:0,
from libpthread/linuxthreads.old/../linuxthreads.old_db/thread_dbP.h:9,
from libpthread/linuxthreads.old/internals.h:32,
from libpthread/linuxthreads.old/attr.c:26:
./include/sys/procfs.h: At top level:
./include/sys/procfs.h:32:21: fatal error: asm/elf.h: No such file or directory
compilation terminated.
make: *** [libpthread/linuxthreads.old/attr.o] Error 1
Has anybody had same problem? I use CentOS 6.4.
gcc searches for the header file from the system in the order
/usr/local/include
libdir/gcc/target/version/include (libdir was /usr/lib in my case)
/usr/target/include
/usr/include
my system had sys/syscall.h under /usr/include so that file was used when sys/syscall under uClib/include should have been used. So I added -nostdinc so that gcc doesn't search the standard include path. Now it became
make PREFIX=${SYSROOT} -nostdinc
and it works!
The following command was also modified
make PREFIX=${SYSROOT} -nostdinc install
Cheers!

Trouble compiling FUSE filesystems on OS X

OS: OS X Mavericks (v10.9)
FUSE: OSXFUSE v2.6.2
I'm trying to compile the loopback filesystem in C, but I'm getting this error:
$ make
cc -D_FILE_OFFSET_BITS=64 -I/usr/local/include/osxfuse/fuse -Wall -g -F/Library/Frameworks -o loopback loopback.c -losxfuse
ld: library not found for -losxfuse
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [loopback] Error 1
Trying to compile boxfs2 also produces this error:
$ make
Package libxml-2.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `libxml-2.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libxml-2.0' found
Package libcurl was not found in the pkg-config search path.
Perhaps you should add the directory containing `libcurl.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libcurl' found
Package libapp was not found in the pkg-config search path.
Perhaps you should add the directory containing `libapp.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libapp' found
Package libjson was not found in the pkg-config search path.
Perhaps you should add the directory containing `libjson.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libjson' found
Compiling boxfs.c
cc -c boxfs.c -o boxfs.o
boxfs.c:15:10: fatal error: 'fuse.h' file not found
#include <fuse.h>
^
1 error generated.
make: *** [boxfs.o] Error 1
Can anyone point me in the right direction?
As far as the first problem, you need the OSXFUSE library somewhere the compiler can see it, or you need to tell the compiler where it is.
You may have some success using mdfind to locate the osxfuse library file, then add -L/path/to/osxfuse to the compile/configure script to the makefile.
Similarly, the for the second, try making sure boxfs2 knows about the fuse header: Looks like adding -I/usr/local/include/osxfuse/fuse might do it.

Resources