0

I wish to replace a function call with a string using pre-processor directives. Something like this:

#ifdef DEBUG
    #define Func1(arg) "Function not supported"
#endif

So basically when someone calls this function, I want a compilation error, such that this function is invisible in DEBUG mode, and in place of that , the following string is printed in the compilation log. This method throws error. Is there any other means to print a particular string i want when func1() is called?

3
  • Please clarify "Throws error". Is it a compilation error? A runtime error? Provide the error description and the code at the usage site. Commented Jul 15, 2014 at 12:26
  • the exact error is: "Error: expression must have (pointer-to-) function type".... This error is thrown at the location this function was called. Commented Jul 15, 2014 at 12:58
  • Is the message so important? Commented Jul 15, 2014 at 14:29

3 Answers 3

2

The most obvious way to archieve such behaviour is to use #error directive. However since it's not possible to construct "conditional #error directive" I guess next move is _Pragma operator introduced in C99. Here is the solution that produces a message during compilation:

#include <stdio.h>

#define DEBUG 1

#ifdef DEBUG
    #define Func1(arg) _Pragma("message \"Function not supported.\"")
#endif

void (Func1)(int arg)
{
}

int main(void)
{
    Func1(1);
    Func1(2);
    Func1(3);

    return 0;
}

Compilation (with gcc):

...
check.c:15: note: #pragma message: Function not supported.
check.c:16: note: #pragma message: Function not supported.
check.c:17: note: #pragma message: Function not supported.

I know It's not direct solution (such message is not even treated as warning, so -Werror doesn't change anything), however you can use e.g. grep tool or any other method to scan compiler's output.

Since GCC 4.8 there is also #pragma GCC error "message", which is direct (but non-portable) solution. Check this answer for more information. For example:

#ifdef DEBUG
    #define Func1(arg) _Pragma("GCC error \"Function not supported.\"")
#endif
Sign up to request clarification or add additional context in comments.

2 Comments

The issue in my case is, i need a compilation error at the place where function is called. In your solution, the preprocessor will be handled before function is actually compiled. So when handled in a sequential manner, the preprocessor won't execute.
@user3840787: The actual place where Func1(arg) function-like macro is "executed" is Func1(1); instruction in main definition. This doesn't matter that macro is defined, without proper invocation it doesn't do anything.
1

One way is to simply leave the function undefined. This will result in an error at link time.

If you use gcc, you could use one of its extensions, a function attribute:

#ifndef DEBUG
#define __nodebugonly
#else
#define __nodebugonly __attribute__((error("Function not supported")))
#endif

void Func1(int arg) __nodebugonly;

1 Comment

Right, I misread your question in that regard. Since __debugonly would be a misnomer then I changed that, too.
0

You can use

#error "Function not supported"

to abort the compilation. But obviously when someone has access to the source that can be removed - so what exactly do you try to do?

If you don't trust those people with your source then don't give it to them. Link it into an .obj, define the 'interface' with a .h and only give out that.

1 Comment

I could have used #error... :) But that will be called immediately when preprocessor is handled, and NOT when function is called. :( :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.