Generating DLL in visual studio - c++

i am trying to generate dll file, use its .lib file in another program ,but unfortunately, no .lib file is generated!
what should i do?

Try it like this:
extern "C" _declspec (dllexport) int add(int a, int b);
extern "C" _declspec (dllexport) int add(int a, int b)
{
return a + b;
}

You'll need to use __declspec (two underscores) with dllexport attribute to export a symbol from a DLL. And on client side you'll need to use __declspec(dllimport).
You better put the declaration on header like this:
// YourHeader.H
#ifdef _DLL_EXPORTING // Define this symbol in DLL project setting
#define EXPORT_IMPORT __declspec(dllexport)
#else
#define EXPORT_IMPORT __declspec(dllimport)
#endif
EXPORT_IMPORT int add(int,int);
Let the client use this header directly without worrying about declspec specifier. The EXPORT_IMPORT macro can also be used to export any other functions you want to export/import.

Related

Using __declspec( dllexport )

Hi I'm little bit confused with dllexport.When I use __declspec( dllexport ) for example in class
#define DllExport __declspec( dllexport )
class DllExport C {
int i;
virtual int func( void ) { return 1; }
};
do I export class C to dll file or do I export C class from dll file?
When compiling the DLL you have to write __declspec(dllexport) as you did. This tells the compiler you want it to be exported. When using the DLL you want __declspec(dllimport) in your included files. The compiler then knows that this functions and classes are in a DLL-file and need to be imported. Because you don't want to change your header-files that much, you should define a macro e.g. BUILD_DLL.
#ifdef BUILD_DLL
#define DLL_PORTING __declspec(dllexport)
#else
#define DLL_PORTING __declspec(dllimport)
#endif
Now you write in example.h:
class DLL_PORTING example_class { … };
In your .exe file just include the header files you need and everything will work.

How to properly create a DLL from C code and use it in a C++ project

I have a set of functions written in C that I need to be able to call from another project written in C++. The C code is essentially some functions that do some calculations on a large data set. I didn't write them - all I want to do is allow my C++ project to be able to call those functions. My solution was to create a DLL for the C code and link it to my C++ project.
In order to make the DLL, I structured myCproj.h (the header in the C project, not C++ project) like so:
#ifdef __cplusplus
extern "C" {
#endif
struct __declspec(dllexport) neededStruct {
int a;
//I need to be able to initialize this struct in my C++ project.
}
__declspec(dllexport) void neededFunc( struct neededStruct *input ) {}
//I need to be able to call this function from my C++ project and feed
//it my local instance of neededStruct.
#ifdef __cplusplus
}
#endif
The src file, myCproj.c, was not changed at all. The function definitions do not have __declspec(dllexport)in front of them, nor is extern "C" inserted anywhere. The code compiles without error and produces myCproj.dll and myCproj.lib.
I then tell my C++ project in VS where to find myCproj.lib and myCproj.h accordingly and copy the DLL over to the directory where my C++ executable lives. To use the DLL, I gave myCPPproj.cpp the following addition:
#define DLLImport __declspec(dllimport)
struct DLLImport neededStruct input;
input.a = 0;
extern "C" DLLImport void neededFunc( &input );
However, I get error EO335 'linkage specification is not allowed' on that last line. What am I doing wrong?
It is preferable to use the same header for both the library and using code.
As mentioned, it is usually done by a conditional define, like the following:
MyLibrary.h:
#if defined(MYLIBRARY_API)
#define MYLIBRARY_EXPORTS __declspec(dllexport)
#else
#define MYLIBRARY_EXPORTS __declspec(dllimport)
#endif
#if defined(__cplusplus)
extern "C" {
#endif
MYLIBRARY_API bool MyLibFunc();
#if defined(__cplusplus)
#endif
MyLibrary.c:
#include "MyLibrary.h"
void MyLibFunc()
{
....
}
App.cpp:
#include <MyLibrary.h>
int main()
{
MyLibFunc();
}
The symbol MYLIBRARY_API will be defined for the library project (usually as a /D on the compiler command line). And if you use visual studio that is pretty much exactly what you get when creating a dll project with exports.

How to export overloads of a function in a C++ dll??

I am looking for a way to export from my C++ dll a function with two overloads.
This is my overloads on the .h file:
static __declspec(dllexport) int __stdcall TotalCost(char* a, double* b);
static __declspec(dllexport) int __stdcall TotalCost(char* a, double* b, double c);
my questions are:
is it possible to export overloads of a function??
how I can set in the file .def the EXPORT ??
Thanks in advance
Fabio
Yes, you can export overloads.
The classic way is to add the following macro definition to your library headers:
#ifdef MYLIB_EXPORTS
#define MYLIB_API __declspec(dllexport)
#else
#define MYLIB_API __declspec(dllimport)
#pragma comment(lib,"MYLIB.lib")
#endif
//...
Use the above macro in your interface files:
MYLIB_API int ComputeTotal(...
class MYLIB_API C_MyClass...
You will have to define MYLIB_EXPORTS in your DLL project settings (C++/Preprocessor Definitions).

__declspec (dllexport) custom type variable

I'm working on a Visual Studio solution with multiple projects (Visual Studio 2013). One project is for generating a .dll file, other projects use that .dll file and generate .exe files. When I export a standard type variable everything works fine. But in case if I want to use my custom defined type I get an compilation error. Here is an example
// Dll.cpp
#define DllExport __declspec (dllexport)
DllExport int maxPackSize = 20;
// my custom type
struct DllExport Header
{
int m_data; // some data
};
DllExport Header qHead = { 100 };
// Exe.cpp
#define DllImport __declspec (dllimport)
DllImport extern int packetSize; // OK
struct DllImport Header;
DllImport extern Header qHead; // leads to an error
When I use qHead in my Exe.cpp I get a compilation error on that line. Error is like
error C2027: use of undefined type 'Header'
What am I doing wrong? Any ideas?
The error message you get is a compiler error (not a linker error!).
You need to define Header prior to its first use.
You could move struct DllExport Header { ... }; to a separate header file (e.g. Dll.h) and then #include "Dll.h" in Exe.cpp.
You need to export your custom type in a header, so that your .exe can see the type. For example,
Dll.h
#ifdef EXPORT_SYMBOLS
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
struct DLL_EXPORT Header
{
int m_data;
};
Exe.cpp
#include "Dll.h"
Header qHead;
You need to add EXPORT_SYMBOLS to the Dll's preprocessor flags so that the correct __declspec macro is switched.

No functions get exported into DLL despite proper dllExport - Visual Studio

I have a base class (QIndicator) and I want to implement derived classes in DLLs. The DLL project in Visual Studio 2012 for a sample derived class has the following code:
header file with base class
#ifndef _DLL_COMMON_INDICATOR_
#define _DLL_COMMON_INDICATOR_
// define the DLL storage specifier macro
#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif
class QIndicator
{
private:
int x;
int y;
};
extern "C"
{
// declare the factory function for exporting a pointer to QIndicator
DECLDIR QIndicator * __stdcall getIndicatorPtr(void);
}
#endif
source file with derived class
#define DLL_EXPORT
#include "indicator.h"
class QIndicatorDer : public QIndicator
{
public:
QIndicatorDer (void) : QIndicator(){};
~QIndicatorDer (void){};
private:
// list of QIndicatorDer parameters
int x2;
int y2;
};
extern "C"
{
DECLDIR QIndicator * __stdcall getIndicatorPtr(void)
{
return new QIndicatorDer();
};
}
The problem I have is that upon successful build, the produced DLL file does not contain the exported getIndicatorPtr function (as shown by DependencyWalker). I checked whether the dllexport keyword gets propagated properly into the declaration of getIndicatorPtr and it does.
Another interesting problem is that I already have another derived class like this, in another DLL project, that I created some months ago. This older project is basically the same and everything works well there. I checked all properties of both the old and the current projects, and they seem identical. So I ran out of ideas, why I can't get getIndicatorPtr to export.
Any help is much appreciated,
Daniel
That's because it's not being exported. Why?
__declspec specifier should only be placed in the declaration of a function, not it's definition. Also, avoid something like #define DLL_EXPORT. Preprocessor definitions should either defined in project properties (MSVC) or command line option (-D in GCC, for example).
Look at you code:
Header
extern "C"
{
DECLDIR QIndicator * __stdcall getIndicatorPtr(void);
}
When compiler parses this header, is sees DECLDIR as dllimport (because you define DLL_EXPORT in .cpp). Then in .cpp, it suddenly appears as dllexport. Which one is used? The first one.
So, leave your header (it's fine), but change your source:
//#define DLL_EXPORT -> remove this!
#include "indicator.h"
class QIndicatorDer : public QIndicator
{
//...
};
extern "C"
{
/* DECLDIR -> and this! */ QIndicator * __stdcall getIndicatorPtr(void)
{
return new QIndicatorDer();
};
}
Then, go to project properties (I assume you use Visual Studio) and then C/C++ -> Preprocessor -> Preprocessor Definitions and add there DLL_EXPORT=1.
That should work.