How to use Clang to build DLL (Dynamic Link Library) and using it in C++ under Windows?

Поделиться
HTML-код
  • Опубликовано: 23 ноя 2024

Комментарии • 8

  • @SassyToll
    @SassyToll Год назад +2

    Thank you, that was great

  • @aamg9586
    @aamg9586 Год назад

    Thank you very much for the tutorial. Very informative and straight to the point!.
    If I may ask you how did you figure how to complete the compilation and everything? I mean where did you look that up?
    For example I have literally spent more than 2 days but nothing at all. However, I will say that I did somehow manage to output a .dll file but never managed to link it using LLVM/Clang. I also managed to output dll file using MSVC cl.exe + link it but again I couldn't with LLVM/Clang.
    I've got 2 more questions hahaha. Don't mind me XD. but what were the warnings that you got when you ran the bat file to compile? Anything worth mentioning?
    The last question is, what was the purpose of __stdcall before __declspec(..)? I haven't seen that part when you did the same build using gcc that's why I'm asking. Is it necessary only for Clang to be able to define the dll import/export macros or has to do with something else?

    • @TommyNgo-SDEV
      @TommyNgo-SDEV  Год назад +1

      The answer is that I have been programming since age 16, and more than 25 years of professional programming experience. I am not a beginner SE. Clang is everywhere in the macOS and Linux world, and I ran across this compiler a few times in my life. But like many people, I often used the GCC toolchain instead. It is just a preference of what you want to do. Like any of the tutorials, I have tried reusing source code, and this one is from a previous tutorial (C++ DLL for VB6) ruclips.net/video/DDzsX94kQ84/видео.html. The __stdcall just ensures that the functions are called correctly between modules (especially between C++ and VB6). One other comment is that I did have Visual Studio 2022 installed before this tutorial. Some of the warnings were mostly about the use of strcpy(), but I am using std::strcpy() which should be a little bit safer than the old primitive C version. Anyhow, I just want to give you an idea of how it should work, and you take this example for your own personal project. You should always define the import/export macro to properly expose the functions in the DLL for other programs to see. You can review my previous tutorials, and I have shown the usage of dumpbin to see if the DLL has been properly created.

    • @aamg9586
      @aamg9586 Год назад

      25 years! That's crazy, you must had it at this point hahah. I can't envy you enough! I always wanted to become a programmer from my childhood days but my life circumstances were different back then and now I'm 31 and my life circumstances changed in a way that allowed me to finally get going and start learn how to become one! I truly do albeit me hitting the wall many times wanting to give up and sometimes my thoughts get to my head and start saying maybe it's late for me to catch up and then who will even think to hire me anyway with no computer degree, lack of experience, maybe already old? and this kinda stuff. Thankfully though I'm still pushing or at least I'm trying and most of such times when I get into a problem or an error and stuff I keep search for a solution and for the majority I do find one eventually. But this one by far was waaaay there and it was litterally trying to killing me literally hahaha and I was this close to just stop do the whole thing. Anyways, thank you again for you putting the video and true effort to it and teaching me how it can be done with Clang! will always be thankful to you my friend :).
      My sub to you is already paying off, you see? ;).
      PS: I didn't know that Clang is also popular in other platforms like Linux and osx!, I thought gcc what makes the majority of it! bsshhh mind blowing.
      I have tried GCC before but the reason why I stuck with Clang toolchain I guess is because first their integration-ready with VS and probably because since I'm 100% windows user, they use the default windows runtime libraries out of the box thus more compatibility in general with windows than others, or at least that what I've understood. @@TommyNgo-SDEV

    • @TommyNgo-SDEV
      @TommyNgo-SDEV  Год назад +1

      @@aamg9586 Yes. You have a mindset of SE/programmer. Keep it up and it will lead you to some good place. It's never too late to become a programmer or developer. I am always busy with my job with different projects, etc. and so this is a sort of a way to give back to the community. When I get a chance, I will produce a video in the hope that it will help someone. Thanks for subscribing.

    • @aamg9586
      @aamg9586 Год назад

      Keep it up too my friend. Thanks for the nice words, I will try by best :_)@@TommyNgo-SDEV

  • @freziyt223
    @freziyt223 Месяц назад

    what do i do if my dll isn't in the same folder as my program and i don't want to add it to PATH?

    • @TommyNgo-SDEV
      @TommyNgo-SDEV  Месяц назад

      Under Windows, you are required to use LoadLibrary("\path\your_dll.dll"), and then use GetProcAddress() to find the address of the function that you want to use in the library. Here is an example code where the DLL is under a different directory:
      File test3.cpp:
      -----------------------
      #include
      #include
      // Define a function pointer type for the SaySomething function
      typedef void (*SaySomethingFunc)(const char*);
      int main() {
      // Load the DLL
      HMODULE hModule = LoadLibrary("D:\\GitRepo\\cpp_tutorials\\shared_library2\\test\\shared_lib.dll");
      if (!hModule) {
      std::cerr