Portable way to enable malloc_np.h

I use a third-party library that includes malloc_np.h. From what I found over the Internet, this means that the code should have compiled under FreeBSD, although just changing the include to malloc.hmade it compile under Linux (Ubuntu 13.10).

Now I am writing a CMake script for this library to create the corresponding make files (including the NMake make files for MSVC 2010).

What is the best way to achieve portability in such a scenario?

My current solution is to check:

${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD"

in the CMake script. Depending on the result, I give

add_definitions (-DINCLUDE_MALLOC_H="#include <malloc[_np].h>")

and use this macro in the source file instead #include <malloc_np.h>.

Is this a good practice?

+4
1

CheckIncludeFile*:

include(CheckIncludeFileCXX)

check_include_file_cxx("malloc.h" have_malloc)

if(have_malloc)
  add_definitions(-DINCLUDE_MALLOC_H)
endif()

boost.predef

+3

Source: https://habr.com/ru/post/1541676/


All Articles