I can`t export an array from a DLL. Here is my code:
" DLL header "
#ifdef EXPORT
#define MYLIB __declspec(dllexport)
#else
#define MYLIB
#endif
extern "C" {
/* Allows to use file both with Visual studio and with Qt*/
#ifdef __cplusplus
MYLIB double MySum(double num1, double num2);
extern MYLIB char ImplicitDLLName[];
#else
extern Q_DECL_IMPORT char ImplicitDLLName[];
Q_DECL_IMPORT double MySum(double num1, double num2);
#endif
}
" DLL source"
#define EXPORT
#include "MySUMoperator.h"
double MySum(double num1, double num2)
{
return num1 + num2;
}
char ImplicitDLLName[] = "MySUMOperator";
" client main.cpp"
int main(int argc, char** argv)
{
printf("%s", ImplicitDLLName);
}
When building I am getting from the linker this error:
Error 2 error LNK2001: unresolved external symbol _ImplicitDLLName \Client\main.obj
// My purpose of exporting the array is to study export of different data structs from DLLs
How to cope with the error raised by linker and what rules are violated?
*UPDATE: *
IDE Visual Studio 2010.
Client - is written on C++, also DLL is on C++
Assuming you're linking your import library correctly (and thats a big assumption), you're not declaring MYLIB correctly for importing symbols:
This:
#ifdef EXPORT
#define MYLIB __declspec(dllexport)
#else
#define MYLIB
#endif
Should be this:
#ifdef EXPORT
#define MYLIB __declspec(dllexport)
#else
#define MYLIB __declspec(dllimport)
#endif
Keep in mind we've little context to work with. It looks like you're trying to consume this from a C-compiled application, but without more info I can't be sure. If that is the case, then Q_DECL_IMPORT had better do the above or it still will not work. I'd start with a basic "C" linkage export and work your way up from there.
Sample EXPORTS.DLL
Exports.h
#ifdef EXPORTS_EXPORTS
#define EXPORTS_API __declspec(dllexport)
#else
#define EXPORTS_API __declspec(dllimport)
#endif
extern "C" EXPORTS_API char szExported[];
Exports.cpp
#include "stdafx.h"
#include "Exports.h"
// This is an example of an exported variable
EXPORTS_API char szExported[] = "Exported from our DLL";
Sample EXPORTSCLIENT.EXE
ExportsClient.cpp
#include "stdafx.h"
#include <iostream>
#include "Exports.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << szExported << endl;
return 0;
}
Output
Exported from our DLL
Related
Basically I have a dll project in visual studio. I'm linking this dll project to a second project successfully but as soon as I try to use extern variables things go wrong. I've seperated my extern variables into a single header and source file from everything else so I can isolate the problem and potential solutions. I've scowered the internet for hours now and I feel like I've tried everything. I am beginning to think it might be a compile flag? Anyway, heres my code.
macros.h
#pragma once
// dll management
#ifdef TOAST_EXPORT
#ifdef _MSC_VER
#define TAPI __declspec(dllexport)
#else
#define TAPI __attribute__((visibility("default")))
#endif
#else
#ifdef _MSC_VER
#define TAPI __declspec(dllimport)
#else
#define TAPI
#endif
#endif
globals.h
#pragma once
#define TOAST_EXPORT
#include "macros.h"
namespace toast
{
TAPI extern const char c;
}
globals.c
Note that I tried this without const and made no assignment here
#pragma once
#include "globals.h"
namespace toast
{
const char c = 'a';
}
main.c (from the project that compiles to an exe)
#include <globals.h>
int main()
{
char c = toast::c;
return 0;
}
So far I've tried making a lot of subtle changes like reordering extern and const and such. I've also done it both with just extern and just const. Still produces the same unresolved external symbol "char const toast::c" error. Keep in mind that I can create instances of classes and call their methods from the dll successfully and thats with things like class TAPI logger... and such.
I am trying out a simple test DLL project. Under my solution, I have two projects - first C++ Dll (library) and second C++ exe (driver). Below I've attached a snapshot of the basic project setup:
dllmain.h
#ifndef DLLMAIN_H_
#define DLLMAIN_H_
#ifdef FFMPEGLIB_EXPORTS
#define FFMPEGLIB_API __declspec(dllexport)
#else
#define FFMPEGLIB_API __declspec(dllimport)
#endif // FFMPEGLIB_EXPORTS
static void TestFoo();
extern "C" FFMPEGLIB_API void Test(int* num);
extern "C" FFMPEGLIB_API void ProxyFoo();
#endif
dllmain.cpp
#include "dllmain.h"
#include "A.h"
void TestFoo()
{
A a;
a.foo();
}
void Test(int* num)
{
*num = *num + 1;
}
void ProxyFoo()
{
TestFoo();
}
driver.cpp
#include <iostream>
#include "dllmain.h"
int main(int argc, char** argv)
{
int mum = 4;
Test(&num);
std::cout << num;
ProxyFoo();
return 0;
}
The library project compiles normally, but the exe fails to compile with a linker error:
Code Description Project File
LNK2001 unresolved extern symbol _imp_ProxyFoo driver driver.obj
LNK2001 unresolved extern symbol _imp_Test driver driver.obj
LNK1120 2 unresolved externals driver driver.exe
I have two questions here:
Why does the function name of dllmain.h get mangled in spite of being marked as extern "C"?
Why can I not create an instance of test class A from extern methods? What would be a good way of doing that?
Why the function name of dllmain.h getting mangled in spite being
marked as extern "C"?
Because __declspec(dllimport).
Why can I not create instance of test class A from extern methods?
What would be good way of doing it?
I think that's fine, but you didn't provide any class A code. Just do this:
class __declspec(dllexport) A
{
/* ... */
};
Why EXE compile failed?
This is because you have not imported the LIB file of the DLL into the project.
There are two ways to import it:
Add #program comment(lib, "<YOUR_LIB_FILE>.lib") to the code file.
Add <YOUR_LIB_FILE>.lib to Properties -> Linker -> Input -> Additional Dependencies.
Microsoft documentation: https://learn.microsoft.com/en-us/cpp/build/importing-and-exporting
You need to put the extern "C" thing around your function definitions that you intend to export in dllmain.cpp so it matches the linkage of your declaration.
Also, you need to do the declexport thing too.
extern "C"
{
__declspec(dllexport) void Test(int* num)
{
*num = *num + 1;
}
__declspec(dllexport) void ProxyFoo()
{
TestFoo();
}
}
I'm learning how to build a DLL and call it from another project(I also want the DLL can be called not only by C/C++ but also by Python). Here is my code for building the DLL:
callbacktesetDLL.h:
#ifdef CALLBACKTESTDLL_EXPORTS
#define CALLBACKTESTDLL_API __declspec(dllexport)
#else
#define CALLBACKTESTDLL_API __declspec(dllimport)
#endif
typedef int(CALLBACK *p)(char*);
CALLBACKTESTDLL_API int __stdcall StrToInt(char*);
CALLBACKTESTDLL_API char* __stdcall NumCompare(p FuncP, char*, int b);
callbacktestDLL.cpp:
#include "stdafx.h"
#include <stdio.h>
#include "callbacktestDLL.h"
#include <stdlib.h>
CALLBACKTESTDLL_API int __stdcall StrToInt(char* StrInput)
{
int IntResult;
IntResult = atoi(StrInput);
return IntResult;
}
CALLBACKTESTDLL_API char* __stdcall NumCompare(p FuncP, char* StrInput, int b)
{
int a = FuncP(StrInput);
if (a>b)
{
return "a is bigger than b";
}
else
{
return "b is bigger than a";
}
}
And a Source.def file:
LIBRARY
EXPORTS
StrToInt #1
NumCompare #2
With the code above, I got callbacktestDLL.dll and callbacktestDLL.lib. With depends, the functions' names in the DLL can be shown:
Now I want to call the functions in the DLL from another project:
CallDLL.h:
#pragma comment(lib,"callbacktestDLL.lib")
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
typedef int(*p)(char*);
extern "C" _declspec(dllimport) int StrToInt(char* InpuString);
extern "C" _declspec(dllimport) char* NumCompare(p FuncP, char*, int b);
CallDLL.cpp:
#include "stdafx.h"
int main()
{
p FuncP_R = StrToInt;
NumCompare(FuncP_R, "1234", 40);
return 0;
}
However, when I run the project, it told me:error LNK2019: unresolved external symbol __imp__StrToInt and error LNK2019: unresolved external symbol __imp__NumCompare. I've already copy the .lib and .dll files under the CallDLL project's root folder. Why this happens? How can I solve it? Thank you for your attention.
I finally made it. Here is the details:
The files that generate the DLL:
callbacktestDLL.h:
typedef int(CALLBACK *p)(char*);
extern "C" __declspec(dllexport) int __stdcall StrToInt(char* InputString);
extern "C" __declspec(dllexport) char* __stdcall NumCompare(p FuncP, char* InputString, int b);
callbacktestDLL.cpp:
#include "stdafx.h"
#include <stdio.h>
#include "callbacktestDLL.h"
#include <stdlib.h>
extern "C" CALLBACKTESTDLL_API int __stdcall StrToInt(char* InputString)
{
int IntResult;
IntResult = atoi(InputString);
return IntResult;
}
extern "C" __declspec(dllexport) char* __stdcall NumCompare(p FuncP, char* InputString, int b)
{
int a = FuncP(InputString);
if (a>b)
{
return "a is bigger than b\n";
}
else
{
return "b is bigger than a\n";
}
}
Comparing to the former files in the post, I removed the .def file and added extern "C" ahead of each function declaration and definition. Then I generate a new .dll and .lib files and copy them to the CallDLL projects' root folder. I used depends to see the functions' names in the DLL:
I also changed the CallDLL's file like this:
CallDLL.h:
#pragma comment(lib,"callbacktestDLL")
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
typedef int(__stdcall *p)(char*); //same as typedef int(CALLBACK *p)(char*) in callbacktestDLL.h
extern "C" _declspec(dllimport) int __stdcall StrToInt(char* InputString); //exactly same as what's in callbacktestDLL.h apart from dllimport
extern "C" _declspec(dllimport) char* __stdcall NumCompare(p FuncP, char* InputString, int b); //exactly same as what's in callbacktestDLL.h apart from dllimport
CallDLL.cpp:
#include "stdafx.h"
int main()
{
p FuncP_R;
char* a = "1234";
FuncP_R = StrToInt;
printf(NumCompare(FuncP_R, a, 42));
return 0;
}
It worked as expected. I think one of the mistakes I made is missing __stdcall when importing. Maybe there are also mistakes about the name mangling. I'll keep testing it.
You should always include the same header file to define a consistent interface. Creating different header files is error prone. The next change to the interface might make the interface incompatible.
There is a slight difference between the module that implements the functions to the module that uses the functions. The difference is the __declspec(dllimport) or __declspec(dllexport). That's why the header files contains the
# ifdef CALLBACKTESTDLL_EXPORTS
Your module that implements the functions also export them. Therefore you must defined the symbols in the project settings. If you compile at the command line you have to add the /D CALLBACKTESTDLL_EXPORT to the compiler arguments.
BTW: If you have defined CALLBACKTESTDLL_EXPORT your macro CALLBACKTESTDLL_API contains the __declspec(dllexport). This causes the linker to create the DLL export table. The .DEF is not necessary. You should remove it because..... it defines the interface again (at a different level).
I'm trying to create a C++ dll. I have followed the msdn tutorial but I can't compile my dll correctly.
The problem is that any function is exported. I have tested it with dumpbin.exe tool and nm tool.
In both cases, there is no detected symbols.
Here is the code of this library:
Header file:
#ifndef NLIB_H
#define NLIB_H
#ifdef _WINDLL
#define NLIB_EXPORTS __declspec( dllexport )
#else
#define NLIB_EXPORTS __declspec( dllimport )
#endif
#ifdef __cplusplus
extern "C"{
#endif
NLIB_EXPORTS int fun1(int n);
#ifdef __cplusplus
}
#endif
#endif
Source code file:
#include "nlib.h"
int fun1(int n) {
return 100;
}
I have found the error. It's necessary to add NLIB_EXPORTS to the *.c file also, like this:
#include "nlib.h"
NLIB_EXPORTS int fun1(int n) {
return 100;
}
I created a DLL project called Test Lib:
// main.h
#ifndef __MAIN_H__
#define __MAIN_H__
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
extern "C" {
DLL_EXPORT void print();
}
#endif
// main.cpp
#include "main.h"
#include <iostream>
#define BUILD_DLL
using std::cout;
using std::endl;
extern "C" {
DLL_EXPORT void print() {
cout << "Success" << endl;
return;
}
}
Above code is from following an example I found online that I could understand. When I try to compile and/or build it, I get the following error & warning:
error: function 'void print()' definition is marked dllimport
In function 'void print()':
warning: 'void print()' redeclared without dllimport attribute: previous dllimport ignored
This is the second library I'm ever creating because I'm trying to replicate a problem in the first one, when this happened. What is wrong? I'm using Code::Blocks.
You need to define BUILD_DLL before you include the header file main.h.
#define BUILD_DLL
#include "main.h"
As it stands in your program, you declare print with __declspec(dllimport) because the header file is processed when BUILD_DLL is not defined.