LCC-win32: compile unidelay
lcc-win32: Introduction | Compile unidelay | Drupal Cron |
lcc-win32 C compiler. |
LCC how to compile
On the previous page I covered how to create a project folder in lcc-win32 this page looks at compiling with lcc-win32. If you followed the introduction we have a project folder named uniform_server_delay and an automatically generated source file for program unidelay. Before looking at the process in detail its worth exploring the need for this delay utility.
Background
When executing a batch file there sometimes is a requirement to pause execution before moving onto the next instruction. Unfortunately there is no inbuilt instruction to do this.
There are novel workarounds the two most common are:
Ping
PING 1.1.1.1 -n 1 -w 60000 >NUL
CHOICE
REM | CHOICE /C:AB /T:A,%1 > NUL
I favour ping because it is cross OS compatible however it fails if IP address is active.
"Choice" looks good however not all OS’es support it, in particular XP (note: It has been re-introduced in Vista)
Workarounds like this are far from ideal, a solution is to download one of the many utilities available. Can you trust them not to deliver something unexpected? With that dilemma the answer is to create your own with the added advantage it can be tailored specifically to meet your requirements.
Requirements - spec
My requirements for this delay utility are to provide a default delay of one second for use in batch file loop delays. The second requirement is to pass a parameter extending the delay for use in something like portable cron. The utility shall run hidden.
Code
The code by its nature is simple I have added no error checking and assumed the maximum delay of 9 hours would be more than adequate.
#include <stdheaders.h> #include <windows.h> int main(int argc,char *argv[]) { int delay; // Delay required int default_delay=1000; // 1000ms gives 1s delay if (argc == 2){ // If arg provided int seconds = (int)atoi(argv[1]); // Convert to int. Seconds required delay=seconds * default_delay; // Calculate total delay } else { // Arg not provided delay=default_delay; // Use default } sleep(delay); // Delay for specified time }
The default delay of one second in computer terms is long while being short in human terms to prevent annoyances.
Edit source file
- Start lcc Start > All programs > lcc-win32 > lcc-win32
If the project file was the last one you had opened page unidelay.c will display. - If the page is not displayed:
You previously had another project open close it: project > close
Open project: project > open in the pop-up window select unidelay.prj click Open
page unidelay.c will display. - Delete all the code (it was automatically generated when creating a project) and copy the above code into the file.
- Save the page file > save
Make - Compile - Run
At this stage we are ready to create our program.
|
With a working program it’s worth putting a few errors in the source code and run “make” to explore the errors reported. Only introduce a single error at a time otherwise it becomes confusing. Make sure the program works before moving onto the next step.
Debugging
You have created a program it compiles and runs however produces unpredicted results (bugs). Ideally what you need is a way to step through the program and check what values the variables contain during run time.
Debugger The debugger with all the options you can select is relatively complex. However I find the following steps quickly resolves most problems.
|
Passing parameters to a program
During development you want to test a program with parameters if that’s part of its function. The delay function has the option to accept a single parameter, which increases the delay. The following shows how to set this and test.
Configure the debugger
10 Fred 20 another_parameter 45 Note 2: Parameters are ignore when you Compile a program hence no need to change or remove them. |
Optimise and hide
This is the final part of generating any program; let the compiler optimise it and remove any debug information. I partially covered this in the introduction and will use the same images.
Enter Project > Configuration and set the following:
Compiler
|
Linker
|
The trick that hides the application is at step 5 however at step 6 it whinges about an error “Windows program but no resources defined”. Ignore this and continue with the following steps:
- Compiler > Make or press F9
- Compiler > Compile unidelay.c
- Check the size of unidelay.exe (E:\lcc\projects\uniform_server_delay\lcc)
It was 14.4K after optimisation 3.03K
Final Test
The finished program is used within batch files hence would be a good idea to test in this environment.
- Inside folder: E:\lcc\projects\uniform_server_delay\lcc
- Create a test batch file test.bat add the following code and save:
@echo off pause unidelay.exe echo Starting 10s delay unidelay.exe 10 echo End of delay pause
Run the batch file, after pressing any key a short pause (1s) before "Starting 10s delay" is displayed. After 10 seconds "End of delay" is displayed.
Summary
This and the previous page have covered a lot of ground resulting in a real program you can use. The intention was to provide a quick start guide to get you up and running more advanced features of the programming environments can be found in the manual.
Having the ability to compile small utilities opens up new areas it allows you to explore other possibilities. If a program does not meet you requirements perhaps a small utility will provide a solution. When running Drupal on a Windows machine it requires a cron-proress to be run, you can simulate this however it is not portable. The next example fills a gap with a small utility I have dubbed portable cron.
Ric |