Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a function in C if it hasn't been defined before?

Tags:

c

I'm writing a Ruby C Extension where I'm using math.h. It's being compiled on both OSX and Windows. Under Windows I use nmake that comes with Visual Studio Express C++ 2010.

I found that VS didn't include the round() function in their math.h. So I added this to compensate:

static inline double round( double value )
{    
  return floor( value + 0.5 );
}

That off course caused an error when compiling under OSX as round() there is defined. (The actual error I think was that I'd declared mine static after it's already been declared a non-static version.

Regardless, I'd like to avoid redefining the function if it does exist.

At the moment I have this conditional:

#ifdef _WIN32
static inline double round( double value )
{    
  return floor( value + 0.5 );
}
#endif

That worked in my scenario - but it seem a bit generic. I mean, what if I compile with a different compiler under Windows?

So my question is, can I detect if a function is already defined, and then avoid defining it myself?

Or, can I detect specifically the compiler nmake use - cl I think it is?

I'm thinking I'd ideally be able to detect if the function is already defined, as it seem like the most robust method.

like image 947
thomthom Avatar asked Mar 08 '12 20:03

thomthom


People also ask

Can you call a function before it has been defined C?

In C, if a function is called before its declaration, the compiler assumes the return type of the function as int. For example, the following program fails in the compilation.

Can a function be call before it has been defined?

So generally html - including javascript - has a top down method of parsing code. However, functions go out of this route as they are read first, but only the function head (= first line). That is why function calls are possible before declaration.

Is function declaration necessary in C?

Function declaration is required when you define a function in one source file and you call that function in another file. In such case, you should declare the function at the top of the file calling the function.

Why function declaration statement is placed prior to function definition?

The reason modern compilers give warnings on an attempt to call a function before seeing a declaration is that a declaration allows the compiler to check if arguments are of the expected type.


1 Answers

That worked in my scenario - but it seem a bit generic. I mean, what if I compile with a different compiler under Windows?

  1. Generic is good.
  2. It doesn't matter what compiler you use; you are simply checking if a symbol is defined. You could define that symbol using any compiler, it doesn't matter. ifdefs are typically how you handle portability issues.
  3. round is defined by the C99 standard. VS does not suppport C99 at this time, that is why it is missing.
like image 149
Ed S. Avatar answered Oct 02 '22 20:10

Ed S.