Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote several functions (no classes) which I may need in different projects.

So you can imagine I want to put these function declaration/implementations in one file
and be able to use them from other projects directly.

How can I do this? Shall I embed all these things in one header file?

Any example how to do it appreciated.
Posted

If these are just a small bunch of functions that all belong to the same problem area, you may put all function declarations in one header file and the corresponding implementations in one cpp file.
To use your functions in a project you will have to include these two source files into the project.

Another technique is to generate an object library from your functions. In the Windows environment, these are called .olb. The linker will then pick functions on demand from such a library and include them in the program. (Advantage that only the used functions are included in the program!) In that case you need to include the header file and .olb file in the using project. Note that in this case you do not have to disclose the source code of your function implementation to the users of the library.

A third technique is to generate a dynamic link library (.dll under Windows). Such a library will be loaded at run-time when you application loads. The linker is just in so far involved that it puts some clues in your .exe file, which DLLs to load and which functions are to come from which DLL. In that case you must distribute the .h file, the .lib file and the .dll file to the user of your library. The latter two are produced when you build the DLL. This approach is the most labor intensive one, put it offers the chance of exchanging the DLL without touching the .exe file -- useful if you want to deliver updates or bug-fixes for your library.
 
Share this answer
 
Comments
_coder 23-Sep-13 4:28am    
Hi nv 3, but this also means if I update my myfuncs.hpp and myfuncs.cpp files it means I must copy these new versions to all the projects that were using it? (your suggestion #1).
nv3 24-Sep-13 1:07am    
Not only that, you also have to re-compile the projects. That's the downside of simply sharing source files. But even if you implement a DLL you will have to re-distribute it and move it into all installation directories.
Why not create a DLL project and include them in there? You can then "release" just the binary DLL file and use them compiled and ready to go from that.
 
Share this answer
 
Comments
_coder 19-Sep-13 9:46am    
why DLL OriginalGriff? First I don't know how to do it, and it is not reasonable for me to spend time on learning that now. I just want solution which works as I mentioned above. Are header files bad for it?
OriginalGriff 19-Sep-13 11:04am    
Well...yes. They are. The problem is that as the number of useful functions you write grows, the size of your header file expands as well - and everyone can see (and probably modify) the source to your function, so you can't guarantee that the the function they call is identical to the one you except it to be. A DLL doesn't allow that, as it only releases the interfaces (the function declarations and parameters) so it acts as a "black box" container where the user (i.e. another programer) doesn't need to know how it works - just that it does. You can expand the collection just by issuing a new DLL and any programs using it don't need to be recompiled to update to any fixes etc.

Putting it in the header means that it is included in every application as well - a DLL is shared - which wastes disk space, and memory, and is generally a poor idea.

In addition, it also helps to keep things modular - which is good for reliability and maintainability.
Menon Santosh 19-Sep-13 13:39pm    
my +5 , i think for short period fredrik's solution is good but for long and short run your solution is best
Put the function declaration in some header file, say myfuncs.hpp, then put the function definitions in a cpp file, say myfuncs.cpp.

Make sure myfuncs.cpp includes myfuncs.hpp and then for every other cpp file just include myfuncs.hpp.

You can either copy these two into whatever project you're working on, or you can build myfuncs.cpp into a lib and just include myfuncs.hpp in your other code and make sure to link against that lib.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
_coder 23-Sep-13 4:27am    
Hi Fredrik. But this also means if I update my myfuncs.hpp and myfuncs.cpp files it means I must copy these new versions to all the projects that were using it?
Fredrik Bornander 23-Sep-13 6:24am    
Yes. So putting them in a library (like a DLL) is likely to be more convenient.
But it depends on the circumstances, sometimes including the sources in every project makes sense.
_coder 23-Sep-13 7:22am    
ok, for me I think still is more reasonable to use header/cpp files. cause I guess it would take me some time to learn creating dlls
_coder 24-Sep-13 7:37am    
Hi, I am getting some errors. Do I need to precede function definitions with something in the .cpp file? Or copy their titles as they are in the header files?
Fredrik Bornander 24-Sep-13 8:45am    
Post a new question instead of commenting, it means more people will have visibility of it and you're likely to get help quicker.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900