在 QtCreator 中链接/使用外部库
Posted
技术标签:
【中文标题】在 QtCreator 中链接/使用外部库【英文标题】:Link / use external library in QtCreator 【发布时间】:2015-07-10 14:10:11 【问题描述】:使用 mingw 的 Msys 工具,我已经成功地从 source 1.1.tar.gz 构建了 opus-codec。该构建生成了一些文件,其中包括libopus.a
和libopus-0.dll
。现在我想试试 QtCreator 中的trivial-example.c。我将 lib 添加到我的 .pro
文件中,并将 opus.h
包含在我的主文件中。编译器抱怨它找不到 opus.h
中包含的标头,这些不应该包含在 lib 中吗?我需要如何设置我的应用程序以运行“简单示例”?
我的文件夹结构是:
main.cpp opus_lib_test.pro opus_lib_test.pro.user 包括[文件夹] opus.h(来自源包含文件夹) 库 [文件夹] libopus.a libopus-0.dll我的.pro
-文件看起来像
QT += core
QT -= gui
TARGET = opus_lib_test
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
INCLUDEPATH += $$PWD/include
LIBS += -L"C:/Qt/Qt5.2.1/Tools/QtCreator/bin/opus_lib_test/libs/" -llibopus
SOURCES += main.cpp
HEADERS += include/opus.h
我的main.cpp
在这里:
//#include <QCoreApplication>
#include "opus.h"
int main(int argc, char *argv[])
// QCoreApplication a(argc, argv);
// return a.exec();
// ----------------------------- trivial_example.c
char *inFile;
FILE *fin;
char *outFile;
FILE *fout;
opus_int16 in[FRAME_SIZE*CHANNELS];
opus_int16 out[MAX_FRAME_SIZE*CHANNELS];
unsigned char cbits[MAX_PACKET_SIZE];
int nbBytes;
/*Holds the state of the encoder and decoder */
OpusEncoder *encoder;
OpusDecoder *decoder;
int err;
if (argc != 3)
fprintf(stderr, "usage: trivial_example input.pcm output.pcm\n");
fprintf(stderr, "input and output are 16-bit little-endian raw files\n");
return EXIT_FAILURE;
/*Create a new encoder state */
encoder = opus_encoder_create(SAMPLE_RATE, CHANNELS, APPLICATION, &err);
if (err<0)
fprintf(stderr, "failed to create an encoder: %s\n", opus_strerror(err));
return EXIT_FAILURE;
/* Set the desired bit-rate. You can also set other parameters if needed.
The Opus library is designed to have good defaults, so only set
parameters you know you need. Doing otherwise is likely to result
in worse quality, but better. */
err = opus_encoder_ctl(encoder, OPUS_SET_BITRATE(BITRATE));
if (err<0)
fprintf(stderr, "failed to set bitrate: %s\n", opus_strerror(err));
return EXIT_FAILURE;
inFile = argv[1];
fin = fopen(inFile, "r");
if (fin==NULL)
fprintf(stderr, "failed to open file: %s\n", strerror(errno));
return EXIT_FAILURE;
/* Create a new decoder state. */
decoder = opus_decoder_create(SAMPLE_RATE, CHANNELS, &err);
if (err<0)
fprintf(stderr, "failed to create decoder: %s\n", opus_strerror(err));
return EXIT_FAILURE;
outFile = argv[2];
fout = fopen(outFile, "w");
if (fout==NULL)
fprintf(stderr, "failed to open file: %s\n", strerror(errno));
return EXIT_FAILURE;
while (1)
int i;
unsigned char pcm_bytes[MAX_FRAME_SIZE*CHANNELS*2];
int frame_size;
/* Read a 16 bits/sample audio frame. */
fread(pcm_bytes, sizeof(short)*CHANNELS, FRAME_SIZE, fin);
if (feof(fin))
break;
/* Convert from little-endian ordering. */
for (i=0;i<CHANNELS*FRAME_SIZE;i++)
in[i]=pcm_bytes[2*i+1]<<8|pcm_bytes[2*i];
/* Encode the frame. */
nbBytes = opus_encode(encoder, in, FRAME_SIZE, cbits, MAX_PACKET_SIZE);
if (nbBytes<0)
fprintf(stderr, "encode failed: %s\n", opus_strerror(nbBytes));
return EXIT_FAILURE;
/* Decode the data. In this example, frame_size will be constant because
the encoder is using a constant frame size. However, that may not
be the case for all encoders, so the decoder must always check
the frame size returned. */
frame_size = opus_decode(decoder, cbits, nbBytes, out, MAX_FRAME_SIZE, 0);
if (frame_size<0)
fprintf(stderr, "decoder failed: %s\n", opus_strerror(err));
return EXIT_FAILURE;
/* Convert to little-endian ordering. */
for(i=0;i<CHANNELS*frame_size;i++)
pcm_bytes[2*i]=out[i]&0xFF;
pcm_bytes[2*i+1]=(out[i]>>8)&0xFF;
/* Write the decoded audio to file. */
fwrite(pcm_bytes, sizeof(short), frame_size*CHANNELS, fout);
/*Destroy the encoder state*/
opus_encoder_destroy(encoder);
opus_decoder_destroy(decoder);
fclose(fin);
fclose(fout);
return EXIT_SUCCESS;
【问题讨论】:
【参考方案1】:opus.h
中引用的头文件:
#include "opus_types.h"
#include "opus_defines.h"
它们都来自源包含文件夹,与opus.h
相同。我认为如果您将所有 .h 文件(包括 opus.h
)从源包含文件夹复制到文件夹结构中的包含 [文件夹] 中,您的问题将得到解决。
lib 中不包含头文件,仅包含 cpp 文件。需要单独指定头文件。
【讨论】:
以上是关于在 QtCreator 中链接/使用外部库的主要内容,如果未能解决你的问题,请参考以下文章
QtCreator里添加外部第三库头文件路径的方法(.pro文件)