473,785 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to do two-stage preprocessing of one file

What I'm trying to do is have the preprocessor parse one file twice.
The file has three parts, and each is dependent on the previous.
Example (file name is foo.c):
#ifndef ONCE /* first part */
#define D #define
#define ONCE
#include "foo.c"
#endif
#ifdef ONCE /* second part */
D x int
#ifndef TWICE
#define TWICE
#include "foo.c"
#endif
#endif
#ifdef TWICE /* third part */
x y;
#endif

The problem is that the preprocessor just adds the third part to the
second part, but doesn't preprocess the third part in the process.
How do I make my #includes and #ifedefs so that the third part will be
preprocessed?

Nov 12 '07 #1
11 2367
In article <11************ *********@k79g2 000hse.googlegr oups.com>,
andreyvul <an********@gma il.comwrote:
>What I'm trying to do is have the preprocessor parse one file twice.
You cannot do that using the facilities defined by the C preprocessor.

Your particular implementation -probably- offers a way to
preprocess files. For example, on the system I am using at the
moment, the sequence would look something like,

cc -P myfile.c
mv myfile.i myfile_preproce ssed.c
cc -o outputfile myfile_preproce ssed.c

But it would also not be uncommon on a Unix system for the sequence
to look something closer to

cc -E myfile.c | egrep -v '^#line' myfile_preproce ssed.c
cc -o outfile myfile_preproce ssed.c

Many unix C compilers offer some option as in the second example,
to preprocess and send the result to standard output; it may
or may not be necessary in your toolchain to remove #line directives
from the preprocessed output before you can push the file back
through the C parseer.

If you are using one of the Windows compilers... there is probably
some similar option, but probably not named -P or -E .
--
"Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us." -- Ecclesiastes
Nov 13 '07 #2
On Nov 12, 7:11 pm, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
wrote:
In article <1194910305.752 743.81...@k79g2 000hse.googlegr oups.com>,

andreyvul <andrey....@gma il.comwrote:
What I'm trying to do is have the preprocessor parse one file twice.

You cannot do that using the facilities defined by the C preprocessor.
Oh, I meant _recursively_ preprocess the file twice.
That's why I was asking about proper #include/#ifdef structure, so
that it preprocesses like this:

#block 1
|->#block 2 (via recursive include)
|->#block 3 (via recursive include)

however, it doesn't get to |->#block 3

Nov 13 '07 #3
andreyvul wrote:
On Nov 12, 7:11 pm, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
wrote:
>In article <1194910305.752 743.81...@k79g2 000hse.googlegr oups.com>,

andreyvul <andrey....@gma il.comwrote:
>>What I'm trying to do is have the preprocessor parse one file twice.
You cannot do that using the facilities defined by the C preprocessor.
Oh, I meant _recursively_ preprocess the file twice.
That's why I was asking about proper #include/#ifdef structure, so
that it preprocesses like this:

#block 1
|->#block 2 (via recursive include)
|->#block 3 (via recursive include)

however, it doesn't get to |->#block 3
I didn't try it but something along these lines might do it:

#ifndef REC_COUNT
#define REC_COUNT 2
#elif REC_COUNT == 2
#undef REC_COUNT
#define REC_COUNT 1
#elif REC_COUNT == 1
#undef REC_COUNT
#define REC_COUNT 0
#endif
/* Here is the meat */
#if REC_COUNT == 2
/* first iteration */
#elif REC_COUNT == 1
/* second iteration */
#else
/* third and last iteration */
#if REC_COUNT != 0
#include __FILE__
#endif

--
Ark
Nov 13 '07 #4
In article <11************ **********@d55g 2000hsg.googleg roups.com>,
andreyvul <an********@gma il.comwrote:
>On Nov 12, 7:11 pm, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
wrote:
>In article <1194910305.752 743.81...@k79g2 000hse.googlegr oups.com>,
>andreyvul <andrey....@gma il.comwrote:
>What I'm trying to do is have the preprocessor parse one file twice.
>You cannot do that using the facilities defined by the C preprocessor.
>Oh, I meant _recursively_ preprocess the file twice.
Oh, in that case, let me correct my statement:

The facilities of the C preprocessor are defined in such a way that
you are not guaranteed to be able to do this; if you were able to
get it to work at all, it would be due to implementation-dependant
behaviour.
--
"History is a pile of debris" -- Laurie Anderson
Nov 13 '07 #5
On Nov 12, 7:58 pm, Ark Khasin <akha...@macroe xpressions.comw rote:
andreyvul wrote:
On Nov 12, 7:11 pm, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
wrote:
In article <1194910305.752 743.81...@k79g2 000hse.googlegr oups.com>,
andreyvul <andrey....@gma il.comwrote:
What I'm trying to do is have the preprocessor parse one file twice.
You cannot do that using the facilities defined by the C preprocessor.
Oh, I meant _recursively_ preprocess the file twice.
That's why I was asking about proper #include/#ifdef structure, so
that it preprocesses like this:
#block 1
|->#block 2 (via recursive include)
|->#block 3 (via recursive include)
however, it doesn't get to |->#block 3

I didn't try it but something along these lines might do it:

#ifndef REC_COUNT
#define REC_COUNT 2
#elif REC_COUNT == 2
#undef REC_COUNT
#define REC_COUNT 1
#elif REC_COUNT == 1
#undef REC_COUNT
#define REC_COUNT 0
#endif
/* Here is the meat */
#if REC_COUNT == 2
/* first iteration */
#elif REC_COUNT == 1
/* second iteration */
#else
/* third and last iteration */
#if REC_COUNT != 0
#include __FILE__
#endif

--
Ark
Nope, doesn't work.

Great, I have now figured out it is impossible to recursively
preprocess more than one level. Is this supposed to happen per ANSI
specs, or should I go to the cpp newsgroup?

Nov 13 '07 #6
And I got it to work now:
cc foo.c -E | sed -re '/#[^di]/D' -e /^$/D' | head -7 | cc -x c - -o
foo
Kind of impossible on windows without GnuWin32, msys, or cygwin.

Nov 13 '07 #7
andreyvul wrote:
On Nov 12, 7:58 pm, Ark Khasin <akha...@macroe xpressions.comw rote:
>andreyvul wrote:
>>On Nov 12, 7:11 pm, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
wrote:
In article <1194910305.752 743.81...@k79g2 000hse.googlegr oups.com>,
andreyvul <andrey....@gma il.comwrote:
What I'm trying to do is have the preprocessor parse one file twice.
You cannot do that using the facilities defined by the C preprocessor.
Oh, I meant _recursively_ preprocess the file twice.
That's why I was asking about proper #include/#ifdef structure, so
that it preprocesses like this:
#block 1
|->#block 2 (via recursive include)
|->#block 3 (via recursive include)
however, it doesn't get to |->#block 3
I didn't try it but something along these lines might do it:

#ifndef REC_COUNT
#define REC_COUNT 2
#elif REC_COUNT == 2
#undef REC_COUNT
#define REC_COUNT 1
#elif REC_COUNT == 1
#undef REC_COUNT
#define REC_COUNT 0
#endif
/* Here is the meat */
#if REC_COUNT == 2
/* first iteration */
#elif REC_COUNT == 1
/* second iteration */
#else
/* third and last iteration */
#if REC_COUNT != 0
#include __FILE__
#endif

--
Ark

Nope, doesn't work.

Great, I have now figured out it is impossible to recursively
preprocess more than one level. Is this supposed to happen per ANSI
specs, or should I go to the cpp newsgroup?
You didn't try hard enough :). The text I posted was not #balanced.
Here's a working example:
------------- iter.c -------------
#include <stdio.h>
int main()
{
#include "iter.h"
return 0;
}
------------- iter.h -------------
#ifndef REC_COUNT
# define REC_COUNT 2
/* first iteration */
puts("first iteration");
#elif REC_COUNT == 2
# undef REC_COUNT
# define REC_COUNT 1
/* second iteration */
puts("second iteration");
#elif REC_COUNT == 1
# undef REC_COUNT
# define REC_COUNT 0
/* third and last iteration */
puts("third and last iteration");
#endif
#if REC_COUNT != 0
# include __FILE__
#endif
------------ output --------------------
first iteration
second iteration
third and last iteration

.... which also disproves a claim of a nearby post that it's impossible.
In fact, you can do recursion of any fixed depth this way.
Unless of course I misunderstood what you were trying to achieve.

--
Ark
Nov 13 '07 #8
On Nov 12, 9:54 pm, Ark Khasin <akha...@macroe xpressions.comw rote:
andreyvul wrote:
On Nov 12, 7:58 pm, Ark Khasin <akha...@macroe xpressions.comw rote:
andreyvul wrote:
On Nov 12, 7:11 pm, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
wrote:
In article <1194910305.752 743.81...@k79g2 000hse.googlegr oups.com>,
andreyvul <andrey....@gma il.comwrote:
What I'm trying to do is have the preprocessor parse one file twice.
You cannot do that using the facilities defined by the C preprocessor.
Oh, I meant _recursively_ preprocess the file twice.
That's why I was asking about proper #include/#ifdef structure, so
that it preprocesses like this:
#block 1
|->#block 2 (via recursive include)
|->#block 3 (via recursive include)
however, it doesn't get to |->#block 3
I didn't try it but something along these lines might do it:
#ifndef REC_COUNT
#define REC_COUNT 2
#elif REC_COUNT == 2
#undef REC_COUNT
#define REC_COUNT 1
#elif REC_COUNT == 1
#undef REC_COUNT
#define REC_COUNT 0
#endif
/* Here is the meat */
#if REC_COUNT == 2
/* first iteration */
#elif REC_COUNT == 1
/* second iteration */
#else
/* third and last iteration */
#if REC_COUNT != 0
#include __FILE__
#endif
--
Ark
Nope, doesn't work.
Great, I have now figured out it is impossible to recursively
preprocess more than one level. Is this supposed to happen per ANSI
specs, or should I go to the cpp newsgroup?

You didn't try hard enough :). The text I posted was not #balanced.
Here's a working example:
------------- iter.c -------------
#include <stdio.h>
int main()
{
#include "iter.h"
return 0;}

------------- iter.h -------------
#ifndef REC_COUNT
# define REC_COUNT 2
/* first iteration */
puts("first iteration");
#elif REC_COUNT == 2
# undef REC_COUNT
# define REC_COUNT 1
/* second iteration */
puts("second iteration");
#elif REC_COUNT == 1
# undef REC_COUNT
# define REC_COUNT 0
/* third and last iteration */
puts("third and last iteration");
#endif
#if REC_COUNT != 0
# include __FILE__
#endif
------------ output --------------------
first iteration
second iteration
third and last iteration

... which also disproves a claim of a nearby post that it's impossible.
In fact, you can do recursion of any fixed depth this way.
Unless of course I misunderstood what you were trying to achieve.

--
Ark
What if you had a macro in the third iteration that was to be expanded
by the previous iterations?

Nov 13 '07 #9
On Nov 12, 9:54 pm, Ark Khasin <akha...@macroe xpressions.comw rote:
andreyvul wrote:
On Nov 12, 7:58 pm, Ark Khasin <akha...@macroe xpressions.comw rote:
andreyvul wrote:
On Nov 12, 7:11 pm, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
wrote:
In article <1194910305.752 743.81...@k79g2 000hse.googlegr oups.com>,
andreyvul <andrey....@gma il.comwrote:
What I'm trying to do is have the preprocessor parse one file twice.
You cannot do that using the facilities defined by the C preprocessor.
Oh, I meant _recursively_ preprocess the file twice.
That's why I was asking about proper #include/#ifdef structure, so
that it preprocesses like this:
#block 1
|->#block 2 (via recursive include)
|->#block 3 (via recursive include)
however, it doesn't get to |->#block 3
I didn't try it but something along these lines might do it:
#ifndef REC_COUNT
#define REC_COUNT 2
#elif REC_COUNT == 2
#undef REC_COUNT
#define REC_COUNT 1
#elif REC_COUNT == 1
#undef REC_COUNT
#define REC_COUNT 0
#endif
/* Here is the meat */
#if REC_COUNT == 2
/* first iteration */
#elif REC_COUNT == 1
/* second iteration */
#else
/* third and last iteration */
#if REC_COUNT != 0
#include __FILE__
#endif
--
Ark
Nope, doesn't work.
Great, I have now figured out it is impossible to recursively
preprocess more than one level. Is this supposed to happen per ANSI
specs, or should I go to the cpp newsgroup?

You didn't try hard enough :). The text I posted was not #balanced.
Here's a working example:
------------- iter.c -------------
#include <stdio.h>
int main()
{
#include "iter.h"
return 0;}

------------- iter.h -------------
#ifndef REC_COUNT
# define REC_COUNT 2
/* first iteration */
puts("first iteration");
#elif REC_COUNT == 2
# undef REC_COUNT
# define REC_COUNT 1
/* second iteration */
puts("second iteration");
#elif REC_COUNT == 1
# undef REC_COUNT
# define REC_COUNT 0
/* third and last iteration */
puts("third and last iteration");
#endif
#if REC_COUNT != 0
# include __FILE__
#endif
------------ output --------------------
first iteration
second iteration
third and last iteration

... which also disproves a claim of a nearby post that it's impossible.
In fact, you can do recursion of any fixed depth this way.
Unless of course I misunderstood what you were trying to achieve.

--
Ark
What if you have a macro in the third iteration that needs expanding
in the second iteration, which needs expanding in the first iteration?
*That* was what I was trying to do.

Nov 13 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1786
by: SimonC | last post by:
I'm looking to do something similar to a feature found on Ticketmaster.com, where you select your seats at a venue, and then you have two minutes in which to take or leave them. QUESTION 1a. Inside (or just after) the same query that searches for available seats, I need to SIMULTANEOUSLY mark those seats as "on hold". I've only read about, but not yet used MySQL transactions, and wonder if this simultaneous "search-and-hold"...
8
1749
by: John Grenier | last post by:
Hi, I have to determine the "standing" (WIN - TIE - LOSS) from confrontations between two teams on a contest. The table matchResults has fields cont_id, team_id and contest_result (int). TABLE matchResults cont_id team_id contest_result 1 1 3 1 2 5
6
1883
by: Willem | last post by:
Hi, I have a newbie question: is it possible to make a search form in asp that searches in two different databases (access)? Willem
6
4083
by: Matt K. | last post by:
Hi there, I have a form in an Access project that contains a subform which displays the results of a query of the style "select * from where = #a certain date#". In the main part of the form the user can change the date, which will force a requery in the subform to bring up records from the date selected. My question is this... The query in the subform is a very simple one, with only three fields being returned. In the interest of...
7
12895
by: Prabhudhas Peter | last post by:
I have two object instances of a same class... and i assigned values in both object instances (or the values can be taken from databse and assigned to the members of the objects)... Now i want to compare these two objects so that it will return true if both object's members have the same value... it is good if u can give me a single function or simple code snippet.. Thank U -- Peter...
0
1698
by: clintonG | last post by:
I applied aspnet_regsql to SQL2K which was working fine throughout Beta 2 development. After installing Visual Studio and SQL Express RTM my application has blown up. Logging in to the application became realllllllllllly slow. Content in LoginView Role Groups was not displaying even after a user in a role had logged in. It was taking about 15 seconds or so for the login control to display when the login link was selected on the homepage....
9
5275
by: Steven | last post by:
Hello, I have a question about strcmp(). I have four words, who need to be compared if it were two strings. I tried adding the comparison values like '(strcmp(w1, w2) + strcmp(w3, w4))', where w1 and w2 make up the first string and, w3 and w4 make up the second string. I do not want to allocate memory, then put the words together to create a string only to facilitate strcmp() comparison. My question; Does anyone know how to get the...
272
14191
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two dimensional arrays from std::vectors ??? I want to use normal Array Syntax.
13
5603
by: paul.joseph.davis | last post by:
Hi, I've just had my first encounter with two-phase lookup and I'm scratching my head a bit. The idea behind two phase look up is pretty easy to understand, but I have a case that fails to compile although it appears to me that it should. template<typename TYPE> void foo( std::map< int, TYPE pmap )
0
9646
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10350
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10097
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8983
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6742
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.