Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7167473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:35:03+00:00 2026-05-28T14:35:03+00:00

Here’s my library Lib.c file: #include <stdio.h> int helloworld(){ printf(Hello World DLL); } Here’s

  • 0

Here’s my library Lib.c file:

#include <stdio.h>

int helloworld(){
    printf("Hello World DLL");
}

Here’s my exe Main.c file:

int helloworld();


int main(int argc, char** argv){
    helloworld();
}

I would like to create Lib.dll, and Main.exe, where Lib.dll comes from Lib.c and Main.exe links against Lib.dll.

What are the specific steps to achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:35:04+00:00Added an answer on May 28, 2026 at 2:35 pm

    See this related question on how to build the DLL.

    Your library code as it stands does not export any symbols and your executable does not import the symbols from your library. Two typical patterns for doing that are shown below but you might want to read up on that first.

    The first method uses __declspec() to declare in the code what functions (or other items) are exported from your DLL and imported by other executables. You use a header file to declare the exported items and have a preprocessor flag used to control whether the symbols are exports or imports:

    mylib.h:

    #ifndef MYLIB_H
    #define MYLIB_H
    
    #if defined(BUILDING_MYLIB)
    #define MYLIB_API __declspec(dllexport) __stdcall
    #else
    #define MYLIB_API __declspec(dllimport) __stdcall
    #endif
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    int MYLIB_API helloworld(void);
    
    #ifdef __cplusplus
    }
    #endif
    
    #endif
    

    I have also specifically set the calling convention to __stdcall as are most DLL functions (I could have used WINAPI instead of __stdcall if I had included windows.h) and have declared the functions as extern "C" so their names do not get mangled when compiled as C++. Not such a problem here as it’s all C, but if you were to build the DLL from C source and then try to use it from a C++ executable then the imported names would be incorrect.

    The code could then look like this:

    mylib.c

    #include "mylib.h"
    #include <stdio.h>
    
    int MYLIB_API helloworld(void)
    {
        printf("Hello World DLL");
        return 42;
    }
    

    You’d build your DLL using the following command line. As well as creating the DLL it will create the import library (.lib) required to use your DLL from another executable (as well as the export file, but that is only required in certain circumstances):

    cl /DBUILDING_MYLIB mylib.c /LD
    

    The /DBUILDING_MYLIB argument defines the preprocessor symbol used to control whether the functions in the DLL are exports (if it is defined) or imports (not defined). So you’d define it when building the DLL but not when building your application.

    The /LD parameter tells cl to produce a DLL.

    The second method is to use module definition files as mentioned in the comments. You can use the code you already have but you also need to create the module definition file. At it’s simplest it looks like this:

    LIBRARY   mylib
    EXPORTS
       helloworld
    

    In this case to build the DLL you require the following command line:

    cl /LD mylib.c /link /DEF:mylib.def
    

    You could then code your application so that it used your library header with the imported version of your DLL function:

    main.c

    /* No need to include this if you went the module definition
     * route, but you will need to add the function prototype.
     */
    #include "mylib.h"
    
    int main(void)
    {
        helloworld();
        return (0);
    }
    

    Which you could then compile with the following command line (assuming the import library from the DLL creation is in the same directory as your main.c). This step is the same whether you used declspec or module definition files:

    cl main.c /link mylib.lib
    

    Arguments passed after the /link argument are passed onto the linker command line as they appear, so as just a filename it is used as extra input to link into the executable. In this case we specify the import library generated when we built the DLL.

    The command lines I’ve shown here are pretty much the absolute minimum you’d need but it’ll allow you to create a DLL and link an application to it.

    I have assumed the calling convention is correct in all of the above and I have not experimented much to see whether I got it wrong at any point.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my code: // Not all headers are relevant to the code snippet. #include
Here is an example: I have a file 1.js, which has some functions. I
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is a simplification of my database: Table: Property Fields: ID, Address Table: Quote
Here is my code, which takes two version identifiers in the form 1, 5,
Here's a coding problem for those that like this kind of thing. Let's see
Here is the scenario: I'm writing an app that will watch for any changes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.