如何在Qt Creator中编译运行pjsua2 Demo并解决链接错误
Let’s work through this linking issue together—since the official pjsua2_demo runs perfectly after compiling PJProject, the problem almost certainly lies in how your Qt project is configuring library includes and links. Here are the most effective fixes to try:
1. Verify Your pkg-config Setup in the .pro File
First, double-check that your Qt project is pulling in all required PJProject components via pkg-config. It’s easy to only link against pjsua2 and miss its dependencies like pjmedia or pjlib-util.
Make sure your .pro file has these lines:
CONFIG += link_pkgconfig PKGCONFIG += pjsua2
If you compiled PJProject to a custom directory (not system-wide), you’ll need to tell pkg-config where to find the .pc files first. Open a terminal, set the path, then restart Qt Creator:
export PKG_CONFIG_PATH=/path/to/your/pjproject/lib/pkgconfig:$PKG_CONFIG_PATH
2. Manually Add Missing System Dependencies
The undefined references you’re seeing (gsm_decode, speex_bits_read_from, snd_device_name_hint) come from system libraries that PJProject relies on but aren’t being auto-linked by pkg-config in some cases. Add these directly to your .pro file:
LIBS += -lgsm -lspeex -lasound
This covers the GSM codec, Speex codec, and ALSA audio backend—exactly the components throwing errors.
3. Cross-Check PJProject’s Compile Configuration
Since the official demo works, you probably have these components enabled, but it’s worth confirming. Navigate to your PJProject build directory and open config_site.h. Look for these defines to ensure they’re set to 1:
#define PJMEDIA_HAS_GSM_CODEC 1 #define PJMEDIA_HAS_SPEEX_CODEC 1 #define PJMEDIA_AUDIO_DEV_HAS_ALSA 1
If any are set to 0, you’ll need to recompile PJProject with these features enabled (using ./configure --enable-gsm --enable-speex --enable-alsa or similar flags).
4. Copy Exact Link Flags from the Official Demo
If the above steps don’t work, let’s steal the working link command from the official demo. In your PJProject directory, go to pjsip-apps/build/samples, then run:
make V=1 pjsua2_demo
This will print the full compilation and linking command. Look for the part starting with g++ ... -o pjsua2_demo—copy all the -l flags (like -lpjsua2, -lpjmedia-codec2, etc.) and paste them into your Qt .pro file’s LIBS section. This ensures you’re linking every single library the official demo uses.
Final Notes
Start with step 2 first—it’s the quickest fix for these specific undefined reference errors. If that doesn’t resolve the issue, move to verifying pkg-config paths or copying the official link flags.
内容的提问来源于stack exchange,提问作者Svaba1012

