View unanswered posts | View active topics It is currently Wed Oct 01, 2014 4:54 pm






Reply to topic  [ 24 posts ]  Go to page 1, 2  Next
How to tell if a file is included 
Author Message
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post 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

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Thu Aug 14, 2008 12:55 am
Profile WWW
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post 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.


Thu Sep 18, 2008 8:00 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 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);

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Last edited by Ford Prefect on Mon Sep 22, 2008 2:18 pm, edited 13 times in total.



Thu Sep 18, 2008 9:22 am
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post 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.


Thu Sep 18, 2008 10:42 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 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?

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Mon Sep 22, 2008 6:16 am
Profile
Professor

Joined: Fri Sep 19, 2008 1:22 am
Posts: 200
Post 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

_________________
Jeff McBride
Benson Robotics Club


Mon Sep 22, 2008 10:17 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 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 !

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Mon Sep 22, 2008 10:43 am
Profile
Professor

Joined: Fri Sep 19, 2008 1:22 am
Posts: 200
Post 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

_________________
Jeff McBride
Benson Robotics Club


Mon Sep 22, 2008 11:09 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 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 :(

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Mon Sep 22, 2008 11:20 am
Profile
Professor

Joined: Fri Sep 19, 2008 1:22 am
Posts: 200
Post 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?

_________________
Jeff McBride
Benson Robotics Club


Mon Sep 22, 2008 11:28 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 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).

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Mon Sep 22, 2008 11:33 am
Profile
Professor

Joined: Fri Sep 19, 2008 1:22 am
Posts: 200
Post 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?

_________________
Jeff McBride
Benson Robotics Club


Mon Sep 22, 2008 11:42 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 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)

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Mon Sep 22, 2008 11:58 am
Profile
Professor

Joined: Fri Sep 19, 2008 1:22 am
Posts: 200
Post 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

_________________
Jeff McBride
Benson Robotics Club


Mon Sep 22, 2008 12:14 pm
Profile
Professor

Joined: Fri Sep 19, 2008 1:22 am
Posts: 200
Post 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

_________________
Jeff McBride
Benson Robotics Club


Mon Sep 22, 2008 12:22 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 24 posts ]  Go to page 1, 2  Next

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.