xcode ld: 8 duplicate symbols for architecture x86_64

前端 未结 1 1944
没有蜡笔的小新
没有蜡笔的小新 2021-01-24 04:27

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:

unsigned int G         


        
相关标签:
1条回答
  • 2021-01-24 05:05

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

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

    extern tells 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.

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

    0 讨论(0)
提交回复
热议问题