Saturday, January 21, 2023

DLL vs Libs in Windows Systems

DLL vs Libs 

DLL (Dynamic Link Library) and LIB (Library) are both file formats used to store compiled code that can be used by multiple programs. However, there are some differences between them:DLLs are used in Windows systems, while LIBs are used in various other systems, such as Linux, macOS, and UNIX.

DLLs are dynamically linked to the program at runtime, which means that the program can use the functions stored in the DLL without having to link to them during the compilation process. 

On the other hand, LIBs are statically linked to the program, which means that the program must be linked to the LIB during the compilation process.

DLL (mathlibrary.dll)

#include <Windows.h>

extern "C" __declspec(dllexport) int add(int a, int b)
{
    return a + b;
}

extern "C" __declspec(dllexport) int subtract(int a, int b)
{
    return a - b;
}

The main program that uses mathlibrary.dll

#include <Windows.h>
#include <iostream>

int main()
{
    // Load the DLL
    HMODULE mathLibrary = LoadLibrary("mathlibrary.dll");
    if (!mathLibrary) {
        std::cout << "Failed to load math library" << std::endl;
        return 1;
    }

    // Get the address of the add function
    typedef int (*AddFunc)(int, int);
    AddFunc add = (AddFunc)GetProcAddress(mathLibrary, "add");
    if (!add) {
        std::cout << "Failed to find add function" << std::endl;
        return 1;
    }

    // Call the add function
    int result = add(3, 4);
    std::cout << "3 + 4 = " << result << std::endl;

    // Get the address of the subtract function
    typedef int (*SubtractFunc)(int, int);
    SubtractFunc subtract = (SubtractFunc)GetProcAddress(mathLibrary, "subtract");
    if (!subtract) {
        std::cout << "Failed to find subtract function" << std::endl;
        return 1;
    }

    // Call the subtract function
    result = subtract(3, 4);
    std::cout << "3 - 4 = " << result << std::endl;

    // Unload the DLL
    FreeLibrary(mathLibrary);

    return 0;
}


Linking a DLL to a Visual Studio project involves a few steps:

  1. Add the DLL to the project: You can add the DLL to the project by copying it to the project's directory or by adding a reference to the DLL's file path in the project settings.

  2. Include the DLL's header file: In order to use the functions in the DLL, you need to include the DLL's header file in your C++ code. This file contains the function declarations that your code needs to access the functions in the DLL.

  3. Link to the DLL: In your project's properties, go to the Linker > Input settings and add the name of the DLL (without the file extension) to the Additional Dependencies field.

  4. Specify the search path: In the project's properties, go to the Linker > General settings, and add the directory where the DLL is located in the Additional Library Directories field.

  5. Call the DLL's functions: Once you have added the DLL and its header file to the project and specified the search path, you can call the DLL's functions in your code by using the extern "C" keyword along with the function name.

  6. Run-time linking: You can also load the dll at runtime and call the functions using the Windows API functions like LoadLibrary and GetProcAddress.

  7. Rebuild the project: Finally, rebuild your project to ensure that the DLL is correctly linked to it.

Please note that these steps may vary depending on the specific version of Visual Studio you are using and the specific settings of your project.



LIB (mathlibrary.lib):

#include <iostream>

extern "C" int add(int a, int b)
{
    return a + b;
}

extern "C" int subtract(int a, int b)
{
    return a - b;
}


The main program that uses mathlibrary.lib


#include <iostream>

// Import the functions from the library
extern "C" int add(int a, int b);
extern "C" int subtract(int a, int b);

int main()
{
    // Call the add function from the library
    int result = add(3, 4);
    std::cout << "3 + 4 = " << result << std::endl;

    // Call the subtract function from the library
    result = subtract(3, 4);
    std::cout << "3 - 4 = " << result << std::endl;

    return 0;
}

To use the mathlibrary.lib in your project, you will need to link the library to your project. This can be done by adding the library to the linker input in the project settings.

Also, you should specify the header file that has the function declarations in your code, using the #include preprocessor directive.

Please note that these steps may vary depending on the specific IDE or compiler you are using.


Linking a LIB to a Visual Studio project involves a few steps:

  1. Add the LIB to the project: You can add the LIB to the project by copying it to the project's directory or by adding a reference to the LIB's file path in the project settings.

  2. Include the LIB's header file: In order to use the functions in the LIB, you need to include the LIB's header file in your C++ code. This file contains the function declarations that your code needs to access the functions in the LIB.

  3. Link to the LIB: In your project's properties, go to the Linker > Input settings and add the name of the LIB (without the file extension) to the Additional Dependencies field.

  4. Specify the search path: In the project's properties, go to the Linker > General settings, and add the directory where the LIB is located in the Additional Library Directories field.

  5. Call the LIB's functions: Once you have added the LIB and its header file to the project and specified the search path, you can call the LIB's functions in your code directly.

  6. Rebuild the project: Finally, rebuild your project to ensure that the LIB is correctly linked to it.

Please note that these steps may vary depending on the specific version of Visual Studio you are using and the specific settings of your project.

Also, make sure that you are using the correct platform version of the lib file, for example, if you're building your project with x64 platform, you should use the x64 version of the lib, otherwise, you might get a "unresolved external symbol" error.



No comments:

Post a Comment

LeetCode C++ Cheat Sheet June

🎯 Core Patterns & Representative Questions 1. Arrays & Hashing Two Sum – hash map → O(n) Contains Duplicate , Product of A...