Portable way to include malloc_np.h
c, cmake, freebsd, malloc, portability
Solution
Generate time
Use `CheckIncludeFile*` modules:
include(CheckIncludeFileCXX)
check_include_file_cxx("malloc.h" have_malloc)
if(have_malloc)
add_definitions(-DINCLUDE_MALLOC_H)
endif()
Compile time
Use boost.predef library to detect os specific features
Problem
I'm using a third-party library which includes `malloc_np.h`. From what I found over internet, this means that the code was supposed to compile under FreeBSD, although simply changing the include to `malloc.h` made it compilable under Linux (Ubuntu 13.10). Now I'm writing a CMake script for this library to generate appropriate make files (including NMake makefiles for MSVC 2010). What is the best way to achieve portability in such a scenario? My current solution is to test for: ``` ${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD" ``` in the CMake script. Depending on the result I issue a ``` add_definitions (-DINCLUDE_MALLOC_H="#include <malloc[_np].h>") ``` command and use this macro in the source file instead of `#include <malloc_np.h>`. Is this a good practice?