Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey,
We have project of 80 .cpp files which so big and hard to conduct while building and maintaining so we want to break the code into small small DLL modules. Can any body help me on DLL implementation?
Posted
Updated 19-Jan-11 21:27pm
v2
Comments
[no name] 20-Jan-11 4:58am    
80 cpp files is not so many. There are projects, that include 3000 cpp and h files.
[no name] 22-Feb-11 0:11am    
my files still going increase no limits on this

Welcome to CP.

We generally don't do your work for you, especially when your description is so vague, but I will have a shot.

If you want it to be a modular type system, then you have your work cut out for you. You will have to have some sort of external interface to your application that each module can interact with to achieve its result.
For a IDE for example (Visual Studio), there may be a function called in each DLL asking it if it knows how to format this language, and if it does, then ask it to format the plain text to return the formatted text. This ends up been quite a lot of work.

If you just want the DLLs as a way of breaking your code up into more meaningful projects then it is rather simple (and you could even use static libraries to the same end).
Basically, create a new MFC DLL project and move the source and header files you want in it to the new project.
You will need some method of exporting all the functions (either a .def file or a __declspec(dllexport)).
Then you just #include the class header file as usual (although the .h is in a different folder now) and link against the .lib file generated for the project.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Jan-11 2:25am    
All right, a rat race... my 5.
Please see my few cents on design method.
[no name] 22-Jan-11 4:57am    
thanks sir,
but i have used __declspec(dllexport) for modulation.
but if I create one function of DLL in that fuction if call other fuctiion then it gives linking error


like following Example:
my project has one class called checkvarprop.cpp this class used to check variable name and initial value entered by user is correct or not whether name contain special character or not and intial value character or not

Collapse | Copy Code
void Ccheckvarprop:ValidateName(){ if(IsNumeric()) ///function for checking numeric values { AfxMessageBox("name contains Numbers"); return; } if(IsSpecialCharacters()) { AfxMessageBox("name contains Numbers"); return; }}run of this DLL will gives linking error for IsNumeric() is invalide call
please help on DLL implementation


or is there other method for modulation.
to saparate my project into small small modules
Sergey Alexandrovich Kryukov 25-Jan-11 12:24pm    
I did not realize at first the question was not about design (in my answer I tried to guide the approach), but about basic technique.
I would recommend using the answer by C Pallini -- good place to start.
--SA
santoshmaruti wrote:
Can any body help me on DLL implementation?


The documentation helps you all the time: "Dynamic-Link Libraries"[^] at MSDN.
:)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Jan-11 12:22pm    
I did not realize at first the question was not about design, but about basic technique.
You answered - my 5.
Then either you havnt exported the function IsNumeric() with __declspec(dllexport), or you aren't linking against the library that it is defined in.

For static linking to DLLs (which is what you are doing) the linker needs to be able to get the (compiled version) of the code to include it in the final .exe.

You can instruct the linker to look for code in libraries with either #pragma comment(lib, "mydll.lib") or adding it to the Additional Dependancies in the Linker options for the project properties.

There will be a .lib file generated alongside your .dll file in the output directory.
You will need to do this for every DLL that you create that is to be loaded statically.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 20-Jan-11 11:07am    
Correct (a 5) but OP may or may not understand how to link, looks like no experience.
santoshmaruti says:
But if I create one function of DLL in that fuction if call other fuctiion then it gives linking error

like following Example:
my project has one class called checkvarprop.cpp this class used to check variable name and initial value entered by user is correct or not whether name contain special character or not and intial value character or not
void Ccheckvarprop::ValidateName() {
	if (IsNumeric()) { ///function for checking numeric values
		AfxMessageBox("name contains Numbers");
		return;
	}
	if (IsSpecialCharacters()) {
		AfxMessageBox("name contains Numbers");
		return;
	}
}

run of this DLL will gives linking error for IsNumeric() is invalide call
please help on DLL implementation

or is there other method for modulation.
to saparate my project into small small modules.


Answer:
The code for the function IsNumeric() is defined in another 1 of your modules or the main executable file.
If it is defined in a different DLL or LIB, simply add the corresponding LIB file to the additional dependancies for the project.

If it is defined in the executable then it needs to be moved to a DLL or static LIB.
If this is the case that this function is used from multiple modules, then it should be placed in a module rather than the exe as part of a good program design rather than linker errors.

Since you made it to the linker errors then you should already have the function prototyped in 1 of the header files, but make sure you do.
Part of the good program design would have the header file with the line __declspec(dllimport) bool IsNumeric(); as part of the module (DLL or LIB) that contains the code for the function, and not have that prototype coded in each of the modules that use it.

If you don't understand this, then try understanding this example:

Module: Common.dll
Files: Common.h; Common.cpp

Code:
Common.h
#pragma once

//Get rid of the #ifdef __cplusplus sections if you don't want the names unmangled
#if defined(DLL_EXPORTS_COMMON) //This is only set when compiling Common.dll in the preprocessor options for the project
	#ifdef __cplusplus
		#define PORT extern "C" __declspec(dllexport)
	#else
		#define PORT __declspec(dllexport)
	#endif
#else
	#ifdef __cplusplus
		#define PORT extern "C" __declspec(dllimport)
	#else
		#define PORT __declspec(dllimport)
	#endif
#endif

PORT bool IsNumeric(char ch);


Common.cpp
#include "Common.h"

PORT bool IsNumeric(char ch) {
	return ch >= '0' && ch <= '9';
}


Module: CheckVars.dll
Files: Ccheckvarprop.h; Ccheckvarprop.cpp

Checkvarprop.h
#pragma once

#if defined(DLL_EXPORTS_CHECKVARS) //This is only set when compiling CheckVars.dll in the preprocessor options for the project
	#define PORT __declspec(dllexport)
#else
	#define PORT __declspec(dllimport)
#endif

PORT bool IsNumeric(char ch);


Checkvarprop.cpp
#include "Ccheckvarprop.h"
#include <common.h>
//You need to add the directory of Common.h to your "Additional Include Directories" in the C/C++ Project Properties
//You also need to add Common.lib to your "Additional Dependancies" in the Linker Project Properties

PORT void Ccheckvarprop::ValidateName() {
	if (IsNumeric()) { ///function for checking numeric values
		AfxMessageBox("name contains Numbers");
		return;
	}
	if (IsSpecialCharacters()) {
		AfxMessageBox("name contains Numbers");
		return;
	}
}</common.h>


If you keep your headers specific to a single module, then whenever you need to add the directory for a module to the "Additional Include Directories", simply add the lib file to the "Additional Dependancies" for the linker too. This is a simple way of keeping track and keeping things neat and easy to understand
 
Share this answer
 
This is right idea.

Draw your dependency diagram and try to arrange it into layers.
The definition of a layer: the definitions from one layer may not use any definition from any of the layers above; any layer can use any definition from the lower layers.

When you obtain layers, arrange each one (or more) in a separate project of DLL type, add the C++ *.cpp/*.h files belonging to the layer. Put all the project in a single solution (how it was called in VC v.6 -- a set of projects accessed at the same time?). Go to the menu Project => Project Dependencies and setup dependency relationships for each project, looking at your layered diagram (VS will not figure this automatically for you).

I hope you know the import/export declarations used to link the libraries. See HowTo: Export C++ classes from a DLL[^].
 
Share this answer
 

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