10

I think the preprocessor handles files one by one and I can't figure out how to do it with includes, so I think it's impossible, but it would be great to hear other's thoughts.

I have in a.cpp:

#define A 1

and I want to use it from 2.cpp.

EDIT: I cant modify first file. So for now i just have copied defines. But question still opened.

Yola
  • 18,496
  • 11
  • 65
  • 106

4 Answers4

21

Defines inside a source file aren't seen by other translation units. Implementation files are compiled separately.

You can either

  • put them in a header and include it
  • use your compiler's options
  • do it the sane way - extern const int A = 1; in an implementation file and declare it when you want to use it extern const int A;.

Of these, I'd say the first option is possibly the worst you can use.

Patrick Trentin
  • 7,126
  • 3
  • 23
  • 40
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 1
    for now option with extern seems as most sane and less invasive way to solve it, +1 – Yola Jan 11 '13 at 10:22
  • 3
    I have to take issue with the blanket statement that including the #define in a header is the worst way to do it! I don't want to be all Daily WTF "Ah well, if you're in an embedded environment with no file system blah blah" about it, but there are definitely situations where a #define is far better than a const int. – Vicky Jan 11 '13 at 12:32
  • In terms of file dependencies and coupling option 1 and 3 seem to be equivalent. Am I missing something here? – CpILL Nov 07 '17 at 05:38
  • 1
    I add to put "extern" in front of the first definition to make it works. See: https://stackoverflow.com/questions/14894698/why-does-extern-const-int-n-not-work-as-expected – HenriC Feb 09 '18 at 20:47
  • Why is the first option possibly the worst? – Yoni Zohar Jul 23 '20 at 06:08
  • @YoniZohar Lots of reasons. Many of these are less dangerous with just a single literal integer, but there are still problems. Dangers include surprising text combinations, duplicate evaluation of arguments, inability to control scope, no type checking (which leads itself to multiple problems, like surprising overload resolution), etc. And for what? There's no benefit to using define here. Define is mostly useful for header guards, conditional compilation (e.g. Windows vs. Linux), or complex and unusual situations where you are pasting text together to make new tokens. – GrandOpener Apr 07 '21 at 20:29
8

If you want to share a define between two source files, move it to a header file and include that header from both source files.

mydefines.h:

#ifndef MY_DEFINES_H
#define MY_DEFINES_H

#define A (1)
// other defines go here

#endif // MY_DEFINES_H

source1.cpp:

#include "mydefines.h"
// rest of source file

source2.cpp:

#include "mydefines.h"
// rest of source file

You could also specify the define in the compiler command line. This can be fiddly to maintain for cross platform code (which may need different command lines for different compilers) though.

simonc
  • 41,632
  • 12
  • 85
  • 103
4

You would need to put your #define in a header file which is then #included by both cpp files.

Vicky
  • 12,934
  • 4
  • 46
  • 54
-1

Like a way - using extern const variables.

For example:

file1.h (where you will use definitions)

extern const int MY_DEF;

#if (MY_DEF == 1)
 //some another definitions
#endif

file2.h (where you will define definitions)

const int MY_DEF = 1

Pro & Con:

(+): you can use some values for defines

(-): ALL definitions must be defined

  • defining in file2.h and including file2.h in two implementation files (.cpp), will give linker error for duplicate definitions. – Abhishek Jain Jul 14 '15 at 06:27