【问题标题】:gcc undefined reference togcc 未定义的引用
【发布时间】:2012-12-12 02:25:33
【问题描述】:

我在跑步。。

gcc -c -I/usr/vt/sample ttssample.c
gcc -L. -lttsapi ttssample.o -o ttsample

我收到以下错误...

ttssample.o: In function `_TTSFile':
ttssample.c:(.text+0x352): undefined reference to `TTSRequestFile'
ttssample.o: In function `_TTSFileEx':
ttssample.c:(.text+0x5e0): undefined reference to `TTSRequestFileEx'
ttssample.o: In function `_TTSBuffer':
ttssample.c:(.text+0x833): undefined reference to `_TTSRequestBuffer'
ttssample.o: In function `_TTSBufferEx':
ttssample.c:(.text+0xabd): undefined reference to `_TTSRequestBufferEx'
ttssample.o: In function `_TTSBuffering_cont':
ttssample.c:(.text+0xcbf): undefined reference to `_TTSRequestBuffer'
ttssample.o: In function `_TTSBuffering_stop':
ttssample.c:(.text+0xf2d): undefined reference to `_TTSRequestBuffer'
ttssample.o: In function `_TTSBuffering_SSML':
ttssample.c:(.text+0x122b): undefined reference to `_TTSRequestBufferSSMLEx'   
ttssample.o: In function `_TTSStatus':
ttssample.c:(.text+0x157b): undefined reference to `TTSRequestStatus'
collect2: ld returned 1 exit status

并且 TTSRequestFile 在 lib 标头中,但它的前面有 DllExport,我想知道这是我错误的原因吗?非常感谢任何帮助。

DllExport int TTSRequestFile(char *szServer, int nPort, char *pText, int nTextLen, char *szSaveDir, char *szSaveFile, int nSpeakerID, int nVoiceFormat);

【问题讨论】:

    标签: gcc undefined-reference


    【解决方案1】:

    您的链接命令错误。应在命令末尾指定库:

    gcc ttssample.o -o ttsample -L。 -lttsapi

    【讨论】:

    • 知道为什么运行可执行文件会出错吗?加载共享库时出错:-melf_x86_64:无法打开共享对象文件:没有这样的文件或目录
    • @JLB 因为库不在运行时加载程序默认搜索的目录中。您需要将库放在搜索过的目录中(如 /usr/local/lib),或导出 LD_LIBRARY_PATH 环境变量以包含库所在的目录(如 LD_LIBRARY_PATH="/home/me/mylib"),或您将使用指向 lib 所在目录的 rpath 链接您的程序,或者使用 rpath 的 $ORIGIN 功能始终在具有可执行文件相对路径的目录中查找 lib。在 Google 上搜索“rpath origin”以了解更多信息。
    【解决方案2】:

    您可以像这样在 DllExport 调用周围添加预处理器 ifdefines

    #ifdef _WIN32
    // we are on windows
    
    #elif defined __linux__
    //we are on linux
    
    #elif defined __APPLE__&__MACH__
    // we are on mac
    
    #endif // os specific
    

    我为我一直为其编译跨平台的三个平台添加。请注意,我用来识别平台的关键字可能会改变,但_WIN32 已经在 Windows 7 和 8 上进行了测试。 我认为是一年前在 sourceforge 上发现的。我现在找不到该页面,但如果我找到了,我会尽快与您联系。

    由于我还不能评论 Nikos C 的答案,所以我会在这里评论:你的链接命令是正确的,我当然看不到你的文件,所以我假设你的路径是正确的。 什么重要的是 -l 需要根据依赖关系按正确的顺序排列,但就我的经验而言,这通常不是问题。

    【讨论】:

      猜你喜欢
      • 2014-04-21
      • 2018-05-07
      • 2017-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-10
      相关资源
      最近更新 更多