Recently I've updated to the latest version of Dev C++ but since then I've had trouble creating DLLs and linking to these DLLs.
Well to show you my exact problem I've conducted this little experiment:
1st I create a project that I'll compile into a DLL. This project includes two files (report.h|cpp) Report.h looks something like this:
#ifndef REPORT_H_
#define REPORT_H_
#include <iostream>
using namespace std;
void Report();
#endif
And report.cpp like this:
#include "report.h"
void Report()
{ cout << "Yay it worked!\n";
}
So basically I've just created a function called Report() that prints "Yay it worked!" to the screen when called.
Anyways, I compile that as a Win32 DLL, and it works fine no errors or anything.
I then move the .DLL to my dev/lib folder and the .h to the dev/include folder so that this new DLL can be used by new projects.
So I create a new win32 console project to test the DLL. First thing I do is open up the projects options via project>project options and Iink to the DLL by adding "-lDLLNAME" to the linker options. Then to test my DLL I write in this code:
#include <report.h>
#include <iostream>
#include <string>
using namespace std;
int main()
{ cout << "Start\n";
Report();
string s;
cin >> s;
}
But when I compile it I get this error:
[Linker error] undefined reference to `Report(void)'
It acts like the DLL isn't linking! Now I know what your thinking, I didn't create a DLLMain for my lil' DLL, right? Well thats true. But oddly enough this little experiment works fine on an earlier version of Dev! In 4.9.3.0 it works fine, in 4.9.5.0 it doesn't! Does that make any sense?! (Both versions use the same version of the compiler and linker right?)
Anyways if you have any idea whats going on let me know, its imperative that I get my DLL working ASAP.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You should export the function in the dll:
--declspec(dllexport) void Report();
or if you want it more general, change your report.h to:
#ifdef BUILD_DLL
// the dll exports
#define EXPORT __declspec(dllexport)
#else
// the exe imports
#define EXPORT __declspec(dllimport)
#endif
// function to be imported/exported
EXPORT void Report();
then if you build your dll use compiler option -DBUILD_DLL so the funcion will be exported. If you compile you application you don't use -DBUILD_DLL so that the function becomes the import. (This trick can be found on the mingw website).
stephan
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Recently I've updated to the latest version of Dev C++ but since then I've had trouble creating DLLs and linking to these DLLs.
Well to show you my exact problem I've conducted this little experiment:
1st I create a project that I'll compile into a DLL. This project includes two files (report.h|cpp) Report.h looks something like this:
#ifndef REPORT_H_
#define REPORT_H_
#include <iostream>
using namespace std;
void Report();
#endif
And report.cpp like this:
#include "report.h"
void Report()
{ cout << "Yay it worked!\n";
}
So basically I've just created a function called Report() that prints "Yay it worked!" to the screen when called.
Anyways, I compile that as a Win32 DLL, and it works fine no errors or anything.
I then move the .DLL to my dev/lib folder and the .h to the dev/include folder so that this new DLL can be used by new projects.
So I create a new win32 console project to test the DLL. First thing I do is open up the projects options via project>project options and Iink to the DLL by adding "-lDLLNAME" to the linker options. Then to test my DLL I write in this code:
#include <report.h>
#include <iostream>
#include <string>
using namespace std;
int main()
{ cout << "Start\n";
Report();
string s;
cin >> s;
}
But when I compile it I get this error:
[Linker error] undefined reference to `Report(void)'
It acts like the DLL isn't linking! Now I know what your thinking, I didn't create a DLLMain for my lil' DLL, right? Well thats true. But oddly enough this little experiment works fine on an earlier version of Dev! In 4.9.3.0 it works fine, in 4.9.5.0 it doesn't! Does that make any sense?! (Both versions use the same version of the compiler and linker right?)
Anyways if you have any idea whats going on let me know, its imperative that I get my DLL working ASAP.
You should export the function in the dll:
--declspec(dllexport) void Report();
or if you want it more general, change your report.h to:
#ifdef BUILD_DLL
// the dll exports
#define EXPORT __declspec(dllexport)
#else
// the exe imports
#define EXPORT __declspec(dllimport)
#endif
// function to be imported/exported
EXPORT void Report();
then if you build your dll use compiler option -DBUILD_DLL so the funcion will be exported. If you compile you application you don't use -DBUILD_DLL so that the function becomes the import. (This trick can be found on the mingw website).
stephan