c++ - 使用库中定义的模板时找不到符号

标签 c++ xcode xmp

我正在尝试在 iOS 应用程序中使用 adobe xmp 库,但出现链接错误。我的路径中有适当的 header 和库,但出现链接错误。我仔细检查以确保标题和库在我的路径上。我检查了方法名称的错位,但它们不在库中(我使用 nm 命令进行了检查)。我究竟做错了什么?

库头:

#if defined ( TXMP_STRING_TYPE )

    #include "TXMPMeta.hpp"
    #include "TXMPIterator.hpp"
    #include "TXMPUtils.hpp"
    typedef class TXMPMeta <TXMP_STRING_TYPE>     SXMPMeta;       // For client convenience.
    typedef class TXMPIterator <TXMP_STRING_TYPE> SXMPIterator;
    typedef class TXMPUtils <TXMP_STRING_TYPE>    SXMPUtils;

.mm 文件:
#include <string>
using namespace std;
#define IOS_ENV
#define TXMP_STRING_TYPE string
#import "XMP.hpp"

void DoStuff()
{    
    SXMPMeta meta;
    string returnValue;
    meta.SetProperty ( kXMP_NS_PDF, "test", "{ formId: {guid} }" );
    meta.DumpObject(DumpToString, &returnValue);
}

链接错误:
(null): "TXMPMeta<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::DumpObject(int (*)(void*, char const*, unsigned int), void*) const", referenced from:
(null): "TXMPMeta<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::TXMPMeta()", referenced from:
(null): "TXMPMeta<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::SetProperty(char const*, char const*, char const*, unsigned int)", referenced from:
(null): "TXMPMeta<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::~TXMPMeta()", referenced from:
(null): Linker command failed with exit code 1 (use -v to see invocation)

最佳答案

基本上发生的事情是您只有标题中的定义
如果我说
template<class T> T something(T);某处,它告诉编译器“相信我,兄弟,它存在,把它留给链接器”

并将符号添加到目标文件中,就好像它确实存在一样。因为它可以看到原型(prototype),它知道有多少堆栈空间,它返回什么类型等等,所以它只是设置它,这样链接器就可以进来并将函数的地址放入其中。

但在你的情况下,没有地址。您/必须/在同一个文件中有模板定义(不仅仅是声明),因此编译器可以创建一个(具有弱链接)所以这里假设它们存在,但它实际上没有从模板中删除这个类,所以链接器找不到它,因此出现错误。

现在会弄乱我的答案,希望这会有所帮助。

附录 1:

template<class T> void output(T&);

int main(int,char**) {
    int x = 5;
    output(x);
    return 0;
}

这将编译但 不是 关联。

输出:
if ! g++ -Isrc -Wall -Wextra -O3 -std=c++11 -g -gdwarf-2 -Wno-write-strings  -MM src/main.cpp >> build/main.o.d ; then rm build/main.o.d ; exit 1 ; fi
g++ -Wall -Wextra -O3 -std=c++11 -g -gdwarf-2 -Wno-write-strings  -Isrc -c src/main.cpp -o build/main.o
g++  build/main.o  -o a.out
build/main.o: In function `main':
(my home)/src/main.cpp:13: undefined reference to `void output<int>(int&)'
collect2: error: ld returned 1 exit status
make: *** [a.out] Error 1

(我为此劫持了一个开放项目,因此得名)

如您所见,编译命令工作正常(以 -o build/main.o 结尾的那个),因为我们告诉它“看看这个函数存在”

因此,在目标文件中,它对链接器说(以某种“名称管理形式”以保留模板)“将位置放在 void output(int&); 的内存中”,链接器找不到它。

编译和链接
#include <iostream>
template<class T> void output(T&);

int main(int,char**) {
    int x = 5;
    output(x);
    return 0;
}

template<class T> void output(T& what) {
    std::cout<<what<<"\n";
    std::cout.flush();
}

注意第 2 行,我们告诉它“在 T 中存在一个函数,一个名为 output 的模板,它不返回任何内容并接受 T 引用”,这意味着它可以在主函数中使用它(请记住,当它解析主函数时) '还没有看到输出的定义,它刚刚被告知它存在),然后链接器修复它。 '尽管现代编译器要聪明得多(因为我们有更多的内存 :))并且会破坏代码的结构,但链接时间优化会做得更多,但这就是它过去的工作方式,以及如何考虑这几天上类。

输出:
make all 
if ! g++ -Isrc -Wall -Wextra -O3 -std=c++11 -g -gdwarf-2 -Wno-write-strings  -MM src/main.cpp >> build/main.o.d ; then rm build/main.o.d ; exit 1 ; fi
g++ -Wall -Wextra -O3 -std=c++11 -g -gdwarf-2 -Wno-write-strings  -Isrc -c src/main.cpp -o build/main.o
g++  build/main.o  -o a.out

如您所见,它编译得很好并且链接得很好。

多个不包含作为证明的文件

主.cpp
#include <iostream>

int TrustMeCompilerIExist();

int main(int,char**) {
    std::cout<<TrustMeCompilerIExist();
    std::cout.flush();
    return 0;
}

证明.cpp
int TrustMeCompilerIExist() {
    return 5;
}

编译链接
make all 
if ! g++ -Isrc -Wall -Wextra -O3 -std=c++11 -g -gdwarf-2 -Wno-write-strings  -MM src/main.cpp >> build/main.o.d ; then rm build/main.o.d ; exit 1 ; fi
g++ -Wall -Wextra -O3 -std=c++11 -g -gdwarf-2 -Wno-write-strings  -Isrc -c src/main.cpp -o build/main.o
if ! g++ -Isrc -Wall -Wextra -O3 -std=c++11 -g -gdwarf-2 -Wno-write-strings  -MM src/proof.cpp >> build/proof.o.d ; then rm build/proof.o.d ; exit 1 ; fi
g++ -Wall -Wextra -O3 -std=c++11 -g -gdwarf-2 -Wno-write-strings  -Isrc -c src/proof.cpp -o build/proof.o
g++  build/main.o build/proof.o  -o a.out

(输出 5)

记住#include LITERALLY 会转储一个文件,其中显示“#include”(+ 一些其他调整行号的宏),这称为翻译单元。而不是使用头文件来包含“int TrustMeCompilerIExist();”它声明该函数存在(但编译器再次不知道它在哪里,它里面的代码,只是它存在)我重复了自己。

让我们看看proof.o

命令
objdump proof.o -t

输出
proof.o:     file format elf64-x86-64

SYMBOL TABLE:
0000000000000000 l    df *ABS*  0000000000000000 proof.cpp
0000000000000000 l    d  .text  0000000000000000 .text
0000000000000000 l    d  .data  0000000000000000 .data
0000000000000000 l    d  .bss   0000000000000000 .bss
0000000000000000 l    d  .debug_info    0000000000000000 .debug_info
0000000000000000 l    d  .debug_abbrev  0000000000000000 .debug_abbrev
0000000000000000 l    d  .debug_aranges 0000000000000000 .debug_aranges
0000000000000000 l    d  .debug_line    0000000000000000 .debug_line
0000000000000000 l    d  .debug_str 0000000000000000 .debug_str
0000000000000000 l    d  .note.GNU-stack    0000000000000000 .note.GNU-stack
0000000000000000 l    d  .eh_frame  0000000000000000 .eh_frame
0000000000000000 l    d  .comment   0000000000000000 .comment
0000000000000000 g     F .text  0000000000000006 _Z21TrustMeCompilerIExistv

就在底部,有一个函数,位于文件的偏移量 6 处,带有调试信息,(尽管 g 是全局的)你可以看到它被称为 _Z (这就是为什么 _ 被保留用于某些东西,我忘了到底是什么。 .. 但它与此有关)并且 Z 是“整数”,21 是名称长度,在名称之后,v 是“void”返回类型。

顺便说一句,开头的零是节号,请记住二进制文件可能很大。

拆机
运行:
objdump proof.o -S
proof.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <_Z21TrustMeCompilerIExistv>:


int TrustMeCompilerIExist() {
    return 5;
}
   0:   b8 05 00 00 00          mov    $0x5,%eax
   5:   c3                      retq   

因为我有 -g 你可以看到它放置了与程序集相关的代码(它对更大的函数更有意义,它向你展示了以下指令直到下一个代码块实际执行的操作)通常不会存在。

main.o

这是符号表,与上面的方法相同:
objdump main.o -t

main.o:     file format elf64-x86-64

SYMBOL TABLE:
0000000000000000 l    df *ABS*  0000000000000000 main.cpp
0000000000000000 l    d  .text  0000000000000000 .text
0000000000000000 l    d  .data  0000000000000000 .data
0000000000000000 l    d  .bss   0000000000000000 .bss
0000000000000000 l    d  .text.startup  0000000000000000 .text.startup
0000000000000030 l     F .text.startup  0000000000000026 _GLOBAL__sub_I_main
0000000000000000 l     O .bss   0000000000000001 _ZStL8__ioinit
0000000000000000 l    d  .init_array    0000000000000000 .init_array
0000000000000000 l    d  .debug_info    0000000000000000 .debug_info
0000000000000000 l    d  .debug_abbrev  0000000000000000 .debug_abbrev
0000000000000000 l    d  .debug_loc 0000000000000000 .debug_loc
0000000000000000 l    d  .debug_aranges 0000000000000000 .debug_aranges
0000000000000000 l    d  .debug_ranges  0000000000000000 .debug_ranges
0000000000000000 l    d  .debug_line    0000000000000000 .debug_line
0000000000000000 l    d  .debug_str 0000000000000000 .debug_str
0000000000000000 l    d  .note.GNU-stack    0000000000000000 .note.GNU-stack
0000000000000000 l    d  .eh_frame  0000000000000000 .eh_frame
0000000000000000 l    d  .comment   0000000000000000 .comment
0000000000000000 g     F .text.startup  0000000000000026 main
0000000000000000         *UND*  0000000000000000 _Z21TrustMeCompilerIExistv
0000000000000000         *UND*  0000000000000000 _ZSt4cout
0000000000000000         *UND*  0000000000000000 _ZNSolsEi
0000000000000000         *UND*  0000000000000000 _ZNSo5flushEv
0000000000000000         *UND*  0000000000000000 _ZNSt8ios_base4InitC1Ev
0000000000000000         *UND*  0000000000000000 .hidden __dso_handle
0000000000000000         *UND*  0000000000000000 _ZNSt8ios_base4InitD1Ev
0000000000000000         *UND*  0000000000000000 __cxa_atexit

看看它怎么说未定义,那是因为它不知道它在哪里,它只知道它存在(连同标准库的东西,链接器会自己找到)

结束
使用 HEADER GUARDS 并使用模板将 #include file.cpp 放在底部,然后再关闭标题保护。这样你就可以像往常一样包含头文件:)

关于c++ - 使用库中定义的模板时找不到符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18543980/

相关文章:

c++ - ID3DX11EffectVariable::AsSharedResource() 有什么作用?

c++ - Arduino 整数错误

c++ - 哪里可以获得boost线程源代码

metadata - 如何在 Objective C 中保留 XMP 元数据?

c++ - 新手问题 : C/C++ with Eclipse

iphone - 使用GameCenter获取附近的玩家列表

xcode - Unity AR Foundation Xcode 项目中架构 arm64 的 undefined symbol

Xcode 7 错误代码“无法转换值类型([AnyObject]

c++ - 在 Adob​​e XMP SDK 中找不到 SXMPMeta

尝试读取图像元数据时,Python XMP Toolkit 过于严格 ("Unrecognized TIFF prefix")