This commit is contained in:
interfiberschool 2023-03-16 18:04:29 +00:00
parent d5ec6494be
commit 7240505bd1
10 changed files with 128 additions and 1 deletions

View file

@ -0,0 +1,19 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/debian
{
"name": "Debian",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/base:bullseye"
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Configure tool-specific properties.
// "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
bin

17
Makefile Normal file
View file

@ -0,0 +1,17 @@
exampleUnix: examples/unix/main.c examples/unix/lib.c src/dlopen.c
## BUILD UNIX EXAMPLES ##
mkdir -p bin
gcc examples/unix/main.c src/dlopen.c -o bin/exampleUnix -ldl
gcc examples/unix/lib.c -shared -o bin/exampleUnixLib.dll
exampleWin: examples/win/main.c examples/win/lib.c src/dlopen.c
## BUILD WINDOWS EXAMPLES ##
mkdir -p bin
x86_64-w64-mingw32-gcc examples/win/main.c src/dlopen.c -o bin/exampleWin
x86_64-w64-mingw32-gcc examples/win/lib.c -shared -o bin/exampleWinLib.dll
all: exampleUnix exampleWin

View file

@ -1 +0,0 @@

5
examples/unix/lib.c Normal file
View file

@ -0,0 +1,5 @@
#include <stdio.h>
void test(){
printf("testing 123\n");
}

6
examples/unix/main.c Normal file
View file

@ -0,0 +1,6 @@
#include "../../src/dlopen.h"
int main()
{
struct hotwire_dll_t dll = hw_dlopen("./bin/exampleUnixLib.dll", RTLD_NOW);
}

5
examples/win/lib.c Normal file
View file

@ -0,0 +1,5 @@
#include <stdio.h>
void test(){
printf("testing 123(windows)\n");
}

6
examples/win/main.c Normal file
View file

@ -0,0 +1,6 @@
#include "../../src/dlopen.h"
int main()
{
struct hotwire_dll_t dll = hw_dlopen("./bin/exampleWinLib.dll", 0);
}

32
src/dlopen.c Normal file
View file

@ -0,0 +1,32 @@
#include "dlopen.h"
#include <stdlib.h>
struct hotwire_dll_t hw_dlopen(const char *file, int flags) {
struct hotwire_dll_t dll;
// UNIX specific code(MacOS uses the same API as linux for dlopen)
#if defined(__linux__) || defined(__APPLE__)
dll.dll_handle = dlopen(file, flags);
if (dll.dll_handle == NULL){
printf("could not load dll(UNIX api call returned error)\n");
printf("error message: %s\n", dlerror());
exit(-1);
}
#endif
// Windows specific code
#if defined(_WIN32)
dll.dll_handle = LoadLibrary(file);
if (!dll.dll_handle){
printf("could not load dll(windows api call returned error)\n");
exit(-1);
}
#endif
return dll;
}

37
src/dlopen.h Normal file
View file

@ -0,0 +1,37 @@
#pragma once
#include <stdio.h>
#if defined(__APPLE__)
#define HW_MODE 0
#include <dlfcn.h>
#endif
#if defined(__linux__)
#define HW_MODE 1
#include <dlfcn.h>
#endif
#if defined(_WIN32)
#define HW_MODE 2
#include <windows.h>
#endif
struct hotwire_dll_t {
#if defined(__linux__) || defined(__APPLE__)
void* dll_handle;
#endif
#if defined(_WIN32)
HINSTANCE dll_handle;
#endif
};
// Function arguments are based off the dlopen() api on POSIX based operating systems
// Loads dll from library
struct hotwire_dll_t hw_dlopen(const char* file, int flags);
// Loads symbol from DLL
inline void* hw_dlsym(struct hotwire_dll_t dll, const char *__restrict symbol);
inline int hw_dlclose(void* handle);