如何在Java中使用PJSIP/PJNATH?STUN编译报错求助
Hey there, fellow PJSIP newbie! Let's break down why you're seeing those undefined symbol errors and how to fix them.
The Root Cause
Your compile command only builds stun_msg.c, but STUN message functions in PJNATH depend on PJLIB (the core PJSIP utility library where pj_pool_calloc and other pool management functions live) and other internal PJNATH modules. You're not linking against these required libraries, so the linker can't locate the definitions for those missing symbols.
Step-by-Step Fix
Ensure PJLIB and PJNATH are fully compiled first
Before building your custom STUN library, make sure you've completed compiling the full PJSIP/PJNATH source tree. This will generate the shared libraries (likelibpj.so,libpjnath.so,libpjlib-util.so) that your code needs to link against.Update your compile command to include required libraries
Modify yourgcccommand to add:-L/path/to/pj/libraries: Replace this with the actual path where your compiled PJ shared libraries are stored (e.g.,/usr/local/libor thelibdirectory in your PJSIP build folder).- Link flags for mandatory PJ libraries:
-lpj(PJLIB core),-lpjnath(PJNATH core),-lpjlib-util(utility functions often used with STUN).
Here's a corrected example command:
gcc -Wall -g -shared -o lib_stun_msg.so -fPIC stun_msg.c -Wl,-z,defs -L/usr/local/lib -lpj -lpjnath -lpjlib-utilAdd header path if needed
If the compiler can't find PJ header files (likepj/pool.h), add the-I/path/to/pj/includeflag to point to the directory containing PJ's header files:gcc -Wall -g -shared -o lib_stun_msg.so -fPIC stun_msg.c -Wl,-z,defs -I/usr/local/include -L/usr/local/lib -lpj -lpjnath -lpjlib-util
Extra Tips
- If you built PJ libraries as static libraries instead of shared ones, replace
-lpjetc. with the full path to the.afiles (e.g.,/usr/local/lib/libpj.a). - Double-check that your build configuration (debug/release, 32/64-bit) matches between the PJ libraries and your custom STUN library—mismatches can cause unexpected linking errors.
内容的提问来源于stack exchange,提问作者Rashed

