How to build library mixing .c and .cpp source files


I made the following code for tests:

A library class .h file:

#ifndef JTAG_h
#define JTAG_H

#include "test.h"
#include "test2.h"

class JTAGClass{
                static int soma(int x);
                static int soma2(int x);
                static int mult(int x);

extern JTAGClass JTAG;

A library class .cpp file:

include "JTAG.h"

int JTAGClass::soma(int x){ return x+x; }

int JTAGClass::soma2(int x){ return sum(x); }

int JTAGClass::mult(int x){ return x*x; }

And other include files test.h test.c test2.h test2.cpp respectively:


static int sum(int x);


include "test.h"

extern int sum(int x){ return x+x; }


ifndef ZAMBER_H

define ZAMBER_H

class zamber{ public : int zam(int x); };



include "test2.h"

int zamber::zam(int x){ return x*x; }

Well, when I compile these codes I get this message from Arduino compiler: JTAG/JTAG.cpp.o: In function JTAGClass::mult(int)': /usr/share/arduino/libraries/JTAG/JTAG.cpp:26: undefined reference tosum(int)' collect2: ld returned 1 exit status

How to fix this problem, mix C with C++ codes? Thanks

Your test.c code is wrong. To use C code with C++ code, you need to tell the compiler that name mangling is not to be applied to the C code. That is done using extern "C" in the header file, not extern in the source file.

extern "C"
  static int sum(int x);
#include "test.h"
int sum(int x)
  return x+x;

Thanks for reply.

I made the changes in respectively files, but with error still:

In file included from /usr/share/arduino/libraries/JTAG/test.c:1:0:
/usr/share/arduino/libraries/JTAG/test.h:1:8: error: expected identifier or ‘(’ before string constant

I’m running Arduino 1.0 in Ubuntu 12.04.

When trying to diagnose compiler error messages, it [u]really[/u] helps to have a complete and up to date copy of the code you're using.

I'm sorry. You need to wrap the extern statement, so that the C compiler does one thing, and the C++ compiler does something different:

#ifdef __cplusplus
 extern "C" {
  static int sum(int x);
#ifdef __cplusplus

Ok PaulS

I tried the code suggested, but the problem persists.

JTAG/JTAG.cpp.o: In function JTAGClass::mult(int)': /usr/share/arduino/libraries/JTAG/JTAG.cpp:25: undefined reference tosum' collect2: ld returned 1 exit status

I think that problem is with Arduino compiler in the moment of creation of the static library. Is possible change the compiler parameters? Thanks