0

I'm not that familiar with DLL libraries so I started making one that displays hello world. I followed the iniatial instructions form this microsoft site.

  1. I created a new DLL library, added an h file called Hello.c and a C file called Hello.c
  2. I created a new empty project and called Hello.c in my main.c file. I also included the Hello.h and Hello.c file paths using Property Pages/General/Additional Include Directories.

The code is really simple

//main.c

#include <stdio.h>
#include "Hello.h"

int main() {
    func();
    return 0;
}

//Hello.c

#pragma once
#include <stdio.h>
#include "Hello.h"

__declspec(dllexport) void func (void) {
    // printf() displays the string inside quotation
    {
        printf("Hello from DLL.\n");
    }

}

//Hello.h

#pragma once
#include <stdio.h>

#ifdef HELLO_WORLD_EXPORTS
#define HELLO_WORLD_API __declspec(dllexport)
#else
#define HELLO_WORLD_API __declspec(dllimport)
#endif
__declspec(dllexport) void func(void);

As it is, I get the error "unresolved external symbol _func referenced in function main". So I modified Hello.h to this instead:

#pragma once
#include <stdio.h>

#ifdef HELLO_WORLD_EXPORTS
#define HELLO_WORLD_API __declspec(dllexport)
#else
#define HELLO_WORLD_API __declspec(dllimport)
#endif
__declspec(dllexport) void func(void);

    void func(void)
{
    // printf() displays the string inside quotation
    {
        printf("Hello from DLL.\n");
    }

}

So this works, but I'm not using Hello.c anymore. Is it mandatory to have functions implementation in the headers files for a DLL library? or maybe I'm missing something else. Thanks.

pekoms
  • 55
  • 6
  • 2
    You define `HELLO_WORLD_API`, but it looks like you never use it. Try replacing `__declspec(dllexport)` with `HELLO_WORLD_API` in the header file. – Locke Aug 02 '21 at 04:42
  • I see what you mean, but I still get the same error. Also, when I right click on fun(); in main.c to "peek definition" it refers me to the Hello.c file. :/ – pekoms Aug 02 '21 at 04:57
  • In the project settings of the library (and only of the library) make sure `HELLO_WORLD_EXPORTS` is added to the preprocessor defines (under C/C++ -> Preprocessor) – PMF Aug 02 '21 at 06:50
  • 1
    Don't put implementation in the header file. I guess you just didn't link the lib file when you compiled your exe. – David Heffernan Aug 02 '21 at 08:23
  • Did you follow [this](https://learn.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=msvc-160#to-add-the-dll-import-library-to-your-project) in the linked page? – the busybee Aug 02 '21 at 09:06
  • Hey busybee. I followed the instructions on the page that you linked, (I was missing these two: To add the DLL import library to your project, To copy the DLL in a post-build event) It works now. However, for the post link part, I had to put the directory as this xcopy /y /d "C:\Users\user\OneDrive\Desktop\HelloWorld\Project1\Project1" I had the path like this at first, but it wouldn't find the path for the dll. xcopy /y /d "..\..\HelloWorld\$(IntDir)HelloWorld.dll" "$(OutDir)" Idk why. Thanks though. – pekoms Aug 02 '21 at 15:46

0 Answers0