ROBOTC.net forums
http://www.robotc.net/forums/

How to tell if a file is included
http://www.robotc.net/forums/viewtopic.php?f=1&t=681
Page 1 of 2

Author:  mightor [ Thu Aug 14, 2008 12:55 am ]
Post subject:  How to tell if a file is included

Hey guys,

I am keen to know whether it is possible to know whether a file is being used as an include file or the main source file. I'd like to use pre-compiler commands to achieve this. In Python I would do something like this:
Code:
if __name__ == "__main__":
    main()

I want to do something "like" this in RobotC. That way my code can be used as a normal driver file or a stand-alone source file with a main() task. Am I making sense? The main() task should be inside a #ifdef/#endif pair that will automatically be enabled/disabled depending on how the file is used.

Regards,
Xander

Author:  Dick Swan [ Thu Sep 18, 2008 8:00 am ]
Post subject:  Re: How to tell if a file is included

SO if there was a built-in / predefined preprocessor variable ("__name__") containing the name of the main file being compiled (just the name and without the file extension), this would work for you?

I've added, for next release, two new preprocessor symbols. "__Name__" which contains the "short name" (i.e. no directories, no file extension) of the main file. The second is "__File__" which is the name of the current file.

Author:  Ford Prefect [ Thu Sep 18, 2008 9:22 am ]
Post subject:  Re: How to tell if a file is included

??
how should this work?
how can double or crosswise file references be included by this?

e.g.,

Code:
main.c
======= 
#include muxers.h 
#include math.h
//...
int i,x,y;
i=max(x,y);



Code:
muxers.h
======= 
#include math.h
int i,x,y;
i=max(x,y);

//...

Code:
math.h
======
int max(int x, int y) {x>y? return x:retun y;}


or
Code:
main.c 
======= 
#include io.h
int j;
println(0,"d",j);

Code:
io.h
======= 
#include math.h
#define println nxtDisplayTextLine
int myfilesize, old_filesize, current_filesize;
myfilesize=max (old_filesize, current_filesize);

Code:
math.h
======
int max(int x, int y) {x>y? return x:retun y;}
int i*=2;
println(1,"%d",i);

Author:  Dick Swan [ Thu Sep 18, 2008 10:42 am ]
Post subject:  Re: How to tell if a file is included

I don't understand what you mean by the following.
Quote:
how should this work?
how can double or crosswise file references be included by this?

__NAME__ will simply define the name of the "root" file being compiled.
__FILE__ will define the name of the file that is currently been parsed. It will be redefined every time a new #include file is entered/exited.

Author:  Ford Prefect [ Mon Sep 22, 2008 6:16 am ]
Post subject:  Re: How to tell if a file is included

sorry, I don't get it.
Can you give me the code how my 2 examples above will look like using this?

Author:  Jeff McBride [ Mon Sep 22, 2008 10:17 am ]
Post subject:  Re: How to tell if a file is included

In either case:
In main.c: both __NAME__ and __FILE__ will be defined as "main.c".
In base1.h: __NAME__ will be "main.c" and __FILE_ will be "base1.h".
In base2.h: __NAME__ will be "main.c" and __FILE__ will be "base2.h".

In order to avoid duplicate references you need to use a structure like this in every file ever used as an include:
Code:
//base1.h
#ifndef _Base1h
#define _Base1h
...
#endif


Jeff

Author:  Ford Prefect [ Mon Sep 22, 2008 10:43 am ]
Post subject:  Re: How to tell if a file is included

thank you a lot for your efforts!
Unfortunately this is too weired for me - I really don't understand it, sorry.

@developers: plz make duplicate and cross references possible, just like it is possible in REAL C !

Author:  Jeff McBride [ Mon Sep 22, 2008 11:09 am ]
Post subject:  Re: How to tell if a file is included

Ford Prefect wrote:
thank you a lot for your efforts!
Unfortunately this is too weired for me - I really don't understand it, sorry.

@developers: plz make duplicate and cross references possible, just like it is possible in REAL C !


I'm sorry but now you've lost me. What are you trying to do in RobotC that isn't working the way you expect? Can you give us a concrete example? The only truely weird thing about RobotC is that every program has one source file which may or may not have include files. In standard C a program is usually compiled from multiple source files each of which may or may not have include files. This is an unfortunate but understandable limitation.

Jeff

Author:  Ford Prefect [ Mon Sep 22, 2008 11:20 am ]
Post subject:  Re: How to tell if a file is included

I tried the same as Mightor, but the detour with --NAME__ and __FILE__
and all the define's and ifdef's or ifndef's I don't understand.

I just want the developers to make things possible like it's possible in common (real) C compilers, there you don't need to make such mental gyrations either :(

Author:  Jeff McBride [ Mon Sep 22, 2008 11:28 am ]
Post subject:  Re: How to tell if a file is included

Ford Prefect wrote:
I tried the same as Mightor, but the detour with --NAME__ and __FILE__
and all the define's and ifdef's or ifndef's I don't understand.

I just want the developers to make things possible like it's possible in common (real) C compilers, there you don't need to make such mental gyrations either :(


Unfortunately, that is where you are confusing me. The stuff we've been talking about IS standard C. It is standard practice to use:

#ifndef FOO
#define FOO
...
#endif

around the contents of an include file. All it means is that if you accidentally get the same file included twice you don't wind up with compiler errors or other problems. The __FILE__ define is also a standard.

Can you give me an example of what you don't think is working like standard C?

Author:  Ford Prefect [ Mon Sep 22, 2008 11:33 am ]
Post subject:  Re: How to tell if a file is included

yes, I've written my 2 examples above!

BTW: surely ifdef etc is "common" C, but you don't need to use it if you have double or cross references
(see my 2 examples:
- 2 files (main and header) include the same header file or
- 2 header files include each other).

Author:  Jeff McBride [ Mon Sep 22, 2008 11:42 am ]
Post subject:  Re: How to tell if a file is included

Ford Prefect wrote:
yes, I've written my 2 examples above!

BTW: surely ifdef etc is "common" C, but you don't need to use it if you have double or cross references
(see my 2 examples:
- 2 files (main and header) include the same header file or
- 2 header files include each other).


I did see your posts but they aren't concrete examples. What exactly are you trying to do and what do you expect to happen. How is the behavior you are seeing from RobotC different from the behavior you expected?

Author:  Ford Prefect [ Mon Sep 22, 2008 11:58 am ]
Post subject:  Re: How to tell if a file is included

I got error messages like "file already has been opened".

I meanwhile edited my examples above, maybe it's more clearly now.

but don't worry, I packed all code into 1 header file and it works (although it's rather unconvenient if other projects need parts of those headerfiles and I have to copy them each time anew)

Author:  Jeff McBride [ Mon Sep 22, 2008 12:14 pm ]
Post subject:  Re: How to tell if a file is included

Ford Prefect wrote:
I got error messages like "file already has been opened"

but don't worry, I packed all code into 1 header file and it works (although it's rather unconvenient if other projects need parts of those headerfiles and I have to copy them each time anew)


OK. I think I understand what you are complaining about. Regardless of what C compiler you are using, best practices dictate that you should avoid including the same file twice. Due to the way that RobotC handles include files, I suggest using this kind of structure:

Code:
// main.c
#ifndef _Base1h
#include "base1.h"
#endif
...

// base1.h
#define _Base1h
...


This practice would ensure that no matter which headers you included in main or in what order you would never need to worry about duplicated symbols or compiler errors. The key part here is that you SHOULD use #ifndef's to prevent duplicate (or even worse recursive) includes no matter what C compiler you are using.

Jeff

Author:  Jeff McBride [ Mon Sep 22, 2008 12:22 pm ]
Post subject:  Re: How to tell if a file is included

Just to give you a real-world example, here is the beginning of math.h that is included with Microsoft's compiler:

Code:
/***
*math.h - definitions and declarations for math library
*
*       Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*       This file contains constant definitions and external subroutine
*       declarations for the math subroutine library.
*       [ANSI/System V]
*
*       [Public]
*
****/

#ifndef _INC_MATH
#define _INC_MATH
...


No matter how many places in your include tree math.h appears the contents will really only be included once. The problem with RobotC is that it will not open and read the contents of the same file twice so you can't use this method. You have to use the #ifndef test outside of the #include to prevent the file from being opened a second time.

This, in fact, would be a good thing for Dick and his gang to consider fixing. It is much better practice to put the checks in the include files themselves rather than around every referece to them.

Jeff

Page 1 of 2 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/