0

I have created a little test program to understand the __declspec(dllimport) and __declspec(dllexport) preproc.

In my solution I have :

1) File: produit.h

#pragma once

int  facteur = 5;
int __declspec(dllexport) base = 100;
int __declspec(dllexport) produit(int, int);

2) File : produit.cpp

#include "produit.h"

int produit(int a, int b)
{
   return facteur * (a*b);
}

3) File: produitEx.h

#pragma once

__declspec(dllimport) int base;
__declspec(dllimport) int produit(int a, int b);

4) File : main.cpp

#include <iostream>
#include "produitEx.h"
using namespace std;

int main()
{
    int a =5, b=2, c;
    c = base + produit(a, b);
    std::cout << c << std::endl;
    return 0;
}

looks pretty clear and nothing special just exporting fucntions and importing them.

When Compiling (with MinGW) I got those 2 errors :

..../main.cpp:8 undefined reference to `_imp___Z7produitii'

..../main.cpp:8 undefined reference to `_imp__base'

So I did not know what's confusing for the linker ? How to resolve it?

Thank you. (I use MinGW and NetBeans Cpp)

Just to add: the Same code Works on Visual Studio.

iehrlich
  • 3,572
  • 4
  • 34
  • 43
Blood-HaZaRd
  • 2,049
  • 2
  • 20
  • 43
  • 1
    Do you link with the *export library*? When creating the DLL there should also be an *export library* created (a `.lib` file) that you need to link with. – Some programmer dude Jul 01 '17 at 12:30
  • I did not do any link with an export library. in fact I don't see any exported library. Excuse but I don't figure out why should I do such link. Does dllexport means that a DLL will be created !? – Blood-HaZaRd Jul 01 '17 at 12:35
  • `dllexport` does not mean that dll will be created. You need to build dll yourself, and then link it yourself into your executable. – user7860670 Jul 01 '17 at 12:48
  • where is body function from profuitEx.h? You don't use produit.h – 21koizyd Jul 01 '17 at 12:51
  • @VTT but I did not mean to build a dll . all the files are in the same project. Unless you tell me that I can't export than import the same functions and data in the same project. – Blood-HaZaRd Jul 01 '17 at 12:52
  • @21koizyd : in the produitEx.h I use the imported functions so the body is already known in produit.cpp file – Blood-HaZaRd Jul 01 '17 at 12:53
  • you need to build dll in spearate project then import it into original – Sly_TheKing Jul 01 '17 at 12:54
  • @Sly_TheKing : so as an answer to my question : if I could export functions then import them on the same project, we will say NO we could'nt do that !! – Blood-HaZaRd Jul 01 '17 at 12:56
  • 1
    If you want to export and then import the same function and data in the same project then you don't need to use function marked with `dllimport` in your application. `dllimport` basically tells that "these functions aren't here". So just include `produit.h`. – user7860670 Jul 01 '17 at 12:57
  • @VTTYeah i am totally ok with you in the second part (including headers) but I am reading a book with the given files and Had the errors and as the dellexport and dllimport are new for me I said I may missed sthing. So I ll take ur comment as a correct answer. But one another, why the same code works on Visual Studio ? – Blood-HaZaRd Jul 01 '17 at 13:00
  • what's the point of building dll for a few functions that you would use only in that project, dlls are meant to be ported between projects – Sly_TheKing Jul 01 '17 at 13:00
  • @VTT The code above was just a test code to understand the dllexport and dllimport. I thought that it makes me able to export things that i can import bypassing the generation of dll/lib themselves (coz I just have to understand better how this works) as I know now that those mean that I have to build a sperate dll ... I could tell you that this code means nothing. But Visual is intelligent and build me a lib :D – Blood-HaZaRd Jul 01 '17 at 13:04
  • Visual Studio has a concept of `import libs`. If you export something then it will produce an `import lib`. Also `__declspec(dllimport)` in VS is a Microsoft-specific extension and it may not always behave the same as gcc extension with the same name. Typically when using gcc you should use `__attribute__ (visibility)` instead. – user7860670 Jul 01 '17 at 13:07
  • So you are not creating a DLL from `produit.cpp`? Then what you're looking for is *not* `dllimport` and `dllexport`. The `dll` prefix of those should be an indicator that those are for when you're creating a DLL. Instead in `produit.h` you should use the `extern` keyword to *declare* the symbols, then *define* then in the source file. – Some programmer dude Jul 01 '17 at 13:47
  • @Someprogrammerdude It is completely fine to use `dllexport` (though somewhat unusual) to export symbols from executable. For example nodejs exports tons of symbols and native plugins need to link to nodejs executable. – user7860670 Jul 01 '17 at 16:13
  • @VTT Yes `dllexport` is okay, when exporting symbols from an executable or DLL. But it's not the same as `extern` which the OP seems to be wanting. – Some programmer dude Jul 01 '17 at 19:42
  • @MikeKinghan: Thanks for the link, it contains plenty of code and explantion. Good hint. – Blood-HaZaRd Jul 01 '17 at 21:50

0 Answers0