1

I'm trying to include some C function in a C++ program. I get some issue with a function declaration that contains array as parameter.

extern "C" {
    double Test( int nVar1, double f[nVar1] ) {
        return f[nVar1-1];
    }
}

int main() {
    cout << "!!!Hello World!!!" << endl; 
    double f[2] = {1.0,2.0};
    Test(2, f);
    return 0;
 }

I get following errors

  • 'f' was not declared in this scope
  • 'nVar1' was not declared in this scope
  • use of parameter outside function body before ']' token

I use Atollic 8.0 (GCC and C -std=gnu11)

Any help will welcome

Thank-you

3
  • 3
    While it's a valid C99 function, it's not valid C++ (what you are compiling as). Commented Oct 19, 2017 at 8:24
  • Just for my personal curiosity, why do you want to do this ? why not using c++ container ? Commented Oct 19, 2017 at 8:46
  • I would like to implement unit testing on legacy embedded C code. I use CppUnit test with output in a console. I'm facing to such prototypes: int ComputedF(int nPoints, int nFunc, double x[], double f[nPoints][nFunc], double df[nPoints][nFunc]) Commented Oct 19, 2017 at 19:33

2 Answers 2

6

extern "C" isn't for compiling c inside cpp source. It's only to use ABI of C inside cpp source/header: mangling, calling conventions, exception handling... (thanks to Ajay Brahmakshatriya)

By mangling, I want to say the internal unique name of function used by compiler/linker. C mangling is really different to c++ mangling, and so not compatible. To find C function inside c++, you have to say to compiler/linker under which internal unique name the function is known.

extern "C" only switches which ABI has to be used, including mangling used to create internal unique name and how function has to be called, not to switches compilation mode.

If you really want to compile c code, you have to put your code inside a c source file and compile it separately. And use extern "C"to declare function in cpp environment, to allow c++ code to use it. BUT declaration of function has to be compatible with c++, and double Test( int nVar1, double f[nVar1] ) isn't.

function.c, compile with gcc -c:

double Test( int nVar1, double f[] ) {
    return f[nVar1-1];
}

function.h, compatible c and c++:

#ifndef _FUNCTION_H
#define _FUNCTION_H

#ifdef __cplusplus
extern "C" {
#endif

double Test( int nVar1, double f[] );

#ifdef __cplusplus
}
#endif

#endif

main.cpp, compile with g++ -c:

#include "function.h"

int main() {
    cout << "!!!Hello World!!!" << endl; 
    double f[2] = {1.0,2.0};
    Test(2, f);
    return 0;
 }

And finally, link everything with g++ linker:

g++ function.o main.o -o my_program

See example here:

Sign up to request clarification or add additional context in comments.

3 Comments

It is not just the mangling, but also calling conventions, exception handling that is done C stlye. ABI would be the correct term in that case.
C mangling is really different to c++ mangling I'd say that "really different" is quite an understatement. There's no real C++-style name mangling in C at all, because there's no overloading. There may be some ABI-based name changes where the symbol isn't the same as the name of the function in the source code, but that's not what I think of when dealing with C++ name mangling.
BUT declaration of function have to be compatible with c++ Clear thank-you
0

The problem is with array size. It cannot be variable, should be const. If you need to pass an array of variable size then just pass the pointer to its first element: double Test( int nVar1, double * f) {

3 Comments

This type of declaration is working since C99 link I would like to use an existing code without changing it
See nothing like function(int array[count], int count) there, only function(int array[], int count) which is not the same
link From C99, C language supports variable sized arrays to be passed simply by specifying the variable dimensions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.