Trouble with C macro '#'
-
#define MyTemplate( filename ) "D:/Template/MyApp/" # filename #include MyTemplate( stdafx.h ) // failed to compile int main() { cout << MyTmeplate( stdafx.h ) // ok, print "D:/Template/MyApp/stdafx.h" } How to include files in "D:/Template/MyApp/" if I don't copy them to my own project directorie? solution like the macro doesn't work perhaps #include instruction doesn't accept another '#' in its statement. Could someone help me, thanks in advance!
-
#define MyTemplate( filename ) "D:/Template/MyApp/" # filename #include MyTemplate( stdafx.h ) // failed to compile int main() { cout << MyTmeplate( stdafx.h ) // ok, print "D:/Template/MyApp/stdafx.h" } How to include files in "D:/Template/MyApp/" if I don't copy them to my own project directorie? solution like the macro doesn't work perhaps #include instruction doesn't accept another '#' in its statement. Could someone help me, thanks in advance!
I recommend you have a look at Token Concatenation[^]. That explains how to define and use a C macro to concatenate tokens.
-
#define MyTemplate( filename ) "D:/Template/MyApp/" # filename #include MyTemplate( stdafx.h ) // failed to compile int main() { cout << MyTmeplate( stdafx.h ) // ok, print "D:/Template/MyApp/stdafx.h" } How to include files in "D:/Template/MyApp/" if I don't copy them to my own project directorie? solution like the macro doesn't work perhaps #include instruction doesn't accept another '#' in its statement. Could someone help me, thanks in advance!
Considering you're using stdafx.h, I assume you are using Visual Studio? Why not just add the include directory to Additional Include Directories in the Project Properties dialog, under the C/C++ tab. That way, there's no need for possibly troublesome macro's and makes it easier to maintain. For other build environments there should be similar options.
-
Considering you're using stdafx.h, I assume you are using Visual Studio? Why not just add the include directory to Additional Include Directories in the Project Properties dialog, under the C/C++ tab. That way, there's no need for possibly troublesome macro's and makes it easier to maintain. For other build environments there should be similar options.
Thank you. That's a nice solution.
-
Considering you're using stdafx.h, I assume you are using Visual Studio? Why not just add the include directory to Additional Include Directories in the Project Properties dialog, under the C/C++ tab. That way, there's no need for possibly troublesome macro's and makes it easier to maintain. For other build environments there should be similar options.
I found another trouble, I have to include the D:\template for every individual project. Is it possible to include it like "VC98\include" as an one-time-job ?
-
I found another trouble, I have to include the D:\template for every individual project. Is it possible to include it like "VC98\include" as an one-time-job ?
In vs2005,
1. Go to Tools->Options
2. Click on projects and solutions tree item
3. Select vc++ directories item
4. Select Include files in drop down window "Show Directories for"
5. Then add your include directory.I think it will be same in all VS IDE.