xcode ld:架构 x86_64 的 8 个重复符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19576245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:11:20  来源:igfitidea点击:

xcode ld: 8 duplicate symbols for architecture x86_64

c++xcodeopenglclangdebug-symbols

提问by David Graovac

I am making a game in xcode and c++ with GLUT & OpenGL. I want to place a 3D model in my game and this is the general look of the header file:

我正在使用 GLUT 和 OpenGL 用 xcode 和 c++ 制作游戏。我想在我的游戏中放置一个 3D 模型,这是头文件的一般外观:

unsigned int GunNumVerts = 37812;

float GunVerts [] = {
// f 1/1/1 1582/2/1 4733/3/1
 0.266494348503772, 0.0252334302709736, -0.000725898139236535,
0.265592372987502, 0.0157389511523397, -0.000725898139236535,
0.264890836474847, 0.0182004476109518, -0.00775888079925833,}
float GunNormals [] = {
// f 1/1/1 1582/2/1 4733/3/1
0.986904930120225, -0.0937549933614904, -0.131257990706016,
0.986904930120225, -0.0937549933614904, -0.131257990706016,
0.986904930120225, -0.0937549933614904, -0.131257990706016,}
float GunTexCoords [] = {
// f 1/1/1 1582/2/1 4733/3/1
0.110088, 0.229552,
0.108891, 0.243519,
0.119508, 0.240861,}

I am getting this error with it:

我收到此错误:

duplicate symbol _GunNumVerts in: /blah/blah/blah/Mouse.o
/blah/blah/blah/ViewPort.o
ld: 8 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I am trying to display this in my display method in my viewport as follows:

我试图在我的视口中的显示方法中显示它,如下所示:

glVertexPointer(3, GL_FLOAT, 0, GunVerts);
glNormalPointer(GL_FLOAT, 0, GunNormals);
glTexCoordPointer(2, GL_FLOAT, 0, GunTexCoords);
glDrawArrays(GL_TRIANGLES, 0, GunNumVerts);

I have 7 other duplicate symbol little phrases but there is only one actual error.

我有 7 个其他重复的符号小短语,但只有一个实际错误。

回答by Marius

You defined your variables in the header. That way each variable is present in every (8) compilation units. Instead declarethe variables in the header and definethem in a .cpp file.

您在标题中定义了变量。这样每个变量都存在于每 (8) 个编译单元中。而是在标头中声明变量并在 .cpp 文件中定义它们。

For example:

例如:

// Gun.h:
extern unsigned int GunNumVerts;
extern float GunVerts[9];


// Gun.cpp:
unsigned int GunNumVerts;
float GunVerts[9] = {
    // f 1/1/1 1582/2/1 4733/3/1
    0.266494348503772, 0.0252334302709736, -0.000725898139236535,
    0.265592372987502, 0.0157389511523397, -0.000725898139236535,
    0.264890836474847, 0.0182004476109518, -0.00775888079925833};

externtells the compiler that the adress of the variable is resolved later (by the linker). Also, if you never intend to change these values at runtime they should be declared as const.

extern告诉编译器变量的地址稍后解析(由链接器)。此外,如果您从不打算在运行时更改这些值,则应将它们声明为const.

/Edit: since you are using clang which has a very good C++11 support, you could also use constexprfor these values. Then they only reside in the header. However, understanding the linker is important for a C++ developer so the original advice stays.

/编辑:由于您使用的是具有非常好的 C++11 支持的 clang,您也可以使用constexpr这些值。然后它们只驻留在标题中。但是,了解链接器对于 C++ 开发人员很重要,因此保留原始建议。