V
V
Vova2018-04-17 10:22:02
C++ / C#
Vova, 2018-04-17 10:22:02

What to do with includes?

Hello.
In the process of picking another feature, I found that I was missing something like:

#include "my_dir//" + some_const_name + "//my_header.h"
void main() {
  use_it(my_const_from_header);
}

In general, I wanted to choose the path to the header at compile time, depending on some external conditions (for example, pass some_const_name to the compiler as a /D parameter).
Googling, I realized that there is no solution (not counting the hell with defines, which solves the problem, but somehow very ugly).
Then I got into Wikipedia, specifically into the description of C ++ 11/14/17 and realized that the pluses are developing towards templates, libraries, etc. There was not a word about the preprocessor.
The only similar thing I found is the import directive, which is suggested to be used instead of include. But, alas, only for the standard library.
Q: Is there a solution to my problem? Do I understand correctly that no one will ever develop the preprocessor and, apparently, they will soon abandon it?
PS: I'm using Visual Studio 2017.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
T
terrier, 2018-04-17
@JustMoose

Well, C++ doesn't do that.
Inserting the results of defines into "#include" is explicitly prohibited, although it might be useful.
If you really really need it - use preliminary code generation, go through the script through the sources and replace some "$placeholder$" with a certain some_const_name should be easy.
No, as long as C++ is alive, so is the preprocessor.

R
res2001, 2018-04-17
@res2001

You don't understand correctly. Preprocessor directives are used in almost every C/C++ project. Look at the header files of the standard library. Templates are about something else.
This is greatly exaggerated.
Usage example:
1. When compiling, set your define: /Dsome_const_name via the /D compiler option
2. In the code:

#ifdef some_const_name
#include "my_header.h"
#else
#include "my_other_header.h"
#endif

3.All

S
Sergey Sakhno, 2018-04-17
@Punk_Joker

Just pass the path to the compiler where to look for the headers, that's all

A
anikavoi, 2018-04-18
@anikavoi

Don't do it. Have pity on those who will look at your code after you. It is possible that it will be you, but older :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question