在QtCreator中链接/使用外部库

时间:2015-07-10 14:10:12

标签: c++ c qt

使用mingw的Msys工具,我已成功构建了来自source 1.1.tar.gz的opus-codec。该版本生成了一些文件,其中包括libopus.alibopus-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(来自源包含文件夹)
  • libs [文件夹]
    • 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 个答案:

答案 0 :(得分:0)

opus.h中引用的标头文件:

#include "opus_types.h"
#include "opus_defines.h"

它们都来自source include文件夹,与opus.h相同。我认为如果将所有.h文件(包括opus.h 5)从源包含文件夹复制到文件夹结构中的include [文件夹]中,您的问题将得到解决。

头文件不包含在lib中,只包含cpp文件。您需要单独指定头文件。