0

I have made a c++ dll with visual studio, I succeeded to use it with visual studio but i can't find how to use it with just vscode or more generally with just command line. My goal is to put my library on github with the documentation explaining how to use it but i am not able to compile a program in command line.
I tried to use this command :
g++ src/SummerFlow_Client.cpp -o bin/prog -I include -L lib -lmingw32 -lSummerFlow

with all the files with the code in the folder "include", the .lib file in the folder "lib" and my .dll file in the folder "bin".
This returned this error :

C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible lib/SummerFlow.lib when searching for -lSummerFlow
C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible lib/SummerFlow.lib when searching for -lSummerFlow
C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible lib\SummerFlow.lib when searching for -lSummerFlow
C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lSummerFlow
collect2.exe: error: ld returned 1 exit status
SumCry
  • 3
  • 4
  • Do you plan to put the binary on github, or the source? – Devolus Mar 17 '21 at 14:27
  • Please format your posting properly, as it is hard to read. – Devolus Mar 17 '21 at 14:29
  • Since you are using MingW you also have dependencies to it, so you would have to put the MingW libs also into your search path. If those are also DLLs you would have to bundle them together as well, as they will be needed by your DLL as well unless you link everything statically. – Devolus Mar 17 '21 at 14:31
  • If you are talking about mixing msvc and mingw dlls that are c++ based you may have incompatibilities that prevent you from using dlls created with the other toolchain. The standard library will have a completely different implementation – drescherjm Mar 17 '21 at 14:35
  • I plan to put the binary and the source on github. I don't really understood what you said, I mean that i just want to put on github the file that people need to have to use it. I just don't understand why i can use it with visual studio and not without, when i saw video on how to use dll in c++ i saw that people also include the .a file in the command line but visual studio doesn't create .a file when you compile your dll. – SumCry Mar 17 '21 at 14:39
  • In fact i just need to know how people will used the dll to gives him the right file. I don't really understand if i can use a dll, created and compiled with visual studio, on all the way you want. – SumCry Mar 17 '21 at 14:41
  • Normally people on github just provide the source code and let the user compile a library using whatever compiler they want. Using a program like CMake will help to make this easier for the users because they can use it to generate their own project file or Makefile for whatever toolchain they use. – drescherjm Mar 17 '21 at 14:42
  • Ok, if I understood, I just put on github all the code file of visual studio and i just provide a cmake file wich compile the dll, and after that the user could use it. If I'm right, did you know an article that explain how to compile dll without visual studio, because I search that but I not found it. – SumCry Mar 17 '21 at 14:47
  • Read about cmake first. One of the things it can do is create a VS project for people, **if** that's what they need. – sweenish Mar 17 '21 at 15:11
  • 1
    See https://stackoverflow.com/a/5347413/103167 for why you cannot share Standard Library objects (like `std::string`) across the DLL boundary if your compilers are different. – Ben Voigt Mar 17 '21 at 16:31

1 Answers1

1

The short answer is, you can't.

You can't use a C++ DLL generated by compiler A from compiler B and vice-versa, because they generally don't use the same ABI (application binary interface). To keep explanations short, several of these elements probably differ (this isn't an exhaustive list):

  • The organisation of objects in memory, i.e. where to find which field given the this pointer
  • The organisation of methods and RTTI, i.e. where to find virtual methods, information about class hierarchy, etc.
  • The name of non-virtual methods, static methods and global functions exported in the DLL, as known as name mangling

Each compiler has its own strategy to manage these things on its own, all with a lot of compromises dealt along the years.

There exists common ABI standards for C, so very often, a DLL compiled with Visual Studio can be used with MinGW as well, as soon as the architecture is the same (of course you can't mix 32 and 64 bit, or Intel and arm).

Sadly, this isn't true for C++. IN C++, you must recompile the DLL if you change the compiler. Note that ABI also evolve , so a DLL compiled with an older version of the same compiler might not work either with the new version.

QuentinC
  • 12,311
  • 4
  • 24
  • 37
  • Some (many?) architectures have ABI standards for C++ as well. But there are a host of problems deeper than ABI, see https://stackoverflow.com/a/5347413/103167 and https://stackoverflow.com/a/3726874/103167 – Ben Voigt Mar 17 '21 at 16:31