473,465 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to replace a string in #include define by using #DEFINE

14 New Member
The original #include string like this:

#include "my_path1\Includes\types.h"

I want to change like this

#DEFINE MY_PATH my_path1

#include "MY_PATH\include\types.h"

This can not be compiled. Is there a $() or other symbol needed?
May 10 '07 #1
13 5797
weaknessforcats
9,208 Recognized Expert Moderator Expert
The \ is an escape sequence character, like \n. The character after the \ is a code. To have an actual backslash, you must use \\. In this case the character after the \ is a \ which is a code for the character \ so the end result is one \.

Your path should be:
Expand|Select|Wrap|Line Numbers
  1. #include "my_path1\\Includes\types.h"
  2.  
May 10 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
Oops. I forgot the second \. Let me try again:

Expand|Select|Wrap|Line Numbers
  1. #include "my_path1\\Includes\\types.h"
  2.  
May 10 '07 #3
AdrianH
1,251 Recognized Expert Top Contributor
The original #include string like this:

#include "my_path1\Includes\types.h"

I want to change like this

#DEFINE MY_PATH my_path1

#include "MY_PATH\include\types.h"

This can not be compiled. Is there a $() or other symbol needed?
So your question is that you wish to use a symbol for the start of your include path?

If you don't want to add the -I switch to your compile line, you can do what you are asking like this:

#define MY_PATH "my_path1/"
#include MY_PATH "include/types.h"

The way the C/C++ compiler deals with strings is that if there are two or more consecutive strings separated by only whitespace or nothing, then it is to concatenate the strings together.

BTW. you should use the '/' not the '\' in your strings for includes, it should work for all compilers. I'm not positive if '\' is standard and even if it is, you would have to double them up like weakness stated.


Adrian
May 10 '07 #4
yanjie111
14 New Member
I did not explain clearly before.
My problem is I want to replace a string in the #include string by prepressor MACRO definition.

I have the same file name types.h in different directories. They have different content because different build will use them.
In one build, I want include
#include "my_path1\types.h"

In another build, I want include
#include "my_path2\types.h"

I want to use a macro definition to switch between them during compiling process.

#define PATH_NAME my_path1
#include "PATH_NAME\types.h"

Can the compiler change it to
#include "my_path1\types.h"
automatically?

If I change to
#define PATH_NAME my_path2
#include "PATH_NAME\types.h"

Can the compiler change it to
#include "my_path2\types.h"
automatically?


Can preprocessor change preprocessor?
That's my question.
May 10 '07 #5
AdrianH
1,251 Recognized Expert Top Contributor
I did not explain clearly before.
My problem is I want to replace a string in the #include string by prepressor MACRO definition.

I have the same file name types.h in different directories. They have different content because different build will use them.
In one build, I want include
#include "my_path1\types.h"

In another build, I want include
#include "my_path2\types.h"

I want to use a macro definition to switch between them during compiling process.

#define PATH_NAME my_path1
#include "PATH_NAME\types.h"

Can the compiler change it to
#include "my_path1\types.h"
automatically?

If I change to
#define PATH_NAME my_path2
#include "PATH_NAME\types.h"

Can the compiler change it to
#include "my_path2\types.h"
automatically?


Can preprocessor change preprocessor?
That's my question.
Yes, just like I suggested in my last post.

Alternatively, you can use a #if/#else/#endif to manipulate the include like

Expand|Select|Wrap|Line Numbers
  1. #if USE_PATH == 1
  2. # include "my_path1/types.h"
  3. #else
  4. # include "my_path2/types.h"
  5. #endif
Again, use slashes (/) not backslashes (\) or you may get into trouble.


Adrian
May 10 '07 #6
yanjie111
14 New Member
The first way you suggested does not work. I changed the \ to /.
If I use
#define PATH_VALUE "mypath1/types.h"
#include PATH_VALUE
it works. But I can not combine two strings like this
#define PATH_VALUE "mypath1"
#include PATH_VALUE "/types.h"
The compile error is:
fatal error C1083: Cannot open include file: "mypath1"
It can not see the second string and combine it.

The second way you suggested is OK. Because a lot of files have the #include line and there are too much different path name. That means in every source code I must add a very long
#if
#elif
#elif
#elif
#elif
#endif
Is there a better way to do that?
I hope the first method works. What's the problem?
If that works, I don't need to change any source code. Only create one makefile for each case.
May 10 '07 #7
weaknessforcats
9,208 Recognized Expert Moderator Expert
I'm getting a better picture here.

Instead of this:

Expand|Select|Wrap|Line Numbers
  1. #include "my_path1\types.h"
  2.  
  3. In another build, I want include
  4. #include "my_path2\types.h"
  5.  
How about:
Expand|Select|Wrap|Line Numbers
  1. #include <types.h>
  2. or
  3. #include "types.h"
  4.  
Using <> causes the preprocessor to search a pre-defined path (called a "standard place". Just set the path to your header in the project settings of your IDE.

Using " " causes the proprocessor to look for the header in the PWD (present working directory) which is the folder with the project file for Visual Studio. If the header is not there, the search reverts to a standard place as though you had used <> on the include.
May 10 '07 #8
AdrianH
1,251 Recognized Expert Top Contributor
The first way you suggested does not work. I changed the \ to /.
If I use
#define PATH_VALUE "mypath1/types.h"
#include PATH_VALUE
it works. But I can not combine two strings like this
#define PATH_VALUE "mypath1"
#include PATH_VALUE "/types.h"
The compile error is:
fatal error C1083: Cannot open include file: "mypath1"
It can not see the second string and combine it.

The second way you suggested is OK. Because a lot of files have the #include line and there are too much different path name. That means in every source code I must add a very long
#if
#elif
#elif
#elif
#elif
#endif
Is there a better way to do that?
I hope the first method works. What's the problem?
If that works, I don't need to change any source code. Only create one makefile for each case.
Doesn't work? What compiler are you using?

If you want to, you can something like this:
Expand|Select|Wrap|Line Numbers
  1. #define STRINGIZE(x) STRINGIZE_A(x)
  2. #define STRINGIZE_A(x) #x
  3. #define MY_PATH() my_path1
  4. #include STRINGIZE(MY_PATH()/types.h)
I am almost positive that it will work properly.

Alternatively, use an include indirection. Do this by making a header file with the #if/#elif/#endif you described and then include that header, which will then include the correct one. That will definitely work properly.

Hope that helps,


Adrian
May 10 '07 #9
Banfa
9,065 Recognized Expert Moderator Expert
Doesn't work? What compiler are you using?
String concatenation is part of the compiler, at the point that yanjie111 is talking about the preprocessor is working. The preprocessor does not do string concatenation in that way.

The preprocessor does have a string concatenation operator but I have never tried using it in a #include statement.

All in all yanjie111 I agree with weaknessforcats, you are better off just putting your #include in normally without any path and then modifying the path using compiler switches for the different builds.
May 12 '07 #10
AdrianH
1,251 Recognized Expert Top Contributor
String concatenation is part of the compiler, at the point that yanjie111 is talking about the preprocessor is working. The preprocessor does not do string concatenation in that way.

The preprocessor does have a string concatenation operator but I have never tried using it in a #include statement.

All in all yanjie111 I agree with weaknessforcats, you are better off just putting your #include in normally without any path and then modifying the path using compiler switches for the different builds.
I did something like that once, maybe its non-standard. I tried it just now on g++ and it didn't work.

There is no string concatenation operator, just a concatenation operator (##) and a stringize operator (#). The stringize operator only works for parameters, where as the concatenation operator works for everything.

Oh well. My second and third methods should work (I’ve tested the second on g++, the third will definitely work unless the include file is not in the current directory, then it will get a bit hairy.

The best way to do this is not with the preprocessor, but using the -I (capital i) switch on the compiler (or whatever switch it is, -I is somewhat standard though) to state which directory should be searched for the include files.


Adrian
May 12 '07 #11
Banfa
9,065 Recognized Expert Moderator Expert
The best way to do this is not with the preprocessor, but using the -I (capital i) switch on the compiler (or whatever switch it is, -I is somewhat standard though) to state which directory should be searched for the include files.
I definitely agree :D
May 12 '07 #12
yanjie111
14 New Member
Thanks you all.
I changed my mind. I don't use the preprocessor to change the path.
Change the path in the #include is a bad idea. We can always find a better way to do the same thing.
May 17 '07 #13
AdrianH
1,251 Recognized Expert Top Contributor
Thanks you all.
I changed my mind. I don't use the preprocessor to change the path.
Change the path in the #include is a bad idea. We can always find a better way to do the same thing.
Yup.


Adrian
May 17 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: ajikoe | last post by:
Hello, I would like to replace string with different values, For example : source = 'kode1 bla bla kode1 bla kode1' I have a list with each member will replace each of kode1. L = So the new...
6
by: msigwald | last post by:
The following line of code works, however, since my professor is a real purist of c, I would like to know if this code is valid (is it good code or a piece of crap?): #define DMP_FILE argv ...
1
by: Neil Zanella | last post by:
Hello, The MSDN documentation does not seem to mention whether the Replace string method returns a new string or modifies the existing string in place and then returns it. I would like to know...
2
by: Dmitriy Kolesnik | last post by:
Hello, all! I don't know how I can replace specified string in the txt-file from one to another. Thank You. -- __________________
0
by: Xah Lee | last post by:
Interactive Find and Replace String Patterns on Multiple Files Xah Lee, 2006-06 Suppose you need to do find and replace of a string pattern, for all files in a directory. However, you do not...
8
by: varsha.gadekar | last post by:
what is the easy way to check if the given string is substring of other string only using c++?
2
stealwings
by: stealwings | last post by:
Hello everyone; Its me again, I was wondering if some one can give me some guidelines about how to Find and replace a string after reading it from a file using i/o commands I mean, I have a file...
3
by: Hvid Hat | last post by:
Hi I want to highlight (make it bold) a word in some text I'm getting in XML format. My plan was to replace the word with a bold (or span) tag with the word within the tag. I've found the code...
17
by: Sumit | last post by:
hi, I have a url which is a char * char * url = "/url=%2Fhome%2Fsumit%2Fpackages%2Fmyxml.xml"; when i send this URL to http server it was like - "/url=/home/sumit/pacakges/myxml.xml" but...
3
by: omar999 | last post by:
im trying to replace NULL with N/A but I think I have a syntax error .. how do you replace string on recordset via classic asp so on objRS17and18("Price_Band_1") should it be something like ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.