Thread: Modular Code

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Modular Code


    I feel like I have a good understanding of C, but I have a problem that's been bugging me for a while, and I can't find anything on Google. I'd like to create modular code with interchangeable files, but I have no clue where to start. I'm just aiming for this program to work on Windows 7. I don't really need a detailed explanation, just some idea for an amateur programmer on how to go about doing this. Thanks in advance for any help!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,905
    Rep Power
    481
    Others already wrote a lot of modules. Become very good at finding them and understanding how to use and connect them.

    For instance, study GLib.

    Write a program solving this recent thread using the GLib linked list structure.

    Interested in graphics? Write a short program that displays a picture given in many formats.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0
    I understand C, but I need this to be written a little more dumbed down for me. I would just like to have separate modules of code that can be interchangeable when one is needed.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,184
    Rep Power
    2222
    It's not really a hard, tried-and-true science, but more a kind of art form. But that doesn't mean that it's hard to do.

    Do you also have a basic familiarity with C++ and object-oriented programming (OOP)? None of the fancy stuff like inheritance and virtual methods. Just the basic idea of defining data and the functions that use them, with some data and functions being public for all to access and some being private accessible only by that object. And the idea of defining objects that perform specific jobs and that other objects communicate with them through public method calls. And one goal of OOP is to create classes (the declaration of an object) that you can reuse.

    If none of that made any sense to you, don't worry. But at least now you have some idea what to look forward to -- don't worry, it's exciting! My point is that one approach to your problem is to basically design each module (ie, each separate .c source file) as if it were a C++ object. For that matter, it's common in C++ to have a separate .cpp source file for each class, so practically no difference.

    To begin with, by "module", we are talking about a separate .c source file.

    There's the concept of namespace, which actually exists in C++ now, but not in C. Your entire C program has one big happy namespace, which means that all global functions and variables (ie, all "file scope" entities) are accessible from any other module in your program. Please note that you still need to inform the other modules of those globals with header files.

    The first namespace issue is that if you create two functions with the same name in different modules, then the linker won't know which one you're trying to call and will fail. That's called a "name collision" or a "namespace collision" and it's the reason why the new C++ standard lets you create different namespaces. C has no feature to support that, so you need to implement it yourself. We do that by prefacing each function in a module with something that identifies it as being in that module; an additional advantage of this is that in a large project you know which file to go to for that function's code. For example, if the module handles serial I/O, then functions start with SIO_ ; that way the only functions that start with SIO_ will be in the sio.c module. Of course, this requires discipline on your part.

    Now, there will be functions and variables that you want to keep private to its module; you don't want them to go into that global namespace. It's simple to do that; you simply declare them as static and they can only be accessed within the module. You can even create static variables and functions by the same name in all the modules and they will never collide with each other.

    Now, write your modules so that each does its own thing. Write a module to handle serial I/O and you should be able to export it to all your other programs that need to use serial I/O.

    You know, it might help you to read up on basic OOP for these concepts of encapsulation and organizing a program based on objects instead of mushed-together mixed-up code. And keep what I've told you in mind as to how to implement the basic OOP concepts that you're learning.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Great help!


    Thanks for all the great help! This is exactly what I was looking for.
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,114
    Rep Power
    1803
    The term "modular" means different things to different people, and the mechanisms for modularisation differ between languages. In C those mechanism are the function and the translation unit(using separate compilation and linking). OOP languages such as C++ add classes, inheritance and generic programming (templates) as additional mechanisms - so intrinsically OOP provides greater flexibility for modularisation.

    Either way, it is a software design issue rather than a language issue, and in any case your aim is to minimise coupling and maximize cohesion. Google this two terms to get an idea of the principles of modular development. Start with these:

    http://en.wikipedia.org/wiki/Modular_programming

    http://en.wikipedia.org/wiki/Coupling_(computer_programming)

    http://en.wikipedia.org/wiki/Cohesion_(computer_science)

    Comments on this post

    • dwise1_aol agrees : As expressed in Msg #7.
    Last edited by clifford; November 30th, 2012 at 01:41 PM.
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,184
    Rep Power
    2222
    Clifford, adding to your reputation did not allow me enough characters to speak.

    It is often said that half the battle is in knowing that it can be done. When researching for information, half the battle is knowing what terminology to use. Professionals know what techniques to use, but may not always know what terminology that academics. Most entries in Google are based on academics' terminology.

    I knew the ideas that the OP was looking for. Clifford knew the terminology to enable the OP to learn so much more.
  14. #8
  15. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,114
    Rep Power
    1803
    If you are simply asking about the structural/syntactical mechanics of modular development in C then consider the following file set for a simple program comprising of three modules (main, module1 and module2):

    module1.h:
    Code:
    #if !defined MODULE1_INC
    #define MODULE1_INC
    
    // Module1 Declarations
    void module1_function1( void ) ;
    void module1_function1( void ) ;
    
    #endif
    module1.c:
    Code:
    #include "module1.h"
    
    // Module1 Definitions
    void module1_function1( void )
    {
        //...
    }
    
    void module1_function2( void )
    {
        //...
    }
    module2.h:
    Code:
    #if !defined MODULE1_INC
    #define MODULE2_INC
    
    // Module1 Declarations
    void module2_function1( void ) ;
    
    #endif
    module2.c:
    Code:
    #include "module2.h"
    
    // Module2 Definitions
    void module2_function1( void )
    {
        //...
    }
    main.c:
    Code:
    #include "module1.h"
    #include "module2.h"
    
    // Program from modules 1 and 2
    int main( void )
    {
        module1_function2() ;
        module2_function1() ;
        module1_function1() ;
    }
    Each of main.c, module1.c and module2.c must be separately compiled and linked to form a complete executable. If you are using some sort of IDE that can manage the process of separate compilation and linking for you my adding teh modules to the managed project. If you are using a command-line tool-chain, then large applications quickly become impractical to compile separately by hand and you would use some sort of script or more efficiently a makefile or similar build management tool.

IMN logo majestic logo threadwatch logo seochat tools logo