How to tell if a file is included
Author |
Message |
mightor
Site Admin
Joined: Wed Mar 05, 2008 8:14 am Posts: 3654 Location: Rotterdam, The Netherlands
|
 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: 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 |
|
 |
Dick Swan
Creator
Joined: Fri Feb 09, 2007 9:21 am Posts: 616
|
 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 |
|
 |
Ford Prefect
Guru
Joined: Sat Mar 01, 2008 12:52 pm Posts: 1030
|
 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., or
_________________ 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 |
|
 |
Dick Swan
Creator
Joined: Fri Feb 09, 2007 9:21 am Posts: 616
|
 Re: How to tell if a file is included
I don't understand what you mean by the following. __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 |
|
 |
Ford Prefect
Guru
Joined: Sat Mar 01, 2008 12:52 pm Posts: 1030
|
 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 |
|
 |
Jeff McBride
Professor
Joined: Fri Sep 19, 2008 1:22 am Posts: 200
|
 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: Jeff
_________________ Jeff McBride Benson Robotics Club
|
Mon Sep 22, 2008 10:17 am |
|
 |
Ford Prefect
Guru
Joined: Sat Mar 01, 2008 12:52 pm Posts: 1030
|
 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 |
|
 |
Jeff McBride
Professor
Joined: Fri Sep 19, 2008 1:22 am Posts: 200
|
 Re: How to tell if a file is included
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 |
|
 |
Ford Prefect
Guru
Joined: Sat Mar 01, 2008 12:52 pm Posts: 1030
|
 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 |
|
 |
Jeff McBride
Professor
Joined: Fri Sep 19, 2008 1:22 am Posts: 200
|
 Re: How to tell if a file is included
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 |
|
 |
Ford Prefect
Guru
Joined: Sat Mar 01, 2008 12:52 pm Posts: 1030
|
 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 |
|
 |
Jeff McBride
Professor
Joined: Fri Sep 19, 2008 1:22 am Posts: 200
|
 Re: How to tell if a file is included
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 |
|
 |
Ford Prefect
Guru
Joined: Sat Mar 01, 2008 12:52 pm Posts: 1030
|
 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 |
|
 |
Jeff McBride
Professor
Joined: Fri Sep 19, 2008 1:22 am Posts: 200
|
 Re: How to tell if a file is included
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: 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 |
|
 |
Jeff McBride
Professor
Joined: Fri Sep 19, 2008 1:22 am Posts: 200
|
 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: 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 |
|
|
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
|
|