1

I have several source and header files that contains code that should be compiled dependent on the same option. That option might be set with

#define myOption

To be visible this option in all my files I need to put it in place where all files could see it. I see only one way - place this option in special header file and include it to all source and header files. Is there any other way to solve my problem? What is the best practices with code organisation?

UPD

In comments I found option that header file that contains myOption might be stdafx.h. But this is not good practice as far I can see because in CPP projects with enabled precompile header I can have for example some units with C code with turned off precompiled header.

vico
  • 17,051
  • 45
  • 159
  • 315
  • 1
    It depends on how your code is set up, but this is one of the cases where using precompiled headers (`stdafx.h`, `stdafx.cpp`) comes in handy. You can put commonly included headers, and common preprocessor macros here. – Cory Kramer May 18 '15 at 15:29
  • 1
    You can also set it as a project compiler option. – crashmstr May 18 '15 at 15:29
  • If you use a utility like cmake you can define the symbols in the makefile. See http://stackoverflow.com/questions/9017573/define-preprocessor-macro-through-cmake – NathanOliver May 18 '15 at 15:34
  • 3
    Most compilers come along with a way to specifiy preprocesor vars on the command line especially for this usecase. Try `-DmyOption` – Sebastian Hoffmann May 18 '15 at 15:39

1 Answers1

2

As @Paranaix mention you can provide preprocessor definition for compiler as command line argument like -DmyOption. Let suppose you use Visual Studio, you can open project properties->C/C++->Preprocessor->Preprocessor Definitions and place myOption in that list. It automatically add -DmyOption argument for compiler. So every source file in your project now can check that option.

gomons
  • 1,946
  • 14
  • 25