Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to use inline in declaration get error C2054

Tags:

c

I'm trying to compile some open source projects using the vs2013 c/c++ compiler. The file is .c extension. The below code returns some errors (below). All of which can be "fixed" by simply removing the inline in the declaration. Note: not a real function, just illustrative

static inline int pthread_fetch_and_add(int *val, int add, int *mutex)
{
    return 0;
}

errors error C2054: expected '(' to follow 'inline' error C2085: 'pthread_fetch_and_add' : not in formal parameter list error C2143: syntax error : missing ';' before '{'

like image 790
user3836754 Avatar asked Jul 14 '14 12:07

user3836754


3 Answers

Use __inline with MSVC.

inline is a c99 keyword and c99 is not yet (fully) supported with MSVC.

"The inline keyword is available only in C++. The __inline and __forceinline keywords are available in both C and C++. For compatibility with previous versions, _inline is a synonym for __inline."

Source: http://msdn.microsoft.com/en-us/library/z8y1yy88.aspx

Example

#if !defined(__cplusplus) && defined(_MSC_VER) && _MSC_VER < 1900
#  define inline __inline
#endif

Note that 1900 means Visual-studio-2015, I mean, said version supports inline in .c files (so c99 standard is little more supported, but not fully).

like image 85
ouah Avatar answered Nov 12 '22 14:11

ouah


A simple workaround is to -Dinline=__inline with the MSVC compiler.

like image 7
jonawebb Avatar answered Nov 12 '22 15:11

jonawebb


I ran into the same issue. Instead of changing every inline to __inline, I added the following before all of the function declarations:

#if defined(_MSC_VER)
#define inline __inline
#endif

This would allow the original code to still be compiled as-is with a different compiler (that presumably didn't have the same limitations as the VS one).

like image 2
gnovice Avatar answered Nov 12 '22 16:11

gnovice