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()) { 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
#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>
PORT void Ccheckvarprop::ValidateName() {
if (IsNumeric()) { 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