It would appear that the example couldn't get any simpler:
//Example.h
#pragma once
#ifdef EXPORTING_DLL
extern __declspec(dllexport) void HelloWorld();
#else
extern __declspec(dllimport) void HelloWorld();
#endif
//Example.cpp
#include "Functions.h"
#define EXPORTING_DLL
void HelloWorld()
{
}
So, what I have problems with? OK, when trying to compile as it is, that is after creation of dll type project in VS, I'm getting warning:
warning C4273: 'HelloWorld': inconsistent dll linkage
if I change definition of HelloWorld in cpp file to:
__declspec(dllimport) void HelloWorld()
{
}
I'm getting error:
Error C2491 'HelloWorld': definition of dllimport function not allowed
If on the other hand I change definition of HelloWorld to:
__declspec(dllexport) void HelloWorld()
{
}
I'm getting warning:
Warning C4273 'HelloWorld': inconsistent dll linkage
Any idea how to define it so it compiles without any warnings? I mean, those warnings are at least worrying.
You need to define EXPORTING_DLL before you include the header. That way the header can declare the correct prototype based on whether you are importing or exporting.
Without doing this it will import which is not what you want
Related
I have a project that is compiled into a library and declares a certain function to be implemented by the user of the library:
//To be defined by user
Application* CreateApplication();
When compiling the code into a shared library on Linux this works perfectly. Any user of the library can define an implementation for the declared function and it can be used inside the library. If the user of the library forgets to define an implementation, they will get an error pointing this out.
I'm now in the process of porting the library to Windows, where it is supposed to be compiled into a dll. However, I'm running into problems as the linker used by Visual Studio is complaining:
unresolved external symbol Application* __cdecl CreateApplication(void)
I tried adding the extern keyword to indicate that the definition of the function is somewhere else, but this didn't work.
Why can't I declare (but not define) a function in a dll like this? How should I fix my code so it works both on Linux and on Windows?
What you are attempting to do only works in a static library, it cannot work in a dynamic library like a DLL. For that, you will have to change the code to use a function pointer instead. The application that is using the DLL can pass in the address of the desired function from its own code, and the DLL can then assign that address to a variable that it uses as needed, eg:
HEADER:
#ifndef MYLIB_H
#ifndef MYLIB_H
#ifdef COMPILING_MY_LIB
#define MY_EXPORT __declspec(dllexport)
#else
#define MY_EXPORT __declspec(dllimport)
#endif
// declare Application as needed...
typedef Application (*lpCreateApplicationFunc)();
#ifdef __cplusplus
extern "C" {
#endif
MY_EXPORT void SetCreateApplicationFunc(lpCreateApplicationFunc func);
#ifdef __cplusplus
}
#endif
#endif
DLL:
#define COMPILING_MY_LIB
#include "MyLib.h"
//To be defined by user
lpCreateApplicationFunc CreateApplication = NULL;
void SetCreateApplicationFunc(lpCreateApplicationFunc func)
{
CreateApplication = func;
}
void doSomething()
{
Application *app = NULL;
if (CreateApplication)
app = (*CreateApplication)();
if (app)
{
...
}
}
EXE:
#include "MyLib.h"
Application MyCreateApplicationFunc()
{
...
}
// during startup, call this...
SetCreateApplicationFunc(&MyCreateApplicationFunc);
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.
I use MinGw (i686-4.9.2-win32) and trying to export the class from dll (which is made in visual studio)
But I get an error undefined reference to _imp___ZN11ConsoleTestC1E'
BUT function foo() from the same library exported without error..
Here my code
testdll.h
#ifdef CONSOLETEST_EXPORT
#define CONSOLETEST_API __declspec(dllexport)
#else
#define CONSOLETEST_API __declspec(dllimport)
#endif
extern "C" class CONSOLETEST_API ConsoleTest{
public:
ConsoleTest();
};
extern "C" void CONSOLETEST_API foo();
testdll.cpp
#include "testdll.h"
ConsoleTest::ConsoleTest(){}
void foo(){}
And in main.cpp:
#include "testdll.h"
int main()
{
foo();
ConsoleTest* cc = new ConsoleTest();
}
PS: I tried to create a .a library from .dll :
pexports testdll.dll | sed "s/^_//" > testdll.def
dlltool -U -d testdll.def -l testdll.a
...but it did not help.
Thanks in advance for any advice!
extern "C" class won't work.
You can dll-export a class, but not with C naming convention.
ISO C++03 7.5[dcl.link]/4:
A C language linkage is ignored for the names of class members and the member function
type of class member functions.
If you remove the extern "C", I see no reason it wouldn't work.
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.
I was following a tutorial on how to create a C++/Cli DLL, for some reason I get a warning for each function declaration, here's the whole code:
// KRecognizer.h
#pragma once
namespace KR
{
class __declspec(dllimport) KinectRecognizer
{
public:
KinectRecognizer();
~KinectRecognizer();
int Display();
};
}
_
// KRecognizer.cpp
#include "stdafx.h"
#include "KRecognizer.h"
using namespace System;
KR::KinectRecognizer::KinectRecognizer()
{
}
KR::KinectRecognizer::~KinectRecognizer()
{
}
int
KR::KinectRecognizer::Display()
{
Console::WriteLine(L"Writing a line");
return 100;
}
Here are the error outputs:
http://pastie.org/3678144
I'm compiling with the /clr flag.
The header declares DLL import, which means the definition of the class comes from a DLL. Since you are providing the definition, this gives the linkage error. You'll want to use __declspec(dllexport) instead when defining the DLL.
Since you'll want to use the same header file in the app that will use the DLL, the following idiom is often used:
#ifdef MYAPI_EXPORTS
# define MYAPI __declspec(dllexport)
#else
# define MYAPI __declspec(dllimport)
#endif
And then use:
class MYAPI KinectRecognizer
#define MYAPI_EXPORTS before including the header in the DLL, but do not define it in the application using the header to import the DLL.