2013-10-24 71 views
1

我在xcode和C++中使用GLUT & OpenGL製作遊戲。我希望把我在遊戲中的3D模型,這是頭文件的一般外觀:xcode ld:體系結構x86_64的8個重複符號

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,} 

我收到此錯誤吧:

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) 

我試圖顯示此我在我的視口顯示方法如下:

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

我有7個其他重複的符號小短語,但只有一個實際的錯誤。

回答

4

您在標題中定義了您的變量。這樣每個變量都存在於每個(8)編譯單元中。相反,聲明頭文件中的變量和定義了它們在.cpp文件中。

例如:

// 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}; 

extern告訴該變量的ADRESS稍後解決編譯器(由接頭)。 另外,如果您從不打算在運行時更改這些值,則應將其聲明爲const

/編輯:由於您使用的clang具有非常好的C++ 11支持,因此您也可以使用constexpr來獲取這些值。然後他們只在標題中。但是,理解鏈接器對於C++開發人員非常重要,因此原始的建議仍然存在。

+0

非常感謝您的幫助,因爲它解決了我的問題,但現在我得到了每個浮點數中「數組初始化符中的過量元素」的問題。我將如何解決這個問題? –

+0

大多數伊利你弄亂了元素的數量。 'float a [42] = {1,2};'不起作用。 – Marius

+0

哦jeez,theres成千上萬,我在終端使用.pl給我的頂點數等。感謝您幫助我解決這個問題。 –

相關問題