![]() |
C - Entwicklung Programm - Flusskontrolle: Bibliotheken Homepage von PS-Trainer - C-Entwicklung - Flusskontrolle - an PS-Trainer |
|
| Program Flow Control | Overview of subjects concerning flow control: program start and stop, branching, interation, functions... |
| Functions | Overview of subjects concerning functions |
| Standard libraries | Compiler-provided general libraries |
| Private libraries | Your own libraries containing re-usable functions. You may have programmed these functions by yourself or taken them from the internet, or books, or... |
| Sharing levels | You may share source code, compiled objects or DLL´s |
| #include | The #include directive replaces itself by the contents of the file specified with it. |
| extern | The extern keyword declares a variable or function and specifies that it has external linkage |
|
Standard libraries: |
|
Private libraries: |
||||||||||
Example:
|
| Sharing options / levels of libraries: If you have created re-usable libraries, there are several possibilities to include these in other projects, e.g.: |
Easy to do, but tedious with many projects. High flexibility - you can see the code and you may even change it. At this level, you may remove all components not necessarily needed, or you may apply some changes to the functions. If you publish source code, it is clear enough that everyone may use this code without even telling you about. You have created freeware. |
Fast and easy - good for routine work. Object code is what makes the compiler from your source code. You find the *.OBJ files in your workspace environment. The idea is to compile the library only once, as it does not change while you develop your programs. This makes you a lot faster, because usually most of your code comes from libs. Now you need to compile only a small portion of code - the program in development. The header file is needed by the compiler (it contains source code, which has to be #Include´d into your program). Once you have got both the header file and your current main code you may compile it - this step produces a new OBJ file of your current project. The compiler does not need your library - it is sufficient to provide extern declarations ! The OBJ file of the library is needed by the linker: This program puts together the OBJ´s of your current program, of your LIB, and of the standard libraries to "link" all these together, in the end resulting in an executable program. (your compiler certainly has created an OBJ file of your lib while you developed and tested it - you find the OBJ files in your lib-project environment) You must find out how your compiler allows to add or import other objects, namely object files to your project . This may be a menu item, e.g. Project - AddTo... , an intelligent workspace manager might recognize by itself that you have added (copied into it) a file, or any other function to provide object import. You may leave the OBJ file where it is (e.g. in the environment where you have created it) and "link" to it, or you may copy the OBJ file into the current workspace - this step is compiler-dependant. If you do not find out how it works, try keywords LINK, LINKER, Link OPTIONS, ... If this is not possible, or if you do not find out how it works: create a new C++ header file and copy / paste the lib´s header source code into it. You may even paste the code (e.g. extern function declarations) into another file, e.g. the one containing your main program. This method not only is fast and easy to do, it keeps the "user" developer away from your source code. You may give away or publish lib OBJects which can only be used but not modified. Keep in mind that every program can be hacked and cracked - it only depends on the effort applied. But still publishing an OBJ is an easy and efficient way to protect your work. |
Rather large effort to do. Platform-dependent !!! What you get from your library are not executable programs, but small pieces of code (or data) other programs can load and use at run-time. Creating DLL´s is a science of its own, therefore it is currently not included in this web. |
| The #include
directive The #include directive tells the preprocessor to treat the contents of a specified file as if those contents had appeared in the source program at the point where the directive appears. You can organize constant and macro definitions into include files and then use #include directives to add these definitions to any source file. Include files are also useful for incorporating declarations of external variables and complex data types. You only need to define and name the types once in an include file created for that purpose. Syntax #include "path-spec" #include <path-spec> The path-spec is a filename optionally preceded by a directory specification. The filename must name an existing file. The syntax of the path-spec depends on the operating system on which the program is compiled. Both syntax forms cause replacement of that directive by the entire contents of the specified include file. The difference between the two forms is the order in which the preprocessor searches for header files when the path is incompletely specified. Quoted form #include "path-spec" This form instructs the preprocessor to look for include files in the same directory of the file that contains the #include statement, and then in the directories of whatever files that include (#include) that file. The preprocessor then searches along the path specified by the /I compiler option, then along paths specified by the INCLUDE environment variable. Angle-bracket form #include <path-spec> This form instructs the preprocessor to search for include files first along the path specified by the /I compiler option, then along the path specified by the INCLUDE environment variable. The preprocessor stops searching as soon as it finds a file with the given name. If you specify a complete, unambiguous path specification for the include file between two sets of double quotation marks (" "), the preprocessor searches only that path specification and ignores the standard directories. If the filename enclosed in double quotation marks is an incomplete path specification, the preprocessor first searches the "parent" file's directory. A parent file is the file containing the #include directive. For example, if you include a file named file2 within a file named file1, file1 is the parent file. Nesting: Include files can be "nested"; that is, an #include directive can appear in a file named by another #include directive. For example, file2, above, could include file3. In this case, file1 would still be the parent of file2 but would be the "grandparent" of file3. When include files are nested, directory searching begins with the directories of the parent file and then proceeds through the directories of any grandparent files. Thus, searching begins relative to the directory containing the source currently being processed. If the file is not found, the search moves to directories specified by the /I compiler option. Finally, the directories specified by the INCLUDE environment variable are searched. The following example shows file inclusion using angle brackets: #include <stdio.h> This example adds the contents of the file named STDIO.H to the source program. The angle brackets cause the preprocessor to search the directories specified by the INCLUDE environment variable for STDIO.H, after searching directories specified by the /I compiler option. The following example shows file inclusion using the quoted form: #include "defs.h" This example adds the contents of the file specified by DEFS.H to the source program. The double quotation marks mean that the preprocessor searches the directory containing the parent source file first. Nesting of include files can continue up to 10 levels. Once the nested #include is processed, the preprocessor continues to insert the enclosing include file into the original source file. |
| extern extern declarator // used when variable or function has external linkage extern string-literal declarator // used when linkage conventions of another language are being used for the declarator extern string-literal { declarator-list } // used when linkage conventions of another language are being used for the declarators The extern keyword declares a variable or function and specifies that it has external linkage (its name is visible from files other than the one in which it's defined). When modifying a variable, extern specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends). The variable or function may be defined in another source file, or later in the same file. In C++, when used with a string, extern specifies that the linkage conventions of another language are being used for the declarator(s). Declarations of variables and functions at file scope are external by default. In C++, string-literal is the name of a language. The language specifier "C++" is the default. "C" is the only other language specifier currently supported by Microsoft C/C++. This allows you to use functions or variables defined in a C module. All of the standard include files use the extern "C" syntax to allow the run-time library functions to be used in C++ programs. Keywords: auto, register, static, const, and volatile. Example The following example declares the functions printf, getchar, and putchar with "C" linkage: // Example of the extern keyword extern "C" int printf( const char *, ... ); extern "C" |
| Aktuelle Daten dieser Seite | Letzte Änderung: |
| |