Undefined symbols for architecture i386: "MyGetOpenALAudioData(__CFURL const*, int*, int*, int*)"

时间:2023-03-08 18:28:55

今天把apple developer上的例子程序oalTouch中的MyOpenALSupport.h和MyOpenALSupport.c添加到自己的工程中,并在另一个文件xxx.cpp里调用,结果出现报错:

Undefined symbols for architecture i386:

"MyGetOpenALAudioData(__CFURL const*, int*, int*, int*)", referenced from:

Cc3dALBuffer::initBuffer(std::string const&, void*&, unsigned int&) in c3dALBuffer.o

"alBufferDataStaticProc(int, int, void*, int, int)", referenced from:

Cc3dALBuffer::initBuffer(std::string const&, void*&, unsigned int&) in c3dALBuffer.o

ld: symbol(s) not found for architecture i386

clang: error: linker command failed with exit code 1 (use -v to see invocation)

起初不解。后搜索到下帖:

http://www.idevgames.com/forums/printthread.php?tid=8148

才明白,原来c++调用.c文件中的函数是需要使用extern "C"的。

于是在xxx.h中加入以下代码:

extern"C" {

#include"MyOpenALSupport.h"

};

问题就解决了。

----补充:

在自己的项目中使用oalTouch中的MyOpenALSupport.h和MyOpenALSupport.c,结果发现某些音效会带有兹兹的杂音。但改用MusicCube例子程中的MyOpenALSupport.h就不会出现问题,对比了一下,发现oalTouch和MusicCube两个例子中的MyGetOpenALAudioData函数的实现却实有差别,那么只能认为oalTouch中MyGetOpenALAudioData的实现有bug了。

注,改用MusicCube中的MyOpenALSupport.h。起初编译不过,MyGetOpenALAudioData中的goto Exit语句报"goto into protected scope"的错误。于是把MusicCube中的MyOpenALSupport.h也拆成MyOpenALSupport.h和MyOpenALSupport.c两个文件,然后就可以编译通过了。