expected unqualified-id before string constant | extern "C"

Hello,

I have minimal experience with Arduino and while I have some experience in C/C++, I have not used extern "C" before so any sort of advice related to extern "C" and the above error will be greatly appreciated. The errors appeared when verifying/compiling the code. I'll post the relevant errors below, then part of my sketch (the full sketch exceeds the character limit but the errors are related to code in header files), and then the header files which are causing errors, in order. (The code was fine on Visual Studio, by the way)

I've read some content related to extern "C" on this community already but there were no concrete answers or explanations.

Once again, suggestions to resolve this or any explanation on extern "C" would be greatly appreciated since despite reading about extern "C" online, I don't quite get it.

Errors:


In file included from C:\Users\EddyM\Documents\Arduino\MPC_P_Motor\MPC_P_Motor.ino:6:0:
sketch\mpcmoveCodeGeneration.h: In function 'void loop()':
sketch\mpcmoveCodeGeneration.h:22:8: error: expected unqualified-id before string constant
extern "C" {
^~~
In file included from C:\Users\EddyM\Documents\Arduino\MPC_P_Motor\MPC_P_Motor.ino:8:0:
sketch\qpkwik.h:21:8: error: expected unqualified-id before string constant
extern "C" {
^~~
In file included from C:\Users\EddyM\Documents\Arduino\MPC_P_Motor\MPC_P_Motor.ino:9:0:
sketch\rt_nonfinite.h:19:8: error: expected unqualified-id before string constant
extern "C" {
^~~


Partial Sketch:


void loop() {
#include "mpcmoveCodeGeneration.h"
#include "mpcmoveCodeGeneration_types.h"
#include "qpkwik.h"
#include "rt_nonfinite.h"
#include <string.h>

  void mpcmoveCodeGeneration(struct3_T * mpcmovestate, const struct4_T * mpcmovedata,
                             double * u, struct9_T * Info)
  {
    static const double Ac[72] = { -0.083037111170812361, -0.097623488903372108,
                                   -0.0995927636417564, -0.099858610346689469, -0.099894498923985137,
                                   -0.099899343783659514, -0.099899997826450648, -0.099900086120436729,
                                   -0.0999000980398831, -0.099900099648975726, 0.083037111170812361,
                                   0.097623488903372108, 0.0995927636417564, 0.099858610346689469,
                                   0.099894498923985137, 0.099899343783659514, 0.099899997826450648,
                                   0.099900086120436729, 0.0999000980398831, 0.099900099648975726, -1.0, -1.0,
                                   1.0, 1.0, -0.0, -0.083037111170812361, -0.097623488903372108,
                                   -0.0995927636417564, -0.099858610346689469, -0.099894498923985137,
                                   -0.099899343783659514, -0.099899997826450648, -0.099900086120436729,
                                   -0.0999000980398831, 0.0, 0.083037111170812361, 0.097623488903372108,
                                   0.0995927636417564, 0.099858610346689469, 0.099894498923985137,
                                   0.099899343783659514, 0.099899997826450648, 0.099900086120436729,
                                   0.0999000980398831, -0.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
                                   1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
                                   0.0, 0.0
                                 };

    static const double Mx[72] = { -0.01458637773255974, -0.0019692747383842917,
                                   -0.00026584670493307239, -3.5888577295669282E-5, -4.8448596743753593E-6,
                                   -6.5404279113680324E-7, -8.8293986077764386E-8, -1.1919446377431031E-8,
                                   -1.6090926263009991E-9, -2.1722309896194071E-10, 0.01458637773255974,
                                   0.0019692747383842917, 0.00026584670493307239, 3.5888577295669282E-5,
                                   4.8448596743753593E-6, 6.5404279113680324E-7, 8.8293986077764386E-8,
                                   1.1919446377431031E-8, 1.6090926263009991E-9, 2.1722309896194071E-10, 0.0,
                                   0.0, 0.0, 0.0, 0.00026122119385124865, 4.5886362754438393E-5,
                                   6.1950167934390758E-6, 8.3631032757372381E-7, 1.1289960446134619E-7,
                                   1.5241137490666415E-8, 2.0575118320182185E-9, 2.7775846399177547E-10,
                                   3.749663215467102E-11, 5.0619426775932234E-12, -0.00026122119385124865,
                                   -4.5886362754438393E-5, -6.1950167934390758E-6, -8.3631032757372381E-7,
                                   -1.1289960446134619E-7, -1.5241137490666415E-8, -2.0575118320182185E-9,
                                   -2.7775846399177547E-10, -3.749663215467102E-11, -5.0619426775932234E-12,
                                   0.0, 0.0, 0.0, 0.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0,
                                   -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0
                                 };

    static const double Sx[30] = { 0.01458637773255974, 0.0019692747383842917,
                                   0.00026584670493307239, 3.5888577295669282E-5, 4.8448596743753593E-6,
                                   6.5404279113680324E-7, 8.8293986077764386E-8, 1.1919446377431031E-8,
                                   1.6090926263009991E-9, 2.1722309896194071E-10, -0.00026122119385124865,
                                   -4.5886362754438393E-5, -6.1950167934390758E-6, -8.3631032757372381E-7,
                                   -1.1289960446134619E-7, -1.5241137490666415E-8, -2.0575118320182185E-9,
                                   -2.7775846399177547E-10, -3.749663215467102E-11, -5.0619426775932234E-12,
                                   1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0
                                 };

    static const double Mu1[24] = { -0.083037111170812361, -0.097623488903372108,
                                    -0.0995927636417564, -0.099858610346689469, -0.099894498923985137,
                                    -0.099899343783659514, -0.099899997826450648, -0.099900086120436729,
                                    -0.0999000980398831, -0.099900099648975726, 0.083037111170812361,
                                    0.097623488903372108, 0.0995927636417564, 0.099858610346689469,
                                    0.099894498923985137, 0.099899343783659514, 0.099899997826450648,
                                    0.099900086120436729, 0.0999000980398831, 0.099900099648975726, -1.0, -1.0,
                                    1.0, 1.0
                                  };

//etc...
}

}

Header Files causing Issues:


mpcmoveCodeGeneration.h:

#ifndef MPCMOVECODEGENERATION_H
#define MPCMOVECODEGENERATION_H

/* Include files */
#include "mpcmoveCodeGeneration_types.h"
#include "rtwtypes.h"
#include <stddef.h>
#include <stdlib.h>
#ifdef __cplusplus

extern "C" {

#endif

  /* Function Declarations */
  extern void mpcmoveCodeGeneration(struct3_T *mpcmovestate, const struct4_T
    *mpcmovedata, double *u, struct9_T *Info);

#ifdef __cplusplus

}
#endif
#endif

qpkwik.h:

#ifndef QPKWIK_H
#define QPKWIK_H

/* Include files */
#include "rtwtypes.h"
#include <stddef.h>
#include <stdlib.h>
#ifdef __cplusplus

extern "C" {

#endif

  /* Function Declarations */
  void qpkwik(const double Linv[9], const double Hinv[9], const double f[3],
              const double Ac[72], const double b[24], short iA[24], double x[3],
              double lambda[24], double *status);

#ifdef __cplusplus

}
#endif
#endif

rt_nonfinite.h:

#ifndef RT_NONFINITE_H
#define RT_NONFINITE_H

/* Include files */
#include "rtwtypes.h"
#ifdef __cplusplus

extern "C" {

#endif

  extern real_T rtInf;
  extern real_T rtMinusInf;
  extern real_T rtNaN;
  extern real32_T rtInfF;
  extern real32_T rtMinusInfF;
  extern real32_T rtNaNF;
  extern boolean_T rtIsInf(real_T value);
  extern boolean_T rtIsInfF(real32_T value);
  extern boolean_T rtIsNaN(real_T value);
  extern boolean_T rtIsNaNF(real32_T value);

#ifdef __cplusplus

}
#endif
#endif
void loop() {
#include "mpcmoveCodeGeneration.h"
#include "mpcmoveCodeGeneration_types.h"
#include "qpkwik.h"
#include "rt_nonfinite.h"
#include <string.h>

  void mpcmoveCodeGeneration(struct3_T * mpcmovestate, const struct4_T * mpcmovedata,
                             double * u, struct9_T * Info)  // function definition is illegal here

I have never seen includes inside of loop(). Not saying it is wrong but in over 8 years on this forum this is the first time that I have seen it.

The includes might be OK, but a function definition inside of a function (loop()) is defiantly illegal.

Why are you using #include directives inside loop()? Thats very unusual, and in your case, wrong.
They usually go in the top of the file, before any code.

And you can't declare/define mpcmoveCodeGeneration() inside loop().

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.