Porting a straight C program to run on Arduino?

I located a set of source code for the old text based star trek game. I was thinking I could possibly port it over to run using the serial monitor. Problem is.. All this stuff is written in straight c and I can't get it to accept my glue code.

To start off I wrote two files.


#ifndef globals_h
#define globals_h

#include "Arduino.h"

extern "C" void randomize(void);

extern "C" int getch(void);

//extern "C" int max(int a, int b);

//extern "C" int min(int a, int b);


And gloabls.cpp

#include "globals.h"
#include "Arduino.h"

void randomize(void) {

int getch(void) {
  return (int)Serial.read();

And the compiler kicks out this..

globals.h:7: error: expected identifier or '(' before string constant
 extern "C" void randomize(void);
globals.h:9: error: expected identifier or '(' before string constant
 extern "C" int getch(void);

Along with lots of other things, but this is the start and I need to at least get this first function to work.

Anyone have experience in soething like this? 'Cause I don't have a lot in this area.


-jim lee

Try it without the includes.
Arduino hides a lot of the standard C from the user, including libraries like stdio.h.

You can probably copy the logic of the original program and write functions to replace the I/O.
Since you want to start with getch(), look at this.

AFAIK the compiler compiles files *.c as C and *.cpp as C++. The extern "C" hack is only required for C declarations compiled as C++, i.e. with header files #included in *.cpp files.

The compiler generates different compiled identifiier names for C (underscore) and C++ (mangled names), so that a function declared as C has to be made known to C++ callers as extern "C" in its header file, else the linker cannot find a function with the mangled name.

If you want to use C standard libraries, these will conflict with C++ standard libraries. Else you can compile all C code as C++, with (hopefully) few adaptations.