Qt开发C++音频应用时链接libsox库出现undefined reference to `sox_open_read'错误的求助
Hey there, I’ve dealt with this exact linking headache when using libsox in Qt C++ projects—let’s get this sorted out for you.
First, let’s clarify the root cause: your IDE can auto-complete sox_open_read because it’s finding the header files correctly, but the linker can’t locate the actual libsox binary library (static or dynamic) that contains the compiled implementation of this function. That’s why you’re hitting the "undefined reference" error.
Here’s how to fix it step by step:
1. Make sure libsox is properly compiled/installed
- If you built libsox from the source you downloaded, double-check that you generated a linkable library file. You should see something like
libsox.a(static library) orlibsox.so(Linux dynamic library)/libsox.dll(Windows) in your build output. If you only compiled the Sox command-line tool, you’ll need to recompile the source with the correct flags to build the library. - On Linux systems, you can skip building from source and install the pre-built development package instead:
sudo apt-get install libsox-dev(this includes both headers and the library files).
2. Configure your Qt .pro file correctly
This is the most critical step—you need to tell Qt’s build system where to find the libsox library and link against it. Add these lines to your project’s .pro file:
# Add path to sox header files (adjust if your src folder is in a different location) INCLUDEPATH += $$PWD/src # Add path to the folder containing the libsox library file LIBS += -L/path/to/your/sox/library/folder # Link against the sox library LIBS += -lsox
For example, if your libsox library is in a lib folder at the root of your Qt project, it would look like this:
INCLUDEPATH += $$PWD/src LIBS += -L$$PWD/lib -lsox
- Note for Windows: If you’re using a dynamic library, you might need to specify the exact library name (e.g.,
-lsox-1) or directly reference the.libfile path.
3. Check architecture compatibility
Ensure that your Qt project’s build architecture (32-bit vs 64-bit) matches the architecture of the libsox library you’re using. If you’re building a 64-bit Qt app, libsox must also be compiled for 64-bit—mismatches will cause linking failures.
4. Verify the library contains the required symbol
To confirm that sox_open_read exists in your libsox library, use the nm command (Linux/macOS) or a similar tool on Windows:
nm -D libsox.so | grep sox_open_read
If you see output with the symbol name, that means the library has the function; if not, you’ll need to recompile libsox correctly.
Once you’ve made these changes, clean your Qt project (Build > Clean All), then rebuild it. The linker should now find the libsox library and resolve the sox_open_read reference.
内容的提问来源于stack exchange,提问作者S At

