-
-
Save ucasfl/70fe97365f5794c43fa6bcb85e2eb26d to your computer and use it in GitHub Desktop.
A sample of using dlopen library.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <dlfcn.h> | |
int main(int argc, char** argv) | |
{ | |
void *handle; | |
void (*func_print_name)(const char*); | |
if (argc != 2) { | |
fprintf(stderr, "Usage: %s animal_type\n", argv[0]); | |
return EXIT_FAILURE; | |
} | |
if (strcmp(argv[1], "dog") == 0) { | |
handle = dlopen("./libdog.so", RTLD_LAZY); | |
} else if (strcmp(argv[1], "cat") == 0) { | |
handle = dlopen("./libcat.so", RTLD_LAZY); | |
} else { | |
fprintf(stderr, "Error: unknown animal type: %s\n", argv[1]); | |
return EXIT_FAILURE; | |
} | |
if (!handle) { | |
/* fail to load the library */ | |
fprintf(stderr, "Error: %s\n", dlerror()); | |
return EXIT_FAILURE; | |
} | |
*(void**)(&func_print_name) = dlsym(handle, "print_name"); | |
if (!func_print_name) { | |
/* no such symbol */ | |
fprintf(stderr, "Error: %s\n", dlerror()); | |
dlclose(handle); | |
return EXIT_FAILURE; | |
} | |
func_print_name(argv[1]); | |
dlclose(handle); | |
return EXIT_SUCCESS; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
void print_name(const char* type); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "animal.h" | |
#include <stdio.h> | |
void print_name(const char* type) | |
{ | |
printf("Tama is a %s.\n", type); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "animal.h" | |
#include <stdio.h> | |
void print_name(const char* type) | |
{ | |
printf("Pochi is a %s.\n", type); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app = dlopen_sample | |
lib = libcat.so libdog.so | |
CFLAGS = -Wall -ansi -pedantic | |
LDFLAGS = -ldl | |
all: $(app) $(lib) | |
lib%.so: %.c | |
$(CC) -shared -fPIC $(CFLAGS) -o $@ $< | |
clean: | |
$(RM) $(app) $(lib) | |
run: all | |
./$(app) cat | |
./$(app) dog | |
-./$(app) bear | |
$(lib): animal.h |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment