Solved

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

Posted on 2004-10-22
203 Views
Last Modified: 2010-04-01
Hi, I have some little general questions.
1)
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).
2)
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.

Thanks.
0
Question by:DEEGBAL
    6 Comments
     
    LVL 17

    Expert Comment

    by:rstaveley
    You are probably looking at writing a class.

    e.g.

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

    Assisted Solution

    by:mrwad99
    >> 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 http://www.flipcode.com/tutorials/tut_dll01.shtml for more info.  You will get the LIB when you compile the DLL.
    0
     

    Author Comment

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

    Accepted Solution

    by:
    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.
    0
     

    Author Comment

    by:DEEGBAL
    Thanks a lot.
    That made a lot clear.
    0
     
    LVL 19

    Expert Comment

    by:mrwad99
    >> Thanks a lot.

    Glad to help.
    0

    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