locked
Error_1_error LNK2001: unresolved external symbol "struct Turtle turtle" (?turtle@@3UTurtle@@A) RRS feed

  • Question

  • Hi,

    I'm trying to create a static library of functions for a graphics positioning system using opengl. The sytstem is based around a turtle.I'm getting the following errors when I try to compile my code:

     

    Error 1  error LNK2001: unresolved external symbol "struct Turtle turtle" (?turtle@@3UTurtle@@A) C:\Users\Documents\Visual Studio 2010\Projects\TurtleDraw\turtleRef\turtleRef.obj turtleRef

     

     

    Error 2  error LNK2001: unresolved external symbol "struct Turtle turtle" (?turtle@@3UTurtle@@A) C:\Users\Documents\Visual Studio 2010\Projects\TurtleDraw\turtleRef\TurtleDraw.lib(turtle.obj) turtleRef

    Error 3  error LNK1120: 1 unresolved externals C:\Users\Documents\Visual Studio 2010\Projects\TurtleDraw\Debug\turtleRef.exe turtleRef

     

     Here's the header file:

    #ifndef TURTLE_H
    #define TURTLE_H
    
    #include<cmath>
    #include<iostream>
    using namespace std;
    
    #include<GL/glut.h>
    
    enum status {UP, DOWN};
    
    struct Turtle{
    	Turtle(){
    		pos[0] = 0.0;
    		pos[1] = 0.0;
    		angle = 45.0;
    		pen = UP;
    	}
    	GLfloat pos[2];
    	GLfloat angle;
    	int pen;
    }turtle;//how do yo set default values
    
    void init(GLint x, GLint y, GLfloat theta);
    void right(GLfloat theta);
    void left(GLfloat theta);
    void forward(GLfloat distance);
    void pen(status s);
    #endif

    here's turtle.cpp

     

    #include "turtle.h"
    
    //how do i ensure this is called to initialize the struct
    void init(GLint x, GLint y, GLfloat theta){
    	turtle.angle = theta;
    	turtle.pos[0] = x;
    	turtle.pos[1] = y;
    }
    void right(GLfloat theta){
    	turtle.angle += theta;
    }
    void left(GLfloat theta){
    	turtle.angle -= theta;
    }
    void forward(GLfloat distance){
    	GLfloat old[2], direc[2];
    	//store initial turtle position
    	old[0] = turtle.pos[0];
    	old[1] = turtle.pos[1];
    
    	//init directional vector
    	direc[0] = cos(turtle.pos[0]);
    	direc[1] = cos(turtle.pos[1]);
    	
    	//update position
    	for(int i = 0; i < 2; i++){
    		turtle.pos[i] = distance * direc[i];
    	}
    	//if pen down draw line
    	if(turtle.pen == DOWN){
    		glBegin(GL_LINES);
    			glVertex2fv(old);
    			glVertex2fv(turtle.pos);
    		glEnd();
    	}
    }
    void pen(status s){
    	turtle.pen = s;
    }

     

    and here's the code that references the static turtle library:

     

    // turtleRef.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "turtle.h"
    
    void myInit(){
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	gluOrtho2D(-2.0, 2.0, -2.0, 2.0);
    	glClearColor(1.0, 1.0, 1.0, 1.0);
    	glColor3f(0.0, 0.0, 0.0);
    	//initializing turtle position
    	init(0.0, 0.0, 45.0);
    }
    
    void display(){
    	//draw line across screen
    	turtle.pen = DOWN;
    	forward(2 * sqrt(2.0));
    }
    
    int main(int argc, char **argv){
    	glutInit(&argc, argv);
    	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    	glutInitWindowSize(500,500);
    	glutInitWindowPosition(0,0);
    	glutDisplayFunc(display);
    	myInit();
    	glutMainLoop();
    }
    
    I would really appreciate any help :)

     

    Thursday, October 6, 2011 3:47 AM

Answers

  • >How would you go about fixing this error?
    >How would I reference the Turtle struct in
    >the the library from outside the library
    >without creating a copy?

    Generally by making the functions members of the struct
    (or better a class) and declaring an instance of the
    Turtle struct (or class) wherever it's wanted. There
    should be *no* objects instantiated within the struct/class
    header or definition file.

    >Sorry but I'm a noob.

    I get the impression that you're not very familiar with
    the concepts of encapsulation and instantiation. You
    really should brush up on these fundamental C++ concepts.

    - Wayne
    • Marked as answer by rob05cl Friday, October 7, 2011 6:36 PM
    Thursday, October 6, 2011 5:34 PM

All replies

  • >I'm getting the following errors when I try to compile my code
    >Error 1  error LNK2001: unresolved external symbol
    >"struct Turtle turtle" ... turtleRef.obj
    >Error 2  error LNK2001: unresolved external symbol
    >"struct Turtle turtle" ... TurtleDraw.lib(turtle.obj)

    First a note about terminology: You're not getting errors when
    you try to compile the code - it apparently has compiled without
    errors. You are getting *link* errors after the compilation(s)
    has/have finished. This brings me to the first issue that I
    have with what you posted:

    Your header is *defining* an object of type Turtle named
    "turtle". This means that every .cpp source module that
    includes that header will have it's own instance of a
    Turtle struct named "turtle". From what you posted, that
    means that *both* the Turtle implementation in turtle.cpp
    *and* the code that is using the Turtle struct - turtleRef.cpp
    - will have their own copies. These should *compile* OK but
    if/when you try to link both the turtle.obj and turtleRef.obj
    together you should get a LNK error saying that struct Turtle
    turtle is multiply defined. But that's *not* the LNK error
    you've posted. It says that the linker can't find *any*
    object of type struct Turtle named turtle. So what have
    you done differently?

    - Wayne
    Thursday, October 6, 2011 5:40 AM
  • Hi Wayne,

      Thanks for replying so soon. I'm not sure what I did, but when I tried building a solution again I got the errors you mentioned:

    Error 1 error LNK2005: "struct Turtle turtle" (?turtle@@3UTurtle@@A) already defined in turtleRef.obj C:\Users\Documents\Visual Studio 2010\Projects\TurtleDraw\turtleRef\TurtleDraw.lib(turtle.obj) turtleRef

    Error 2 error LNK1169: one or more multiply defined symbols found C:\Users\Documents\Visual Studio 2010\Projects\TurtleDraw\Debug\turtleRef.exe 1 1 turtleRef

    How would you go about fixing this error? How would I reference the Turtle struct in the the library from outside the library without creating a copy?
    Sorry but I'm a noob.
    Thanks,
     - Robin

    Thursday, October 6, 2011 4:02 PM
  • >How would you go about fixing this error?
    >How would I reference the Turtle struct in
    >the the library from outside the library
    >without creating a copy?

    Generally by making the functions members of the struct
    (or better a class) and declaring an instance of the
    Turtle struct (or class) wherever it's wanted. There
    should be *no* objects instantiated within the struct/class
    header or definition file.

    >Sorry but I'm a noob.

    I get the impression that you're not very familiar with
    the concepts of encapsulation and instantiation. You
    really should brush up on these fundamental C++ concepts.

    - Wayne
    • Marked as answer by rob05cl Friday, October 7, 2011 6:36 PM
    Thursday, October 6, 2011 5:34 PM
  • Yeah your right. Thanks Wayne
    Friday, October 7, 2011 6:36 PM