views:

787

answers:

5

Hi, I'm trying to get this:

//C.h
#ifndef C_H
#define C_H
#include "c.h"

class C
{
    public:
     C();
     int function(int, int);
};
#endif

which is defined in this:

//c.cpp
#include "c.h"
C::C()
{
}
int C::function(int a, int b)
{
    return a * b;
}

to work in this:

//crp.cpp
#include <iostream>
#include "c.h"

void main(void)
{
    C a;
    std::cout << a.function(1, 2);
}

but I get two errors

Error: Unresolved external 'C::C()' referenced from C:\C++\CRP.OBJ

Error: Unresolved external 'C::function(int, int)' referenced from C:\C++\CRP.OBJ

I'm really stuck. Help v. much appreciated!

EDIT:

Thank you for your replies,

I'm using Borland C++ 5.5.1 for Win32, via the command line, I'm not actually sure what a linker is, this is the first time I've tried doing this.

A: 

Which compiler / development environment are you using? Is this from the command line or an IDE?

You need to make sure you compile c.cpp and tell the linker to include c.obj together with crp.obj to create your executable.

Rob Walker
A: 

Hmm...which compiler/linker do you use? Arguments provided to linker? Are the files part of the same project?

badbadboy
+1  A: 

When you link your program you need to link both the c.obj file and the crp.obj file. The error you are seeing is because only the crp.obj file is being used for the link.

What compiler are you using? If you are using something like VisualStudio then if both c.cpp and crp.cpp are in the same project it should work. If you are using the command line to build then you need to link both files, eg

gcc -o crp.exe c.cpp crp.cpp

for gcc

David Dibben
+3  A: 

It looks like your link phase is trying to create an executable from just crp.obj, NOT crp.obj and c.obj.

How are you compling it? It should be something like (in the case of Borland, as mentioned in edit):

bcc32 -ecrp.exe crp.cpp c.cpp

You also don't need the include line within c.h, the only thing stopping an infinite include loop there is the include guard.

paxdiablo
A: 

Thank you for your replies,

I'm using Borland C++ 5.5.1 for Win32, via the command line, I'm not actually sure what a linker is, this is the first time I've tried doing this.

EDIT:

** This is what I was trying before in the windows Command Prompt:

C:\c++>bcc32 crp

**And this was the result:

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland

crp.cpp:

Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

Error: Unresolved external 'C::C()' referenced from C:\C++\CRP.OBJ

Error: Unresolved external 'C::function(int, int)' referenced from C:\C++\CRP.OBJ

** Then I tried this:

bcc32 -ecrp.exe crp.cpp c.cpp

**as Pax Diablo suggested, and got this :

C:\c++>bcc32 -ecrp.exe crp.cpp c.cpp

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland

crp.cpp:

c.cpp:

Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

**but on trying to compile crp.cpp once more, I got the same error as before:

C:\c++>bcc32 crp

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland

crp.cpp:

Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

Error: Unresolved external 'C::C()' referenced from C:\C++\CRP.OBJ

Error: Unresolved external 'C::function(int, int)' referenced from C:\C++\CRP.OBJ

EDIT: Oops! Trying to compile it the wrong way again was pretty silly of me. It works now though. So the solution is, as Pax Diablo orginally suggested, to compile crp.cpp like this:

    bcc32 -ecrp.exe crp.cpp c.cpp

Problem solved, thanks to everyone who replied!

Show us the command line you're using. In addition, you should be editing your original question to add this sort of info, since it's not actually an answer (forgiven since you're new here :-).
paxdiablo
"**but on trying to compile crp.cpp once more ..." Why did you try to repeat the command line that you knew wouldn't work? Since you're on Linux, simply drop the ".exe" from "-ecrp.exe" and call it a day.
Max Lybbert
So steps (1) do it wrong, (2) do it right (as I suggested), (3) do it wrong again - didn't work out for you ? Just kidding :-). After you do "bcc32 -ecrp.exe crp.cpp c.cpp", you're finished, both crp.cpp and c.cpp have been used to create crp.exe - just run crp.exe at that point.
paxdiablo
@me.yahoo - not linux, win32.
paxdiablo