AntiClimacus
AntiClimacus

Reputation: 167

Compiling C/C++ code on MacOS

I have a C/C++ project that I started on Linux and I want to port it now to MacOS. The issue I am facing is the following:

My source code links to a C library (the abc tool from https://bitbucket.org/alanmi/abc) and uses header files from that library. When compiling on Linux everything goes well but things tend to break when compiling on MacOS. The error I am getting is the following:

abc/src/misc/vec/vecPtr.h:895:33: error: too many arguments to function call, expected 0, have 2

It is mainly due to the way function pointers are handled on the two systems. As I have understood from searching online is that in C, function pointers with empty parenthesis () are considered to have a variable number of arguments while this is not the case in C++. It seems that g++ on Linux is able to compile such code correctly while on MacOS it is failing.

Can anyone please help with some insights on this issue?

Thanks

Upvotes: 0

Views: 467

Answers (1)

nneonneo
nneonneo

Reputation: 179687

The library is declaring function pointer arguments like this:

int (*Vec_PtrSortCompare)()

and then invoking them like this:

Vec_PtrSortCompare(p->pArray+i, p->pArray+k-1)

In C, a function declarator with an empty argument list doesn't specify how many arguments it takes. Therefore, you can legally pass as many arguments as you want to such a function. However, such "variadic" functions are considered deprecated.

In C++, however, a function declarator with an empty argument list specifies that the function takes zero arguments.

This difference means that you cannot compile a header file like this with a C++ compiler. You have two options: you could compile the parts of your program using this header with only a C compiler and make a library or set of object files, then compile the rest of your program with a C++ compiler (assuming you have C++ parts).

Alternatively, you could fix the function declarations. From context, it appears that the functions should be declared as

int (*Vec_PtrSortCompare)(const void **a, const void **b)

(In proper C++, this would be a templated function to avoid having to use void *; however, C does not have templates.)

Upvotes: 1

Related Questions