Click here to Skip to main content
15,883,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Does anyone know how to pre-pend and append a double quote in a #include using the preprocessor?

I would like to create a preprocessor macro say FULL_PATH that I will use to #include files as followed
C++
#include FULL_PATH(PATH, FILE_NAME)

I do want to use VC++ Include Environment Variables less since they are annoying to utilize with dozens of projects written by me and other people, especially when some projects occasionally have files with the same name.

I've tried an example below
C++
#define BOOST_JOIN( X, Y ) BOOST_DO_JOIN( X, Y )
#define BOOST_DO_JOIN( X, Y ) BOOST_DO_JOIN2(X,Y)
#define BOOST_DO_JOIN2( X, Y ) X##Y
#define  boost_dir( __X ) BOOST_JOIN("C:/Boost/boost_1_44_0/", #__X)


#pragma message( boost_dir(boost/thread/mutex.hpp) )
#include boost_dir(boost/thread/mutex.hpp)

The #pragma message expands to the correct file name
C:/Boost/boost_1_44_0/boost/thread/mutex.hpp

The #include does not work however; possibly missing double quotes.
fatal error C1083: Cannot open include file: 'C:/Boost/boost_1_44_0/': Permission denied

If I add a double quote as a prefix and suffix, it's is if the code was
C++
#include "\"C:/Boost/boost_1_44_0/boost/thread/mutex.hpp\""

when I would want it to be

C++
#include "C:/Boost/boost_1_44_0/boost/thread/mutex.hpp"
Posted
Updated 18-Nov-10 2:53am
v2

This works:
#define boost_dir( __X ) "C:/Boost/boost_1_44_0/" __X
	cout << boost_dir("/boost/thread/mutex.hpp") << endl;

so should be fairly simple to adapt.
 
Share this answer
 
Comments
T2102 18-Nov-10 8:59am    
Yes, that format sometimes works. The solution I tried before my post utilized the format you mention. This worked inconsistently. In some projects, I could utilize the path
C:/Documents and Settings/Ted/My Documents/Visual Studio 2010/Projects/
and in others the same include line would not work.
The inconsistency occurs when I create the path using the preprocessor VS2010_dir, for example:

#define VS2010_dir(C:/Documents and Settings/Ted/My Documents/Visual Studio 2010/Projects/__X)
#define error_code_dir(__X) VS2010_dir(Error_Code/Include/__X)
#include error_code_dir(Error_Code.h)
 
Share this answer
 
Comments
Richard MacCutchan 18-Nov-10 9:32am    
You need double quotes around your strings.
T2102 18-Nov-10 21:36pm    
I could not get it to work with double quotes in Visual C++ 2010, but using open and close brackets ( < and > ) worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900