Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Don't know if its art, but I need to know!

 
Aaron Roberts
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In C and C++ compilers there are often preprocessor commands. I am trying to duplicate some of the functionality I'm used to in other languages. Examples of things I would like to do -
- Be able to reference the build/compile time in my code
- Same as above but for the date
- Reference the line number the compiler is at
I know there are others, but I can't think of specifics right now. Some pseudo code in C/C++ demonstrating the above might be

Where __CTIME__ and #COMP_TIME are the compile time parameters.
Any thoughts?
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, u can use java.util.GregorianCalendar class to do things with the Time and Date stuff...
Java does have java.lang.Runtime class to deal with the low level commands as well... But there is a limitation on how low Java can dig into the system...
 
Aaron Roberts
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I realize I can do date and time things, but the idea here is that the compiler is replacing, statically, those variable names. If today was Monday, 11 Jan 2004, then the __CTIME__ would be replaced during the compile only with that value. Its nothing that happens at runtime per se.
Its not something which is done very often, but when you need it, its handy to have.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess u might have to use Java Native Interface to apply the C/C++ native variables in the program...
I'm not that sure... But there are experts in the Other Java APIs forum who can help u in dealing with those native stuff...
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Java has no preparser such constructs are not available.
In C(++) the construct you mention would technically NOT give you the compilation time but the time the preparser was run at. This does not have to be the compilation time (it is possible to run only the preparser, and in large projects or on slow computers it is quite possible that there is a considerable (in machine terms) gap of several seconds if not longer between the preparser running and the compiler running.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are using Ant to do this. Basically, we do have a template file named Version.src with some java code declaring constants and placeholders for the actual values. The Ant script will copy the file to a Version.java file, thereby replacing the placeholders with actual values (using filters). Than the code gets compiled and other classes are able to refer to the constants with the current values.
Did that help?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic