Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditional #include's 1

Status
Not open for further replies.

tcollins

Programmer
Sep 12, 2000
34
US
I'm trying to compile for different cases. In some cases I have a desired library installed, in others I don't. I want the same piece of code to manage both. So if I have:

#include <dataacq.h>

and this file exists, everything is fine. If it does not exist, I get a [pre]compiler error, ofcourse, because it tries to load a file that doesn't exist. Is there any way I can get it to load if it exists and not if it doesn't? Can I do anything like:

#if #fexists(dataacq.h)
#include <dataacq.h>
#define DATAACQ_EXISTS
#endif

Using a user-managed compiler #define is a little hard to manage:
#define _DAQ //remove if no DAQ is present
#ifdef _DAQ
#include <dataacq.h>
#endif

Thoughts? Suggestions? Thank you.
 
tcollins,

A number of options are available to you.

You can actually define a macro on the compiler command line. For unix it would be:

cc myFile.c -D_DAQ -o myProgram.

Also, in unix (and maybe Windoze) your environment variables are automatically defined as macros in the makefile. You could then GNU make to conditionally define the macro. For example:

ksh> export HAVE_DAQ=Y

============ Makefile (using GNU make) ============

ifeq ($(HAVE_DAQ), 'Y')
MACROS = -D_DAQ
else
MACROS =
endif

myProgram: myFile.c
cc myFile.c $(MACROS) -o myProgram

The third option is by far the most complicated and I mention it just for reference. It is to use the autoconf tool. You can get it at
Brudnakm.
 
Yes, I see how that would go, with a good compiler. It is my fault that I did not qualify my situation. I'm looking for an ANSI C type (standard) solution because the choice of devlepement platform is somewhat limited for now. We have no 'make' files. I have the software vendor looking into this problem as well for me, but so far tech support doesn't know. Any other solutions?
Thanks.

 
What is your platform?
What is your compiler?
What is your criterion for choosing different include files?

I am sure any (even simple one) compiler should have option of setting macro diffinition in command line or as environment value.
 
Its LabWindows/CVI (ANSI C) (Measurment Studio) from National Instruments running on MS Windows 9x. The compile itself does &quot;compiler defines&quot; and such, but I'm looking for an automated solution. I don't want to have to hand 'tweak' the code from situation to situation. I just need to to detect the existance of a file...if its there, #include it and do this, if not, don't #include it and do that.

Thoughts on the matter? Or is my only option to have a user-managed compiler define since I can't use make files???
 
In desperate situation You can use BAT file.
First set define, kinda:
#ifdef MODEA
#include <file1.h>
#endif

#ifdef MODEB
#include <file2.h>
#endif

then create bat file:

@echo off
rem check file existense
if exist &quot;c:\include\file.h&quot; goto MODEA
goto MODEB

:MODEA
echo Run your compiler string with define MODEA
goto End

:MODEB
echo Run your compiler with define MODEB
goto End


:End
echo &quot;Done&quot;
goto:EOF

Ugly, but should work in your case.

Also makefile has nothing to do with compiler itself, nmake should work with any compiler and there is some free makefile intepritators. If you starting compilation directly from some IDE it can be a problem to do any customization. Hope you will be able to call bat file.
Regards.
 
I was looking at that route as well. Its a little nasty, but might be the only way to go until we get something better. As it seems to appear there is no precompiler way to do it this is the road I may be forced to take.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top