linking problem

This is a discussion on linking problem within the C++ Programming forums, part of the General Programming Boards category; I am getting the following errors when trying to compile my code: DLLMain.obj : error LNK2001: unresolved external symbol "class ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Location
    Kingston, Ontario
    Posts
    12

    linking problem

    I am getting the following errors when trying to compile my code:

    DLLMain.obj : error LNK2001: unresolved external symbol "class BMLClient netClient" (?netClient@@3VBMLClient@@A)
    Desktop\Walker26.dlc : fatal error LNK1120: 1 unresolved externals

    the line that is causing it is :
    Code:
    int az = netClient.GetAzimuth();
    netClient is declared as
    Code:
    extern BMLClient netClient;
    in the same file

    the original declaration is in another header file BMLDrawable.h:
    Code:
    class BMLDrawable: public osg::Drawable {
    public:
    	BMLDrawable();
    	virtual ~BMLDrawable();
    
    	virtual osg::Object* cloneType() const
    	{
    		return new BMLDrawable();
    	}
    
    	virtual osg::Object* clone(const osg::CopyOp&) const
    	{
    		return new BMLDrawable();
    	}
    
    	bool computeBound() const;
    	virtual void drawImplementation(osg::State& state) const;
    	
    	
    protected:
    	BMLClient netClient;
    };
    GetAzimuth is declared as
    Code:
     int GetAzimuth(void) { return m_nAzimuth; }
    in the BMLClient class

    is anyone familiar with what this error means?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you declare it as extern in a header file, you must define it in a source file that is compiled and linked into your project. It is the same syntax without the extern (unless you want to add constructor arguments).

  3. #3
    Registered User
    Join Date
    Jul 2006
    Location
    Kingston, Ontario
    Posts
    12
    I don't have it declared in a header file, it's in my main cpp file...

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If you have an extern variable in a file, then the compiler assumes that you've declared the variable in another file and leaves the linker to find out where. If the linker can't find where you declared the variable, it gives you an error message like the one you encountered.

    You probably declare the variable as extern, but never actually declare it (un-extern-ly).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    It's a little confusing what you are trying to do.

    The extern keyword is simply introducing the name into the scope. However, this name has yet to be defined. No storage was yet allocated to it either. So your error is because of that. When the line int az = netClient.GetAzimuth(); is reached, netClient has not been defined yet.

    The name inside the class has nothing to do with it, even though it has the same name.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You are trying to use the netClient variable that is a member of BMLDrawable? If so, then remove the entire declaration, the netClient variable is already declared as part of that class.

    To use the netClient that is part of BMLDrawable, you have to be inside of a BMLDrawable member function. You posted "the line that is causing it." As long as that line is inside of a member function of BML drawable, you should be fine.

    If you don't want to use the netClient that is part of the BMLDrawable class, then you need to remove the extern to create a global variable. Whether that's the best choice or not depends on the rest of your code, but it should help it compile and link.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with linking files
    By slippy in forum C Programming
    Replies: 2
    Last Post: 11-23-2007, 10:35 PM
  2. Linking problem
    By Agent89 in forum C++ Programming
    Replies: 4
    Last Post: 03-27-2005, 02:03 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. Long file linking problem
    By hypertension in forum C Programming
    Replies: 3
    Last Post: 10-15-2002, 09:55 PM
  5. Linking problem...
    By BrianK in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2002, 04:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21