Date: Wed, 30 Dec 2009 09:32:49 +0000 From: Matthew Seaman <m.seaman@infracaninophile.co.uk> To: Oliver Mahmoudi <olivermahmoudi@gmail.com> Cc: "freebsd-questions@FreeBSD.org" <freebsd-questions@freebsd.org> Subject: Re: how does the C pre-processor interface with make? Message-ID: <4B3B1E41.4010104@infracaninophile.co.uk> In-Reply-To: <6b4b2d2c0912292317h7c4d9bbfo196dcf48396e0afa@mail.gmail.com> References: <6b4b2d2c0912292317h7c4d9bbfo196dcf48396e0afa@mail.gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
This is an OpenPGP/MIME signed message (RFC 2440 and 3156)
--------------enigC23A6DBAF5F7C2611DC5979A
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: quoted-printable
Oliver Mahmoudi wrote:
> Hey folks,
>=20
> I was wondering how the C pre-processor interfaces with make. Let's sup=
pose
> that I have a little C program, something along the lines of:
>=20
> #include <stdio.h>
>=20
> int
> main()
> {
> #ifdef FOO
> fprintf(stdout, "Hi, my name is foo.\n");
> #endif
>=20
> #ifdef BAR
> fprintf(stdout, "Hi, my name is bar.\n");
> #endif
>=20
> fprintf(stdout, "I am always here.\n");
>=20
> return(1-1);
> }
>=20
> It is easy to control the pre-processor in the above code by defining t=
he
> respective terms or leaving them out, however,
> I am trying to control the conditionals from out of a makefile.
>=20
> This is essentially the way in which the kernel sources are compiled.
>=20
> Any suggestions?
Yes. You have to define your FOO and BAR variables on the cc(1) command
line using the -D flag, based on the equivalent settings in make. Suppos=
ing
your little program is held in a file wibble.c, then in your Makefile you=
'ld
need something like this to compile and link it into a program called wib=
ble:
=2Eif defined(FOO)
CFLAGS+=3D -DFOO
=2Eendif
=2Eif defined(BAR)
CFLAGS+=3D -DBAR
=2Eendif
wibble: wibble.c
${CC} ${CFLAGS} ${LDFLAGS} ${.ALLSRC} -o ${.TARGET}
Then you'ld invoke make(1) as:
% make -DFOO -DBAR wibble
or define FOO and BAR in the shell environment, which make(1) should pick=
up.
Cheers,
Matthew
--=20
Dr Matthew J Seaman MA, D.Phil. 7 Priory Courtyard
Flat 3
PGP: http://www.infracaninophile.co.uk/pgpkey Ramsgate
Kent, CT11 9PW
--------------enigC23A6DBAF5F7C2611DC5979A
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iEYEAREIAAYFAks7HkcACgkQ8Mjk52CukIxEGQCdFTuhkHJlkEM91SmWsSW3ZHcd
9mgAn2W2aiD1tmbOzJooj8C4v2vyZ5/5
=7JiM
-----END PGP SIGNATURE-----
--------------enigC23A6DBAF5F7C2611DC5979A--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4B3B1E41.4010104>
