Question

I am developing an embedded C application in a C90-compliant compiler which, on the other hand, for testing and debugging purposes, is deployed in Matlab/Simulink interfacing the application with a CPP file. This bundle is compiled instead with Matlab MEX, which is configured to use Visual Studio 2005 to build.

This means we have one CPP file, and a couple of .C/.H files that are built altogether. This workflow has been successful for me, using the #ifdef __cplusplus extern "C" {...} trick on each C call.

However, I have come with a problem when the need of inline functions arose due to time constraints in the embedded application.

Next, SSCCE built in VS2005:

main.cpp

#include "c_method.h"

void main()
{
    for(int b = 0; b < 9; b++)
    int a = c_method(b);
}

c_method.c

#include "c_method.h"

inline int inline_fun(int x)
{
    return x+1;
}

int c_method(int b)
{
    return inline_fun(b);
}

c_method.h

#ifndef __C_METHOD_H__
#define __C_METHOD_H__

int c_method(int b);

#endif

Which provides the following errors:

c_method.c(7) : error C2054: expected '(' to follow 'inline'
c_method.c(8) : error C2085: 'inline_fun' : not in formal parameter list
c_method.c(8) : error C2143: syntax error : missing ';' before '{' 

I noticed I missed extern "C" {...} but did not work either.

As read here in SO, changing c_method.c to c_method.cpp will do the trick, however I'd rather have an alternative solution, if exists any, as I'm not very confident about the embedded C compiler accepting the cpp extension without coming whining to me...

Thank you.

Added

@πάντα ῥεῖ got with the answer. Creating an empty declaration of inline will do the trick.

However, as @Lundin suggests, I'll meditate the possibility of using another C compiler for the PC platform build.

Was it helpful?

Solution

Visual studio doesn't support the inline keyword in C, because it does not implement C99 (which introduced that keyword).

However, it does support the Microsoft-specific keyword __inline in both C and C++. To make your code portable, you can do this:

#include "c_method.h"

#ifdef _MSC_VER
  #define inline __inline
#endif

inline int inline_fun(int x)
{
    return x+1;
}

int c_method(int b)
{
    return inline_fun(b);
}

Of course, in practice, you'd put the inline definition into a shared header file.

OTHER TIPS

There is no inline keyword in C90, so that would be why it doesn't work. It seems rather unlikely that a C compiler will compile C++. Visual Studio only does it because it is a C++ compiler, not a C compiler.

You have to check the embedded compiler for compiler extensions to the language. There is usually a compiler option you can enable.

I don't think in Visual Studio, inline function can be defined in a .c or .cpp file. You may try to move the definition of the inline function into the header file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top