Thread: Library Linking problem

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    7

    Library Linking problem

    I'm having problems with statically linking to a DLL with the mingw compiler in DEV C++. I have the .a(generated when compiling the DLL) file in directory my .exe is in and the .h file from the DLL in the \Include directory and the .exe does not import the DLL and so I cannot access the functions as required. I know it does not import it cos it's not included on the import table when I open the .exe with Quick View.

    Any help would be much appreciated!!

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    when you linked the dll, did you create a def file? Use quickview to list the export table (or better do this with dependancy walker if you have it)

    when you linked the exe did you actually add the dll's library to the project?

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    7
    Fordy,
    The DLL is fine as far as I can tell-there is an export table with all my functions. I have used this DLL already with HP-VEE and it worked fine.

    As for the library goes I placed it in the project folder(if thats what your asking me) and the header file in the \include folder. To link all I have to do then is:

    #include <cdll.h> /*My header file*/
    {
    .......Rest of program
    }

    Am I correct in that?
    The header file included the same one used to compile the DLL?
    What do I have to do with the .def file?

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    The library created with the dll is the import library...it tells the linker what the program expects to import......I'm suprised the project links without this library being added to the exe...

    The def file is created with the dll project like so

    Code:
    LIBRARY MyDll 
    EXPORTS MyExportedFunc
    and it tells the linker which functions to add to the dll's export table. I dont know how Devc++ uses def files......I do all my dlls with the Microsoft Linker in VC++ or MASM32 (VC++ has a better method than defs....__declspec(dllexport))......with that linker, if you used a def file you would add def file with the params you send when linking

    Ok...if you are sure the dll is fine.....try this

    Say the function you are exporting is declared like

    int MyExportedFunc(int);....

    Adapt this pseudocode and put it in your code

    Code:
    typedef int (MYEXPORTEDFUNC*)(int);
    MYEXPORTEDFUNC lpfMyExportedFunc;
    
    HMODULE hMod = LoadLibrary("MyDll.dll");
    if(!hMod){
    Errorhandler();
    return;
    }
    lpfMyExportedFun = 
        (MYEXPORTEDFUNC)GetProcAddress(hMod,"MyExportedFunc");
    if(!lpfMyExportedFun){
    Errorhandler();
    return;
    }
     /*Use func*/
    int nRes = lpfMyExportedFunc(50);
    
    FreeLibrary(hMod);
    lpfMyExportedFunc = NULL;
    Here you are loading the dll at runtime and finding where the function you need is...then you are assigning it to a function pointer and then calling it.

    I havent tested the above, but all those functions are on MSDN

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Odd Problem with Linking
    By jamez05 in forum C++ Programming
    Replies: 6
    Last Post: 09-21-2005, 01:49 PM
  2. problem in using a library (libpci.a)
    By jeanyves in forum C Programming
    Replies: 2
    Last Post: 06-17-2004, 08:36 AM
  3. help solve linking problem, thanks
    By Raison in forum Windows Programming
    Replies: 8
    Last Post: 05-29-2004, 11:14 AM
  4. Long file linking problem
    By hypertension in forum C Programming
    Replies: 3
    Last Post: 10-15-2002, 09:55 PM
  5. Linking problem...
    By BrianK in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2002, 04:13 PM