Conditional compilation in C?

Conditional compilation in C?

Post by Q Modna » Sun, 20 Jun 1999 04:00:00



Hi, wonder if anyone can advice. I've got a C program with two (yes two!)
main() functions. During compilation I want only want part to compile
depending on what has been passed to gcc.

I've heard that I should be able to do something like

        #ifdef SECTION_1
        int main(int argc, char *argv)
        {
                /* First version of main() */
                .......
        }
        #endif

        #ifdef SECTION_2
        int main(int argc, char *argv)
        {
                /* Second version of main() */
                .......
        }
        #endif

and during compile I should be able to do something like
        gcc -DSECTION_2 progname.c -o progname
to compile SECTION_2 only but I've not been very successful. Any ideas?
Thanks.

 
 
 

Conditional compilation in C?

Post by John Burto » Sun, 20 Jun 1999 04:00:00


That should work, what's the problem?

>Hi, wonder if anyone can advice. I've got a C program with two (yes two!)
>main() functions. During compilation I want only want part to compile
>depending on what has been passed to gcc.

>I've heard that I should be able to do something like

> #ifdef SECTION_1
> int main(int argc, char *argv)
> {
> /* First version of main() */
> .......
> }
> #endif

> #ifdef SECTION_2
> int main(int argc, char *argv)
> {
> /* Second version of main() */
> .......
> }
> #endif

>and during compile I should be able to do something like
> gcc -DSECTION_2 progname.c -o progname
>to compile SECTION_2 only but I've not been very successful. Any ideas?
>Thanks.


 
 
 

Conditional compilation in C?

Post by Roland Geie » Mon, 21 Jun 1999 04:00:00


Q Modnar wrote

Quote:> and during compile I should be able to do something like
>         gcc -DSECTION_2 progname.c -o progname
> to compile SECTION_2 only but I've not been very successful. Any ideas?

'Something like' won't work,  'gcc -DSECTION_2 -o progname progname.c' will.

--- Roland.

 
 
 

Conditional compilation in C?

Post by Q Modna » Mon, 21 Jun 1999 04:00:00



> That should work, what's the problem?

Yes you're right, it does work :) There was a bug elsewhere (I've actually got
so many mains() and #ifdef #ifndef #elif etc. that one of the conditions were
wrong). Simplifying to the form I posted on this newsgroup actually showed
that the fault wasn't with the -D argument. Thanks all the same to everyone
who replied and sorry for the trouble!
 
 
 

Conditional compilation in C?

Post by Aaron Cran » Mon, 21 Jun 1999 04:00:00




> Q Modnar wrote
> > and during compile I should be able to do something like
> >         gcc -DSECTION_2 progname.c -o progname

> 'Something like' won't work,  'gcc -DSECTION_2 -o progname progname.c'
> will.

For suitably large values of `won't work'.

    $ echo 'int main(void) { puts("Hello, world!"); return 0; }' > hello.c
    $ gcc -DFOO hello.c -o hello
    $ ./hello
    Hello, world!

The GNU getopt routines don't by default impose any requirements on the
order of option and non-option arguments.

--

 ** Please send on-topic followups by Usenet, not email **

 
 
 

Conditional compilation in C?

Post by Q Modna » Tue, 22 Jun 1999 04:00:00



> For suitably large values of `won't work'.
> ....
> The GNU getopt routines don't by default impose any requirements on the
> order of option and non-option arguments.

I don't understand....could you expand on that?
Thanks.
 
 
 

Conditional compilation in C?

Post by Aaron Cran » Tue, 22 Jun 1999 04:00:00





> > For suitably large values of `won't work'.  The GNU getopt routines
> > don't by default impose any requirements on the order of option and
> > non-option arguments.

> I don't understand....could you expand on that?

Yes, but your removal of the context makes it harder.

The article I was responding to claimed, essentially, that

    gcc foo.c -o foo

would not work, while

    gcc -o foo foo.c

would.  The only difference between these two is the relative order of
option arguments (`-o foo') and non-option arguments (`foo.c'); and indeed,
traditional getopt() implementations require all option arguments to appear
before all non-option arguments.  (getopt() is the standard (POSIX, Single
Unix, etc.) function to parse command-line arguments in C programs.)
However, the GNU implementation of getopt() does not have this restriction
by default.  You can set the environment variable POSIXLY_CORRECT to enable
this restriction for GNU getopt(); but gcc itself doesn't use getopt() and
ignores this environment variable.

--

 ** Please send on-topic followups by Usenet, not email **

 
 
 

Conditional compilation in C?

Post by Rya » Wed, 23 Jun 1999 04:00:00



Quote:

>Hi, wonder if anyone can advice. I've got a C program with two (yes two!)
>main() functions. During compilation I want only want part to compile
>depending on what has been passed to gcc.

>I've heard that I should be able to do something like

>    #ifdef SECTION_1
>    int main(int argc, char *argv)
>    {
>            /* First version of main() */
>            .......
>    }
>    #endif

>    #ifdef SECTION_2
>    int main(int argc, char *argv)
>    {
>            /* Second version of main() */
>            .......
>    }
>    #endif

>and during compile I should be able to do something like
>    gcc -DSECTION_2 progname.c -o progname
>to compile SECTION_2 only but I've not been very successful. Any ideas?
>Thanks.

I would prefer the following:

int
main( int argc, char** argv )
{
#ifdef SECTION_1
...
#endif
#ifdef SECTION_2
...
#endif

Quote:}

so you only have one main function, but use preprocessor directives to
control what you do inside.  IMHO, this seems less confusing than
having more than one version of the same function.
Perhaps you have a good reason for doing it the other way.

======
Remove nospam information if you must reply via e-mail.


======