2015-07-10 141 views
1

使用mingw的Msys工具,我已經成功地從source 1.1.tar.gz構建opus-codec。該版本生成了一些文件,其中有libopus.alibopus-0.dll。現在我想嘗試QtCreator中的trivial-example.c。我將lib添加到我的.pro文件中,並在我的主文件中包含opus.h。編譯器抱怨它找不到包含在opus.h中的頭文件不應該包含在lib中嗎?我如何設置我的應用程序來運行「小例子」?在QtCreator中鏈接/使用外部庫

我的文件夾結構是:

  • main.cpp中
  • opus_lib_test.pro
  • opus_lib_test.pro.user
  • 包括[文件夾]
    • opus.h(從源包括文件夾)
  • 個庫[文件夾]
    • libopus.a
    • libopus-0.dll

.pro -file看起來像

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; 

} 

回答

0

opus.h引用的頭文件:

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

他們都從源文件夾包括,同爲opus.h。我認爲如果您將所有.h文件(包括opus.h)從源包含文件夾複製到文件夾結構中的include [文件夾],您的問題將得到解決。

頭文件不包含在lib中,只包含cpp文件。您需要單獨指定頭文件。