본문 바로가기

*집필*/*집필* Linux

리눅스 .so 파일 빌드

Build .so file from .c file using gcc command line


source file (so_test.c)

 #include <stdio.h>

void hello_so()

{

    printf("Hello .so\n");

}


command (01)

#gcc -c -fPIC  so_test.c -o test.o


command (02)

#gcc  test.o -shared -o libtest.so




source file (main.c)

#include <stdio.h>

#include <dlfcn.h>


void main(void)

{

        void *handle;

        void (*hello_so1)(void);

        char *error;


        handle = dlopen("./libtest.so", RTLD_LAZY);

        if(!handle)

        {

                printf("[ERROR] : %s\n", dlerror());

                exit(1);

        }


        hello_so1 = dlsym(handle, "hello_so");

        if( (error = dlerror()) != NULL)

        {

                printf("[ERROR] : %s\n", dlerror());

                exit(1);

        }


        hello_so1();

        dlclose(handle);


        return;

}


command (01)

 #gcc main.c -o main -ldl


command (02)

#./main

Hello!