Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linker problems with multiple files 1

Status
Not open for further replies.

titanandrews

Programmer
Feb 27, 2003
130
US
Hi,
I am having some difficulty on the link step when I try to compile a multiple file program. I really don't understand why the linker complains. Could someone take a look at the following files and see if you know why?

Code:
//Base.h file
#ifndef __Base_H__
#define __Base_H__


class Base
{
public:
    virtual void aFunc();
  
};

#endif

//Base.cpp file
#include "Base.h"

void aFunc()
{
    int i = 0;
}


//Derived.h file
#ifndef __Derived_h__
#define __Derived_h__

#include "Base.h"

class Derived : public Base
{
public:
    virtual void aFunc();
};

#endif


//Derived.cpp file
#include "Derived.h"

void aFunc()
{
    int y = 0;
}


//main.cpp file
#include "Base.h"
#include "Derived.h"

int main(int argc,char *argv[])
{
    Base base;
    Derived deriv;
    return (1);
}

Here is my compile command:
cl /GX /GZ /Zi main.cpp Base.cpp Derived.cpp

If I just put everything in a couple of files, then it works okay, but that's not what I want. Any suggestions?


many thanks,

Barry
 
I forgot to post the link error:
Derived.obj : error LNK2005: "void __cdecl aFunc(void)" (?aFunc@@YAXXZ) already defined in Base.obj
main.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Base::aFunc(void)" (?aFunc@Base@@UAEXXZ)
main.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Derived::aFunc(void)" (?aFunc@Derived@@UAEXXZ)
main.exe : fatal error LNK1120: 2 unresolved externals
 
Change the line

void aFunc()

to
void Base::aFunc()

and also the other aFunc to
void Derived::aFunc()

/JOlesen
 
Good grief!!! Thanks!
Link errors are so cryptic. Why wouldn't these errors be caught in the compile step?




Barry
 
When you have seen these errors a few hundred times, it's like reading Donald Duck ;-)

/JOlesen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top