-
Notifications
You must be signed in to change notification settings - Fork 0
/
call_cpucount_lib.c
31 lines (28 loc) · 1.19 KB
/
call_cpucount_lib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#define SHARED_CPU_LIB "./cpucount_lib.so"
int main() {
int num_cpu_cores;
void *cpucount_lib;
int (*ext_get_num_cpu_cores)(void);
cpucount_lib = dlopen(SHARED_CPU_LIB, RTLD_LAZY);
if (cpucount_lib != NULL) {
/*
* `man dlopen` says that C99 standard leaves casting from
* `void *` to a function pointer undefined. The following is
* the POSIX.1-2003 workaround found in man page. See also the
* following StackOverflow answer:
* https://stackoverflow.com/a/19487645/236007
*/
*(void **)(&ext_get_num_cpu_cores) = dlsym(cpucount_lib, "get_num_cpu_cores");
num_cpu_cores = ext_get_num_cpu_cores();
dlclose(cpucount_lib);
printf("Number of CPU cores: %d\n", num_cpu_cores);
return EXIT_SUCCESS;
} else {
printf("ERROR opening the shared library %s!\n", SHARED_CPU_LIB);
return ELIBACC; // "Can not access a needed shared library."
}
}