I would like to run C code in C++, using Xcode 8.33 on MacOS Sierra 10.12. I am new to C/C++, compilers, etc so please bear with me. The C code, when compiled and ran with make
via Terminal, works. But when I throw all the same files into a XCode C++ project, there is an error with the data file. Note: I did change main.c
to main.cpp
.
//**** main.cpp *****
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
extern "C" {
#include "msclib.h"
}
int main(int argc, char** argv)
{
assert(argc >= 1);
return msc_get_no(argv[1]);
}
The file msclib.c
calls on the data file mscmix_dat.c
. Here is also msclib.h
// ***** msclib.h *****
extern size_t msc_get_no(const char*);
// ***** msclib.c *****
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include "msclib.h"
struct msc_data
{
const char* code;
const char* desc;
};
typedef struct msc_data MSCDat;
static const MSCDat mscdat[] =
#include "mscmix_dat.c"
;
static const size_t msccnt = sizeof(mscdat) / sizeof(mscdat[0]);
static int msc_cmp(const void* a, const void* b)
{
const char* msc_code = a;
const MSCDat* p = b;
return strcmp(msc_code, p->code);
}
size_t msc_get_no(const char* msc_code)
{
assert(NULL != msc_code);
assert(strlen(msc_code) == 5);
MSCDat* p = bsearch(msc_code, &mscdat[0], msccnt, sizeof(mscdat[0]), msc_cmp);
if (NULL == p)
{
fprintf(stderr, "MSC \"%s\" not valid\n", msc_code);
return 0;
}
assert(NULL != p);
return p - &mscdat[0];
}
When running/compiling, the mscmix_dat.c
file gets the error Expected identifier or (
- which is what I need help with. Even when I replace mscmix_dat.c
with .cpp
, I get the error Expected unqualified-id
// ***** mscmix_dat.c *****
{ //<-- Xcode highlights this line and gives the error
{ "*****", "Error" },
{ "00-01", "Instructional exposition (textbooks, tutorial papers, etc.)" },
{ "00-02", "Research exposition (monographs, survey articles)" },
{ "00A05", "General mathematics" },
.
.
.
}
I would appreciate explanations as to why this error is occurring, suggestions on how to fix it, and if necessary alternatives to processing this data file. Thank you!