Anmol Singh Jaggi
Anmol Singh Jaggi

Reputation: 8576

Including header from the same file on every platform

I have a file cpp-options.txt in which I have written every compiler option I use to compile my C++ programs.

I have made an alias g+ as g++ @/path/to/cpp-options.txt $* , so that whenever I invoked g+ prog.cpp, from anywhere on my computer, the program is compiled with all the compiler options from that file.

Now I want to add another option which includes a header file header.h in the options file. This file is always kept on the same directory as the cpp-options.txt file.
So, now the cpp-options.txt file looks like this -:

-Wall -Wextra.....
-include /path/to/header.h

Now, this setup works on Windows perfectly, but wont work on Linux, as the absolute path to the options file on Linux would be something like this -:

/mnt/media......../absolute/path/to/header.h

So, the compiler would complain about the absence of any such file on Linux.
Now I am aware of one solution of this problem, that is to include the folder in which these two files are kept in the PATH environment variable on both the Operating Systems and then simply writing -:

-Wall -Wextra.....
-include header.h

However, I dont want to pollute the PATH variables.
Is there any other way of accomplishing this ?

Upvotes: 2

Views: 66

Answers (1)

Anmol Singh Jaggi
Anmol Singh Jaggi

Reputation: 8576

The best I could do was to create another common file cpp-options-common.txt which contained only the the compiler options (-Wall, -Wextra, -std=c++14 etc), and shift the -include /path/to/header.h statement to the cpp-options.txt file.
Also, I imported the cpp-options-common.txt file into the cpp-options.txt file by using the @ GCC compiler directive.

My final configuration -:

cpp-options-common.txt -: ( located in the Windows partition )

-Wall -Wextra -Wfatal-errors ...

On Windows -:

cpp-options.txt -:

@path/to/cpp-options-common.txt
-include path/to/header.h

Linux -:

cpp-options.txt -:

@/media/Data/path/to/cpp-options-common.txt
-include /media/Data/path/to/header.h

Upvotes: 2

Related Questions