Ordinary Library vs. Static Library and more questions

Sorry in advance if this is a bit long :|
I learned and created some simple libraries to use with my code and to make it more readable and brief, however, I had a problem with where I had to put each file(".h"s and ".cpp"s) so that it would be more proper and maybe standard, so I searched for this using Google, and I came across the weirdly enough things like the concept of static libraries in here (https://msdn.microsoft.com/en-us/library/ms235627(v=vs.80).aspx) and then even didn't find a decent article that would explain the difference between my ordinary library and the one explained in there(the static one).

So now, I have a number of major questions that make my head spin all the time and I'd really appreciate anyone who takes the time and answers them all:
1) Where do I put my ".h"s and ".cpp"s if I want to comply with a standard or a common convention?
2) what's with the static library? Why would I use it instead of what I've learnt already?(I didn't understand the technical explanations in MS article)
3) Why no instruction manuals try to teach such practical things and always talk about the bare C++? I know that there are a huge number of compilers and IDEs out there and covering their facilities and capabilities is something impossible, but why can't I even find a single well-structured one(by that I mean: proceeding from basic things toward difficult things) in simple layman's terms? I wonder whether people learn a lot of these concepts through in-person classes and professional relationships RATHER THAN books and online articles?
4) Was I wrong to choose VS to begin C++ with? I kind of feel like there are a lot of options and properties and configurations, blah blah blah! I don't even know what is a "startup project"(again mentioned in that MS article)! Could a plain compiler on a Linux OS be probably better for me?

Thank you a lot and sorry for being a silly grouch :)
Edit:
BTW just in case that you're curious to know, I'm currently reading the Programming:P&P using C++ by Sir Stroustrup!
Last edited on
Last edited on
I don't think so... The format of the ordinary library I talked about is .h while the dynamic one is .dll
The format of the ordinary library I talked about is .h while the dynamic one is .dll

That's another misunderstanding. A .h file is source code which you include in your program when it is compiled.

The library (of whichever type) is the corresponding binary file (corresponding to the .h) which will be linked into your program.

A static library is linked at the time when your program is built. First it is compiled, then linked. It is on windows a .lib file.

A dynamic library is not linked at build time. Instead when the program is run, the corresponding binary code is found in the dynamic library (the .dll - Dynamic-link library)
Any explanations couldn't be better than this for me! I appreciate it Chervil.
Why no instruction manuals try to teach such practical things and always talk about the bare C++?


This might go some way towards helping you: https://www.daniweb.com/programming/software-development/tutorials/466177/understanding-c-from-source-to-binaries
When I read your original post, I assumed by "ordinary" library you simply meant a bunch of files you reused from one project to another. So, if you have a time travel module, you might have TimeWarp.h, TimeWarp.cpp, TimeMachine.h, TimeMachine.cpp, etc. When you start your second project, you simply copy these files to a new project and start compiling. So that leads to your question of where is a standardized location to put the code so it can be reached by multiple projects.

I guess that this could loosely be considered a library. However, it is not a static or dynamic library as described by @Chervil. If this is what you want, then just put the code wherever you want to and aim your project at it. I don't think there are really any standard locations for code like this.

A library in the traditional sense is a pre-compiled mass of executable code with associated header (.h) files. The user includes the header files when compiling his/her project and links in the library to create an executable program. How the library is linked in depends on whether it is static or dynamic, and you can read about that in @Chervil's above links.

The library developer compiles the library separately for each system on which it needs to be deployed. Then the developer bundles the library and associated header files into a distribution package and distributes the bundles. A fairly standard organization for this would be to have a directory named for the library, or possibly the company distributing the library. Underneath you would have the following subdirectories: hdr (containing the header files--could also be name "incl"), lib (containing the compiled libraries), and doc (containing documentation about how to use the library).

There are other ways to organize things. Nothing is as universally accepted. Start doing something that is fairly intuitive and consistent, and when you find something that you don't like, make an adjustment.
@doug4
A library in the traditional sense is a pre-compiled mass of executable code with associated header (.h) files.

Can you suggest a step-by-step clear explanation or book about it? It might be 100% correct but just doesn't make the situation better for me, b/c I hardly understand what it is in action.
Thanks anyways.

@Repeater Thanks. I'll take a look now.
Last edited on
b/c I hardly understand what it is in action.


Consider this familiar code:

1
2
3
4
5
6
#include <iostream>

int main()
{
     std::cout << "Hello World!" << std::endl;
}


How does the computer know how to write "Hello World!" to the console? How does the computer know what to do when std::endl is encountered?

C++ provides the standard header <iostream> to declare the standard ostream cout, ostream::operator<<, and the function std::endl. Your code includes the header, and then your program is compiled. You end up with an object file (something like main.o) that knows there are std::cout, operator<< and std::endl, but knows nothing about how they operate. When the program is linked, the necessary standard library files get added to the executable to provide the capability to write characters to the console. That's what it is in action (for static libraries).

Dynamic libraries are similar, but the code does not get linked into the final product. There is a common block of code that all programs that use the library can access. You should try to understand static libraries first before learning about dynamic libraries.

If you are asking how to build a library, it depends on what system you are on and what IDE you are using. In Linux without an IDE, first you compile all of the object files, and then you archive them into a library using the 'ar' command. Using the Code::Blocks IDE, there is an option somewhere under build settings to allow you to create a library rather than an executable. Other environments I don't know about. I would try googling "create static library using <your environment>" and see where it leads. It should give you the step-by-steps you need.
@Doug4
Thank you. It's clear now.
Topic archived. No new replies allowed.