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.
- I created a new DLL library, added an h file called Hello.c and a C file called Hello.c
- 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.