What to compile as a library? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I've lost my head just a little trying to reason the most effective means of compiling a c++ project. I stick to more managed languages like Java so the CMake file is a bit obtuse to me.
My main problem is what do I compile as a library and what do I just compile together? I have a main function in my program with various over classes in different files with headers. What is the most normal way of handling these files together? Should I compile the main function separate from the classes then link them or should they be a shared library even though it is a bit small for a library?
Mainly I am looking just for general guidelines of what should be compiled together, what should simply be linked, and someone to more clearly explain the norms/best practices of how this all works.
I understand that the compiler needs to convert the Header and Source files to object files and then combines them together as a binary. I am just confused at what should go into the binary.

If you need the code for only one executable you can just link all object files together. Libraries are useful if you need the same functions/object files in different executables.
Of course the bigger a project gets you could also use sub projects which output libraries and then link the main project files and the sub project libraries together.

Related

What are the different ways of including libraries in a C++ project? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Allow me to preface this question by letting you know that I have no formal CS education and have been self-learning C++.
I would like to understand what the different ways of including third party libraries in a project are.
How to identify how a project is to be included if there is an interesting library on github?
I've read about concepts of dynamic and static linking in a windows context however I am still somewhat not clear about them.
Libraries contain implementations of functions, regardless of being static or dynamic. A library contains m function implementations, of which a subset n < m is exposed to the user. In C++, for these n functions the library normally offers a header file provided to you for inclusion, a list of so-called function prototypes. Depending on what header you include, and what function you have used in your project, the linker memorizes the prototypes of such external functions and demands you to link the appropriate library against your executable to satisfy the unresolved prototype symbol.
Function implementations of static libraries are welded at compile time into your executable. Function implementations of dynamic libraries remain exactly in their location, there's just a stub welded into your executable that will transfer control over to the dynamic library upon a call to it.
Briefly, you just need to do following steps to include/referece a C++ library:
Add the heade file (*.h or *.hpp) directory to your project's include paths.
#include the xxx.h in your source code files, to call the functions/methods/interfaces.
Add libaray:
For static lib (*.lib on Windows or *.a on Linux), add the lib to your project's library paths.
For dynamic lib: just make sure the *.dll (or *.so) are in the same directory of your project output (like *.exe or your lib), for running or to publish/deploy.

What files have to shipped with the actual program? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I know these are the include files(in c++) We have to compile them and then have to ship them with the actual binary. But I have a bit strange problem.I used windows.h in a program and I want to ship it but windows.h have other include files and so on.So I would have to ship whole windows sdk in the form of dll's .Is there any other way to do it?
You do not need to ship header files with a binary application.
You do however need to ship any shared libraries (DLL's on Windows) that your program depends on - and this includes the compilers runtime (the standard library etc) - static libraries are made part of the executable and thus do not need to be shipped separately.
If you are using Visual Studio then you need to ship the Visual Studio redistributables along with your program (google the version for your Visual Studio version) - for other compilers there are similar requirements.

Install OpenCV on a C++ compiler [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am used with using OpenCV with python. But does someone have an idea how to add openCV library to a C++ compiler (such DevCpp or CodeBlocks...).
If there is a compiler on which it's easier to install OpenCV library no problem, I have no restriction conserning the compiler.
I followed some tutos on the net but they were not so clear.
Thanks.
C++ has two important phases of compilation. First, each individual .cpp file is needed. You need the library header files (.h) for this. Secondly, the separate parts are linked together, and you need the library files themselves. (.lib/.a depending on platform).
So, you need to provide paths to both. The compiler knows which exact headers are needed from the #include statement, but the libraries to link must be explicitly listed.

Best practice when building large libraries for project [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am currently working on a Raspberry Pi project that uses the OpenCV library, among others. The OpenCV library is quite large, and the build process for it is decently extensive. I found a script which downloads and installs the latest version of OpenCV, and following some suggestions from this question, I was able to build the library, and begin using the functions within OpenCV.
Considering the actual build process for OpenCV took considerably longer than building our project would, is it acceptable to just build the library once, as opposed to building the library each time we build our project?
While I realize this is probably personal preference, I am wondering how others handle situations similar to this.
As you probably already know, code that does not change does not need to be recompiled. This is true for executables and libraries alike.
A library is supposed to provide you addictional functionality in a neat, pre-packaged form. The difference between additional code you add to your project and a library is that the code included in a library is supposed to be in a stable state, so that once built the user will be able to use its features without any maintenance hassle; the APIs will be available and they will always work. You will be able to discard any implementation files, and just work with the header files - which provide you with the API within your code - and the library files, which contain the compiled implementation.
You are pretty much pre-compiling part of your program; a part that will also be able to be used in other projects, again without recompiling.
Note that C++ itself is an example of this: an implementation of the C++ standard library (for example libc++) is already included with your compiler, so that you are able to use the standard C++ headers without the need to recompile C++ whole every time you try a "Hello World!" program.
You can even extract libraries out of parts of your project that you feel are already completed and stable: this can allow you to reduce the time required to compile your project even though it becomes bigger. These reasons are part of why modularity is so strongly encouraged when programming.
TL; DR: Recompiling a library only once is not only acceptable, is most probably what you want to do.
It is normal to compile once and then only link the library. For that reason the compilers can detect whether there are changes in source files.

Why do we need *.lib files? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I don't seem to understand the need of *.lib files. Let's say I have 3 *.c files:
module1.c
module2.c
module3.c
myheaders.c
In each .c file we have 2 functions. Now if I call function x in module3.c from module1.c, module1.c knows about function x by looking in myheaders.c.
So if I compile this in VS I get a shiny little portable *.exe with no *.lib file attached, so why and when does one need *.lib files?
*.lib files are for other programmers who want to use your functions in their own programs.
*.exe files are for end users who just want to run your program.
Libraries can be made from groups of related functions that have been tested. They allow you to reuse code without having to go through the compilation stage each time.
Dynamic or Shared libraries allow you to upgrade parts of your executable without recompiling the executable or altering it.
In larger projects, validation of a program is a large portion of the schedule. Libraries that have already been tested will shorten the schedule and make everybody happy.
Let's say I wrote a compiler. I implement some IO functions (in English - cout) to work on windows. If you write code, and want to compile it with my compiler, you don't want to wait on each compilation for my implementation of cout to compile. More than that, you don't care about the HOW, so you don't need the source code (witch will take up some memory). So, instead of recompiling code, you get a *.lib file with the functionality, compiled once by me, no heavy source code to take along.
When you sell someone a program you wrote to use with other programs, you (usually) don't want to give him the source code - just the functionality.
If two languages share the same standard's for functions (or if the linker knows about different standards) I could write some code in one language, compile it to a *.lib, then use it with the different language. That doesn't look useful until you download some third party code written in a language you are not using.
The header files doesn't contain the function, they only contain there prototypes so the compiler know how to call them. In the case of predefined functions, they are precompiler seperatly as .lib file which refers to (library) so to call them you will need that .lib file and the header so the compiler know how to call them. when you compile using VS or any compiler, there is another process called the linking which links these files as a single executble.
Because library can be use in a lot of programs.
Your header file is necessary only for your prof
Good Question.
*.exe stands for executable. This executable could be used to run the program from End user or developer.
*.lib could be used when someone wants to use functions from your code.
There are two types of linking libraries. Static linking and Dynamic linking. follow this question to understand the difference between both of them.
Static linking vs dynamic linking