We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
It happened again.
I have many cases of modules with something like #ifdef TYPEA ... many lines #endif #ifdef TYPEB ... many similar lines #endif #ifdef TYPEC ... many similar lines #endif
It happens that I am looking at the type b code trying to figure out something in type a (the codes are VERY similar).
To avoid this, I have tried
blah,blah // comment TYPEA
and that "kind of works"
Now, the ideal would be if for type a I could do the following: #ifdef TYPEA #define ATYPE #define BTYPE // #define CTYPE // #endif
and then just make it ATYPE blah,blah // comment
Of course this does not work. Does anyone have a trick that makes this possible?
Erik
The build works just fine, there is no problem there, if you understand this to be a build problem, this thread must have gone astray.
It went astray when you failed to notice that changes to the build system are exactly what it takes to solve your authoring confusion. You want to modify the syntax of the file you edit, to make more explicit which variant each line of that file belongs to. So far, so good. But the you want to do that, this file would be incompatible with C source file structure. So it has to be input to something else than the C compiler: a preprocessor is needed. That's exactly what I proposed 'sed' for: it can handle the file structure you want, and turn it into compilable C.
This thread was originated because I made some changes in a ~200 line segment for type q which should have been inserted in the ~200 line segment for type x.
In this particular case, I really can't see why you want to keep all these variants in one file --- 200 lines is well worth starting a new file. This would be a lot easier by putting these segments into separate files, and then in the main file just writing
#if defined(VARIANT_A) # include "fragment_foo_A.c" #elif defined(VARIANT_B) # include "fragment_foo_B.c" #elif defined(VARIANT_C) # include "fragment_foo_C.c" #endif
"This would be a lot easier by putting these segments into separate files, and then in the main file just writing ..."
And for #include, the preprocessing tokens after include in the directive are processed just as in normal text (i.e., subject to macro replacement), so one can locate all the names in the source file's front matter or separate header file, leaving the code in the function's body relatively clean and free of #if/#elif/#else/#endif if that's desireable.
#if defined(VARIANT_A) # define FRAGMENT_FOO1 "fragment_foo1_A.c" # define FRAGMENT_FOO2 "fragment_foo2_A.c" # define FRAGMENT_FOO3 "fragment_foo3_A.c" #elif defined(VARIANT_B) # define FRAGMENT_FOO1 "fragment_foo1_B.c" # define FRAGMENT_FOO2 "fragment_foo2_B.c" # define FRAGMENT_FOO3 "fragment_foo3_B.c" #elif defined(VARIANT_C) # define FRAGMENT_FOO1 "fragment_foo1_C.c" # define FRAGMENT_FOO2 "fragment_foo2_C.c" # define FRAGMENT_FOO3 "fragment_foo3_C.c" #endif void foo(void) { #include FRAGMENT_FOO1 /* * ... some code ... */ #include FRAGMENT_FOO2 /* * ... some more code ... */ #include FRAGMENT_FOO3 }
#if defined(VARIANT_A) # include "fragment_foo_A.c"
Anyhow, Hans-Bernhard, I do appreaciate your efforts, believe me. However I was looking for a simple solution to something that happens about once or twice a year.
As to the #include, I do that by a switch in the ,bat file like this fragemet show
IF "%1"=="b" goto lcb IF "%1"=="ba" goto lcba ......
:lcb copy ..\ss\usmainb.c usmain.c >..\trash\trashbin copy ..\ss\usledst1.c usledstb.c >..\trash\trashbin copy ..\ss\ussgndv1.c ussgndvr.c >..\trash\trashbin goto lxx
Again, I apprecieate your efforts and am sure that your solutions work, but none accomplish the original desire of a simple solution, not to a problem, but to an annoyance.
Lets us stop here