3

I am new to C/C++. Wanted to know why we cannot declare 'extern C' for C++ macro's similar to methods/functions...which will allow macro's defined in C++ file to be accessed in .c files.

Thanks in Advance

2 Answers 2

10

Macros in both C and C++ are a text replacement mechanism. Because of that, you can't define a macro in one source file and use it in a different source file.

What you can do is define a macro in a header file and include that header in both C and C++ source files where you want to use that macro. Such a header would need to contain only code that comes from the common subset of C and C++ so it will be acceptable in both languages.

3
  • 1
    Actually, can't you #define f_x(x) cout << (x) << endl; in a header file that's included from a plain C source code file? As long as you don't f_x("whatever"); or somesuch in your C source code, you should be fine, because the expanded form never occurs in the post-preprocessed (ugh!) C code. (#include <iostream> or the ilk inside that header file may be trickier :))
    – user
    Apr 22, 2013 at 13:35
  • @MichaelKjörling: As long as a macro does not get expanded, its replacement list can contain utter garbage. That is how a C compiler would see your f_x macro. Apr 22, 2013 at 14:35
  • 3
    ...or, better, use the preprocessor to avoid defining the macro at all if __cplusplus is not defined. But really, don't use macros in C++.
    – Blrfl
    Apr 22, 2013 at 15:34
1

Generally, you should not use macros in C++ at all. The main reason is that macros do not respect scope. They simply drop a chunk of code in the middle of your source file. This can cause all manner of bugs, which will be very hard to find, because you would have to do this textural insertion in your head.

In C++, if the cost of a function call is your performance bottleneck, then you should use inline functions, which do respect scope, instead of macros.

If you need to share code between C++ and C, then you probably should write a proper C function and declare that as extern "C". If you need to be able to call C++ code from C, then you should write a very thin C wrapper function around it.

IMHO, the simplest thing to do is to just use C++. The only reason to use C would be if you are working on an embedded platform, which doesn't have a C++ compiler.

8
  • 2
    Wrong. There are many valid use cases for the preprocessor in C++.
    – SK-logic
    Apr 22, 2013 at 15:49
  • 1
    The only valid use cases are include guards and #ifdef __cplusplus. What possible reason is there to use macros? See "Effective C++" by Scott Meyers.
    – Dima
    Apr 22, 2013 at 15:55
  • 1
    Wrong again. Take a look at, say, .def and .inc files in Clang sources. It is quite an idiomatic way of using preprocessor in C++.
    – SK-logic
    Apr 22, 2013 at 16:10
  • 1
    I am going to have to go ahead and disagree. Those files are generated automatically, and presumably are not meant to be read by people. Big difference. By the way I remembered one more legitimate use case: assert is a macro.
    – Dima
    Apr 22, 2013 at 20:52
  • 1
    Wrong again. Many are handwritten.
    – SK-logic
    Apr 23, 2013 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.