[CMake] Cmake inline macro?

Jack Kelly endgame.dos at gmail.com
Fri Sep 14 19:30:38 EDT 2007


Gregory C. Sharp wrote:
> Hi,
> 
> Does Cmake have a macro that tests how a compiler
> defines inline, such as autoconf's AC_C_INLINE?
> 
> Thanks,
> Greg

Hi Greg,

I couldn't find one, so I ported AC_C_INLINE to CMake. Since there's no 
equivalent of autoheader to make the config.h.in, I just used 
ADD_DEFINITIONS.

Should these sort of tests be collected somewhere?

Here is a minimal working example. It tests the inline keyword, then 
__inline__ and then __inline. When it finds one that works, it will 
ADD_DEFINITIONS(-Dinline=${KEYWORD}) and if none work, it will 
ADD_DEFINITIONS(-Dinline=).

File 1: CMakeLists.txt:
# Inspired from /usr/share/autoconf/autoconf/c.m4
PROJECT(FOO C)
FOREACH(KEYWORD "inline" "__inline__" "__inline")
   IF(NOT DEFINED C_INLINE)
     TRY_COMPILE(C_HAS_${KEYWORD} "${FOO_BINARY_DIR}"
       "${FOO_SOURCE_DIR}/test_inline.c"
       COMPILE_DEFINITIONS "-Dinline=${KEYWORD}")
     IF(C_HAS_${KEYWORD})
       SET(C_INLINE TRUE)
       ADD_DEFINITIONS("-Dinline=${KEYWORD}")
     ENDIF(C_HAS_${KEYWORD})
   ENDIF(NOT DEFINED C_INLINE)
ENDFOREACH(KEYWORD)
IF(NOT DEFINED C_INLINE)
   ADD_DEFINITIONS("-Dinline=")
ENDIF(NOT DEFINED C_INLINE)

File 2: test_inline.c
/* Test source lifted from /usr/share/autoconf/autoconf/c.m4 */
typedef int foo_t;
static inline foo_t static_foo(){return 0;}
foo_t foo(){return 0;}
int main(int argc, char *argv[]){return 0;}

Have fun.

-- Jack


More information about the CMake mailing list