1

Assume I create a DLL in Netbeans according to this youtube tutorial, I am able to proceed and call the DLL from another C++-source file. Here is the step-by-step procedure, though the DLL works fine using Netbeans!

I create a new empty Project (in my case Visual C++ Win32 Console Application). I add a C++ source file to the source folder. Here is my code

#include "..\SampleLib\SampleLib.h"
#include
int main() {
SampleClass *ptr = new SampleClass();
ptr->TestFunc();
return 0; }
I then type in Include directories, library directoy and at linker input finally under additional dependencies the .dll file

However, I cannot call it from a C++-source file from Microsoft Visual Studio 2010 (currently using Express version).

I include the directory and include the header file. Then I link the source file to the DLL at "additional dependencies".

My error message is:

1>C:\Users\misefe\Documents\NetBeansProjects\SampleLib\dist\Debug\MinGW-Windows\libSampleLib.dll : fatal error LNK1107: Ungültige oder beschädigte Datei: Lesen bei 0x2E0 nicht möglich.

I fear the problem is that no *.lib file is created. I was following another tutorial of how to implement a DLL in MSVS2010. The only difference is instead of adding the *.lib file at additional dependencies is my *.dll file. Obviously, that is a bad try, but best I could think of.

Does anybody know how to:

  1. either create a *.lib file in Netbeans
  2. or connect my Netbeans DLL with MSVS2010 properly or solve my problem, respectively?
  • Can you please update your question and summarize the steps of the tutorial you reference. – nils Jul 17 '15 at 09:45
  • Sure (my MSVS is in German, so I translate the words, they might not match 100%): I create a new empty Project (in my case Visual C++ Win32 Console Application). I add a C++ source file to the source folder. Here is my code > #include "..\SampleLib\SampleLib.h" #include int main() { SampleClass *ptr = new SampleClass(); ptr->TestFunc(); return 0; } I then type in Include directories, library directoy and at linker input finally under additional dependencies the .dll file – PublicUserName Jul 17 '15 at 09:55
  • A DLL is a dynamic library and a LIB is a static library. They should be created differently and used differently, so you need to find matching tutorials. – citywall Jul 17 '15 at 09:55
  • I meant you to please *update* your question with the above information (there is an edit-button below that tags), and not paste it as a another comment. – nils Jul 17 '15 at 10:03

2 Answers2

0

Read Netbeans C/C++ Projects Quick Start Tutorial - pay special attention to the Wizard in step Creating C and C++ Projects - you get to choose among other things between a static or dynamic library project.

Select static to create a LIB, dynamic to create a DLL.

citywall
  • 235
  • 2
  • 11
0

If you only use a DLL, you could use the LoadLibrary functions. It takes the path of your DLL and load it. Then you use the GetProcAdress method to get a pointer to the method in your DLL. But it has a massive drawbacks. Actually due to C++ name mangling the methods name in the DLL and the methods name in your code will be different. If you really want to only use a DLL you can flatten your class using extern "c".

If you can use a static library you can go like that To create a static library with netbeans
-Create a New C++ project
-Select C/C++ static library

you can load the library in visual studio with the following steps :

  1. Add #include statements in your code

  2. Add an include directory in ->
    Configuration Properties/VC++ Directories/Include Directories (click and edit, add a new entry)

  3. Add a library directory for *.lib files ->
    Configuration Properties/VC++ Directories/Library Directories (click and edit, add a new entry)

  4. Add the lib you want to link -> Configuration Properties/Linker/Input/Additional Dependencies (example library.lib;

  5. Place DLL files in the directory of your final executable

Community
  • 1
  • 1
Pumkko
  • 1,523
  • 15
  • 19
  • I see that I have had some confusion regarding DLL and LIB. Anyway, setting my project to static library and using the same code I dont get a .lib file when compiling in Netbeans.
    On the other hand when I try to access the DLL via a pointer using the other method with GetProcAdress MSVS tells me the DLL is damaged. But technically I can open it from another c++ source file in Netbeans.
    Is it possible that there is a compatibility issue between Netbeans and MSVS files? I experienced some restrictions when I wanted to create a .def file in Netbeans, too.
    – PublicUserName Jul 17 '15 at 10:47
  • A LIB is a static library, it will be combined with your code by the linker. A DLL is dynamically loaded library, it will be loaded by your OS at runtime. The big difference is here, LIB works at link time (after compile time), DLL works at runtime (when you launch your exe) – Pumkko Jul 17 '15 at 10:53
  • Still I can not understand, if you turned your project into a static library what did you obtain after the build ? only a DLL ? a DLL and a LIB ? – Pumkko Jul 17 '15 at 10:54
  • No, to clarify. I tried both methods. First, I reprogrammed my MSVS source file using hinstance and GetProcAdress. But it keeps on telling me following: 1>C:\Users\misefe\Documents\NetBeansProjects\SampleLib\dist\Debug\MinGW-Windows\libSampleLib.dll : fatal error LNK1107: Ungültige oder beschädigte Datei: Lesen bei 0x2E0 nicht möglich. (not illegal or damaged file) Then I set my project to static library and compiled again. There was no error. But no .lib file showed up. So nothing to add for in my MSVS program. As to answer your question, neither DLL nor LIB was creating using static library – PublicUserName Jul 17 '15 at 11:10