Thread: Need a refresher, the difference between static and dynamic.

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    Need a refresher, the difference between static and dynamic.

    I haven't been using as much C++ these days as I would like to and am getting back into installing some libraries. I was just wondering, when creating projects using the libraries, there are the static libraries that are the standard .a files and the .a filles that are postfix with dll and come complimentary with dlls. If I remember the dll enabled libraries allow the compilation of the source code and allow the program to execute functions during runtime, while the .a files compile all the functions entirely. Am I correct?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    .a/.lib are static libraries. When you link them against your program, the linker pulls the code from the library and adds it to your executable.

    .so/.dll are dynamic libraries. When you link them against your program, the linker marks the dependency in the header and creates indirect calls in the code. The program loader loads the required dynamic libraries and resolves the indirect calls.

    Dynamic libraries can also be loaded at runtime with the appropriate functions (dlopen()/LoadLibrary()). This is not possible with static libraries.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    So why i I build a .dll library it generates the .a which I have to include in the project options.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The .a contains stubs that jump into the .dll. It's the way Windows DLLs work. They're weird.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    And those stubs are compiled within the executables?

    If I wanted to create the functionality seen in something like C# where you can invoke the methods and classes from just a dll file without the .a, is it possible?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by indigo0086 View Post
    And those stubs are compiled within the executables?

    If I wanted to create the functionality seen in something like C# where you can invoke the methods and classes from just a dll file without the .a, is it possible?
    It is certainly possible to load a library at runtime. I'm a bit blurry on the details of how you achieve that, because you essentially need to have a connection to get the vtable from the DLL. I'm sure someone else can explain, or perhaps you can find the answer on the web somewhere.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Example:

    DLL:

    Code:
    #include <stdio.h>
    #include <Windows.h>
    
    void sampleputs(const char *msg)
    {
    	if(msg)
    	{
    		puts(msg);
    	}
    	else puts("<NULL Pointer>");
    }
    
    BOOL WINAPI DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID lpReserved)
    {
    	return TRUE;
    }
    Test program:

    Code:
    #include <stdio.h>
    #include <Windows.h>
    
    typedef void (*FN_OUTPUT)(const char *);
    
    int main(void)
    {
    	FN_OUTPUT fnOutput = NULL;
    	HMODULE hModule = LoadLibrary("sampledll.dll");
    	
    	if(hModule)
    	{
    		if((fnOutput = (FN_OUTPUT)GetProcAddress(hModule,"sampleputs")))
    		{
    			fnOutput("Success!");
    			fnOutput(NULL);
    			fnOutput = NULL;
    			FreeLibrary(hModule);
    		}
    		else fputs("Invalid proc.", stderr);
    	}
    	else fputs("Invalid dll.", stderr);
    	return 0;
    }
    Output:

    Code:
    Success!
    <NULL Pointer>
    Compilation for MinGW:

    Code:
    gcc -Wall -ansi -pedantic -O3 -shared sampledll.c -osampledll.dll
    
    gcc -Wall -ansi -pedantic -O3 sampledyn.c
    Edit: Apologies for C code, but this was the quickest example I had.
    Last edited by MacGyver; 06-27-2008 at 02:33 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 01-12-2006, 11:04 AM
  2. static and dynamic
    By chrismiceli in forum C Programming
    Replies: 1
    Last Post: 08-18-2003, 09:19 PM
  3. dynamic or static binding
    By noob2c in forum C++ Programming
    Replies: 1
    Last Post: 08-06-2003, 01:43 PM
  4. static memory and dynamic memory
    By nextus in forum C++ Programming
    Replies: 1
    Last Post: 03-01-2003, 08:46 PM
  5. Static Binding & Dynamic Binding :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2001, 08:51 PM