|
THS 2011-2012 |
CProg /
The Files of a ProgramSource FilesThe functional code of a program resides in source files. Usually, when using the C language, these files have a ".c" extension, but they are still text files. When you are writing code for you robot, the control algorithms you design will probably be written into a source file named "main.c". This file contains a special C function: The main function. When you compile your program and run it, the main function is the first function that the computer executes. Every C program must one and only one main function. There is also a file named "create.c" which contains the functions that you will use to communicate with the Create. These group of functions can be called an Application Programming Interface, or just API, because the provide you with a high level interface (C language functions) to lower level functions (OI Commands) that you can use to program your application. This source file does not have a main function, so it needs to be compiled with another source file that has a main function for it to work. In order to have the "create.c" file work with the "main.c" file, you need to let "main.c" know that "create.c" exists. You do this by using header files. Header FilesHeader files are also text files, with a ".h" file extension. These files are the 'introductions' to your source files, containing the names of functions in your source files, special data types that are used, constants that are important to your algorithms, and documentation of how your functions are used. For example, if your program did very simple physics modeling, you might decide to have constant called EARTH_GRAVITY, which has a value of 9.8, or EARTH_MASS, which has a value of 5.9736*10^24. These constants would be listed in the header file. Also, if you wrote a function called 'Displacement' in your source file, you would also list it's name in the header file. You don't write any code for your functions in the header file, you only 'introduce' them. The file "create.h" is the header file for the "create.c" source file. Unless you decide to make a giant program for your robot, with many source files, you probably won't need to make any header files.
Object FilesWhen you have finished writing your code and have given it to the compiler, it will try to translate from the C language to machine language. When it does, it will put the translated code into object files. If you try to open an object files with a text editor, all you will see is a bunch of 'junk'. In a text file, the ones and zeros that make up the data are ordered so that a text editor can display them, but in an object files, the ones and zeros are ordered so that the computer can execute them. When all the source files have been translated into object files, the linker can combine them into an executable file. Executable FilesThe executable file is the final result of the compilation and linking. At this point, you can run your program and see what it does , and hopefully it will do exactly what you intended. Alot of times, however, it won't, and you will end up rebuilding this sucker again and again.
Makefile'sMakefiles arn't really a part of the C language, but since we will be using one, and because they're almost essential for complex programs, they are worth mention. A Makefile is a script that will tell the compiler what files it should use, in a specific order, with any special options you require. In WinAVR, a Makefile is used to give the compiler and linker special options to make sure the executable file is specialized to run on the Command Module. In the real world, a software company may have a program that is written over hundreds of source files. Without a Makefile, the programmers would have to remember the order they needed to compile their files, and then type in all the names of those hundreds of files every time they wanted to make an executable file for testing. That might take even longer than writing the program!
In this course, you don't have to worry about writing Makefiles. Just feel reassured that you have one of these helpful little filess watching your back.
<< Basic Tools | Home Page | A Simple Program >> Have a Question? Please ask below. |