In the program, it will use dlopen to get a function from a library. And the sockfd is defined in the main progrm, so when the function in the library execute, it caused a crash!
That is because the ld doesn't export the sockfd.
The solution is add the following parameters when compiling:
-rdynamic -Wl,--export-dynamic
-rdynamic
Pass the flag -export-dynamic to the ELF linker, on targets that support it. This instructs the linker to add all symbols, not only used ones, to the
dynamic symbol table. This option is needed for some uses of "dlopen" or to allow obtaining backtraces from within a program.