C++ Libraryler hakkında sorular(aklıma takılan sorular)

hello.c:

#include <stdio.h>

void print_hello(void)
{
	puts("Hello!\n");
}
$ gcc -c hello.c -o hello.o
$ nm -g hello.o
                 U _GLOBAL_OFFSET_TABLE_
0000000000000000 T print_hello
                 U puts

main.c:

extern void print_hello(void);

int main(void)
{
	print_hello();
	return 0;
}
$ gcc main.c hello.o -o main1
$ ./main1
Hello!
$ ar rcs libhello.a hello.o
$ nm -g libhello.a

hello.o:
                 U _GLOBAL_OFFSET_TABLE_
0000000000000000 T print_hello
                 U puts
$ gcc -L. main.c -lhello -o main2
$ ./main2
Hello!
$ gcc -s -fPIC -shared hello.o -o hello.so
$ nm -gD hello.so
                 w __cxa_finalize
                 w __gmon_start__
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
0000000000001119 T print_hello
                 U puts

main_dyn.c:

#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	void *handle = dlopen("./hello.so", RTLD_NOW);
	if (handle == NULL) {
		printf("Error opening: %s\n", dlerror());
		goto exit;
	}

	void *symbol = dlsym(handle, "print_hello");
	if (symbol == NULL) {
		printf("Error loading symbol: %s\n", dlerror());
		goto close_exit;
	}

	void (*print_hello_dyn)(void) = symbol;
	print_hello_dyn();

close_exit:
	dlclose(handle);
exit:
	return 0;
}
$ gcc main_dyn.c -ldl -o main_dyn
$ ./main_dyn
Hello!
2 Beğeni