Thread: Separating modules into different components

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    6

    Separating modules into different components

    Hey,

    I'm trying to separate some commonly used modules - for instance, a full socket server.

    I've put everything into individual folders and am creating Makefiles for each folder (this is a Linux server, btw). The problem is, the Server component needs to call an object which is used in every project as the 'event handler' so to speak - an event processing the command the client has said. Not only that, but occasionally the programs logic will need to send commands to the client at its will - not initiated by a client event (such as a regular ping, or a new message).

    I want to somehow create - and preferably compile - the entire server module in to .o files ready for full linking to the project in question. Then, be able to iteratively go through the recompile all projects that rely on said Server module without editing them - AND without having the server need to be modified for each project.

    Is there another way other than a common 'interface' object that must be created to talk between the Server and the rest of the program? This would require a class within the Server folder that can be used for communication between Server and the program. Also, is there a way to compile just classes that have no implementation (ie, are missing a main() function)?

    Basically - is my logic here correct or is there some super-elegant way I could be doing this?

    Cheers!

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I want to somehow create - and preferably compile - the entire server module in to .o files ready for full linking to the project in question. Then, be able to iteratively go through the recompile all projects that rely on said Server module without editing them - AND without having the server need to be modified for each project.
    So you want to make libraries. A general pattern for the makefile would be:

    Code:
    SRCS = serv1.c serv2.c serv3.c
    OBJS = $(SRCS:.c=.o)
    
    SERVER = libserv.a
    
    libserv.a: $(OBJS)
            ar ruvs $@ $?
    
    clean:
            rm -f $(OBJS) $(SERVER)
    Each library you want to create will have its own makefile which would look something like that. Obviously a real makefile is more complex, but that's the meat of it.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    6
    Thanks, brewbuck.

    Libraries - I've heard of these used with regards to linking, so that makes sense - cheers, gives me something to look up.

    Tell me - how would I then go about using that libary within another project? Just by including it in -lserver.a ? Or some other syntax? (As the only library I'm really familiar with is -lpthreads - so perhaps the syntax is different from user-defined libraries).

    Also - how do I solve the problem that the server somehow has to have (2-way) communication with the rest of the program? What would the interfaces be there, or do I simply have to create an interim object and communicate between them, like:
    Code:
    class Server
    {
    //....
    public: void SetClient(ClientObject * client){...}
    private: void ReceivedData()
    {
     m_ClientObject->SendData(data);
    ClientObject * m_ClientObject;
    }
    //...
    };
    And then - upon creating an instance of Server - set the client for 2-way communication?

    Cheers

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MrSparky View Post
    Tell me - how would I then go about using that libary within another project? Just by including it in -lserver.a ? Or some other syntax? (As the only library I'm really familiar with is -lpthreads - so perhaps the syntax is different from user-defined libraries).
    Close -- you use "-lserver" not "-lserver.a". You leave the "lib" off the front and the ".a" off the back. And the code which uses the library will probably need access to the relevant header files.

    Also - how do I solve the problem that the server somehow has to have (2-way) communication with the rest of the program?
    Typically a library has none or very limited "two way" communication. Communication can occur via callbacks, returns values, or exceptions. If the library actually depends on symbols from some other library, then these two libraries are said to be "coupled" and conceptually, they might actually be better represented as a single library.

    One of the more difficult parts of library design is coming up with a way for "user code" (i.e., the code which calls your library) to communicate with it. It takes practice, and lots of failure, before you start getting the hang of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 AM
  2. win32 GUI components
    By abraham2119 in forum Windows Programming
    Replies: 5
    Last Post: 06-18-2009, 10:18 AM
  3. Sugguestion on choosing my modules
    By ssharish2005 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-27-2007, 05:44 PM
  4. Multiple Modules
    By rwmarsh in forum C++ Programming
    Replies: 6
    Last Post: 02-18-2006, 04:34 PM
  5. Problems with modules
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2005, 08:56 PM