gpt4 book ai didi

c++ - 在 QtCreator 中链接/使用外部库

转载 作者:太空宇宙 更新时间:2023-11-04 04:35:10 27 4
gpt4 key购买 nike

使用 mingw 的 Msys 工具,我已经成功地从 source 1.1.tar.gz 构建了 opus-codec。 .该构建生成了一些文件,其中包括 libopus.alibopus-0.dll。现在我想试试 trivial-example.c在 QtCreator 中。我将库添加到我的 .pro 文件中,并将 opus.h 包含在我的主文件中。编译器提示它找不到包含在 opus.h 中的 header 这些不应该包含在 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;

}

最佳答案

opus.h中引用的头文件:

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

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

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

关于c++ - 在 QtCreator 中链接/使用外部库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31342932/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com