Some general questions about C++ and .lib's (how to make...)

Posted on 2004-10-22
Last Modified: 2010-04-01
Hi, I have some little general questions.
When you have a block of code which consist mainly of two parts:
->1)initialisation and conversion of input
->2)calculation of a number, which makes use of the initialised and conevrted data
And you have to do the second part 1000's of times;
What is then the fastest way to do that (since it's difficult to make a function with 20 arguments or so...).
I know that you can put it just in a loop, but that's not what I want (I want it to be outside the code).
Can you easily make a lib with two functions (void Init() and Calculate(lots of parameters) from normal code (main()) ? If so, where can I find a tutorial? I searched but didn't find anything usable.

Question by:DEEGBAL
    LVL 17

    Expert Comment

    You are probably looking at writing a class.


    class X {
            // here's the class data...
            // ...
            Init(); // Initialises the class data
            Calculate(); // Runs calclations based on the class data
    LVL 19

    Assisted Solution

    >> Can you easily make a lib with two functions (void Init() and Calculate(lots of parameters) from normal code (main()) ? If so, where can I find a tutorial? I searched but didn't find anything usable.

    I think what you are after is a DLL.  Read for more info.  You will get the LIB when you compile the DLL.

    Author Comment

    and is this correct:
    ->a lib is for static linking
    ->a dll is for dynamic linking
    LVL 17

    Accepted Solution

    In the Windows world a .lib is used for static linkage and .dll is used for dynamic linkage.

    Your options are as follows:

    (1) Distribute your class implementation and header source code (.cpp and .h). Advantages, somebody can choose to compile with different compiler settings than those you chose.

    (2) Distribute an object file compiled from your source code with the header (.obj and .h). Advantages (dubious), you keep your source code secret and save the fraction of a second required to recompile it.

    (3) Distribute a library for static linkage and your header (.lib and .h). Advantages, you can distribute a whole load of .obj files in a .lib and it should be easy for you client to link the .obj files into his project.

    (4) Distribute a dynamic linked library for dynamic linkage (.dll). Advantages, if lots of programs need to use this code at the same time, it saves some memory. Disadvantages - you need to learn some tricks to get up and running with this.

    Before you get hung up on libraries, I recommend you make sure you can do (1), which requires the minimum knowledge. It sounds like you are relatively new to C++ and libraries are relatively advanced things to be playing with.

    You need to make your code modular so that the required functionality can be moved into another project. You also ought to consider using object orientated design - i.e. wrap your data up into a class - to address the first part of your question. I recommend that you develop a harness module consisting of main(). The harness module includes the header file for your Init() and Calculate() code, which preferably is in a class. The Init() and Calculate() code should be in a separate module (i.e. separate .cpp file) so that you can test it with your harness and then move it to your required project(s) when it is debugged.

    Author Comment

    Thanks a lot.
    That made a lot clear.
    LVL 19

    Expert Comment

    >> Thanks a lot.

    Glad to help.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
    Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    857 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now