Thread: Dll not found

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    4

    Dll not found

    Hello there,

    I'm currently playing with creating and using DLLs with MinGW.

    I've written the following files:

    test.h
    Code:
    #ifndef __TEST_H__
    #define __TEST_H__
    
    int addOne(int);
    
    #endif
    test.c
    Code:
    #include "test.h"
    
    int addOne(int i)
    {
        return i + 1;
    }
    test_test.c
    Code:
    #include <stdio.h>
    #include "test.h"
    
    int main()
    {
        int i = addOne(13);
        printf("%d\n", i);
        return 0;
    }
    I compliled my DLL using the following line:

    gcc -Wall -shared test.c test.dll

    I then compile the application using this line:

    gcc test_test.c test.dll -o test_test.exe

    Running the file test_test.exe worked fine. But when I moved test.dll to a folder called lib and recompiled test_test with

    gcc test_test.c lib/test.dll -o test_test.exe

    trying to run the application results in an error mentioning that test.dll cannot be found. I'm unable to find any decent information on this so if someone could explain to me why this is and what can be done to allow test_test.exe to look in the correct place for test.dll I would be very greatful.

    Thanks
    marclurr

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    As a silly test, what about
    Code:
    gcc test_test.c lib\test.dll -o test_test.exe
    I'm assuming that the lib folder is in the current folder you're typing the command from.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    Hello thanks for the quick reply,

    Yes your assumption is correct. i.e. the directory structure looks like the following:

    [edit]root\test_test.c[/edit]
    root\test_test.exe
    root\lib\test.dll

    I tried replacing / with \ but it made no difference. I should note that it compiles fine either way.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Yeah I missed that it was compiling fine and blowing up at runtime. I'm guessing then that there's a path issue. Perhaps reading Search Path Used by Windows to Locate a DLL may help.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    I'm surprised that your DLL compiles at all. I always thought that DLLs had to contain the DLLMain Function, but I guess not. Anyway, to export your addOne function you should add __declspec(dllexport) before the function definition (so it should now look like '__declspec(dllexport) int addOne(int i)').

    Usually when you compile a DLL with MinGW it will give you a .a file along with the DLL, this is the file you should link with. If your dll is called test then the library file should be called libtest.a.

    The DLL only needs to be in the same directory (or the system directory) as the executable when run, it does not need to be used for compiling against.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    Thanks for the replies. The msdn page gad some useful information.

    Regarding DeadPlanets reply, how do you compile to get the .a and the .dll output? As with the commands outlined above I only got the .dll.

    Cheers

  7. #7
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Looking around, it looks as if you can just link straight against the DLL with MinGW. It seems like you can use a library file, but it isn't necessary. I'm really not sure anymore.

    Sorry if anything I said was misleading.

    (All DLLs I have written and seen use __declspec(dllexport) before function definitions so that should be right, but maybe it also isn't necessary.)

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I believe DeadPlanet's point was that, by default, if you ask for a library gcc will give you a file with a .a extension (since that's the "standard" on *nix systems). Since you specified the .dll extension, gcc was more than happy to call it that instead.

    Also, since you "linked" with the library itself, that is probably why you didn't need the export manifest.

    If you had wanted something that also needed to work with Visual Studio, I'm not sure how much of this you could still get away with (since that's a different linker).

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    Ahh thanks for the info. I don't usually develop on Windows, but since I'm home over Easter that's all there is. It's useful stuff to know so thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HMODULE from current DLL
    By DaveHope in forum Windows Programming
    Replies: 20
    Last Post: 07-22-2009, 02:20 AM
  2. creating a simple DLL
    By Dark_Phoenix in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2009, 09:41 PM
  3. strange error -- COM DLL is not installed correctly?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 07-16-2007, 08:32 AM
  4. Error C2664 - Trying to call an external Dll
    By jamez05 in forum C++ Programming
    Replies: 3
    Last Post: 08-08-2006, 06:07 AM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM