C ++ Problem: "Error external" C :: C () 'referenced by C: \ C ++ \ CRP.OBJ "

I am trying to do 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 am getting two errors

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

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

I am really stuck. Help v. Much appreciated!

EDIT:

Thank you for your responses,

I am using Borland C ++ 5.5.1 for Win32, via command line, I am not really sure what the linker is, this is the first time I tried it.

+1


source to share


5 answers


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

How do you borrow it? It should be something like (in the case of Borland, as mentioned in the editorial):



bcc32 -ecrp.exe crp.cpp c.cpp

      

You also don't need the include line inside ch, the only thing that stops the infinite include loop is the include protection.

+3


source


When you link your program, you need to link the c.obj file and the crp.obj file. The error you are seeing is that the link only uses the crp.obj file.

Which 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 command line to build you need to link both files, for example



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

for gcc

+1


source


What compiler / development environment are you using? Is it from the command line or the IDE?

You need to make sure you compile c.ppp and tell the linker to include c.obj along with crp.obj in order to create an executable.

0


source


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

0


source


Thank you for your responses,

I am using Borland C ++ 5.5.1 for Win32, via command line, I am not really sure what the linker is, this is the first time I tried it.

EDIT:

** This is what I tried before on the Windows command line:

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 the following:

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 when trying to compile crp.cpp again, 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: Sorry! Trying to compile it the wrong way was pretty silly with me. It works now. So the solution, as originally intended by Pax Diablo, was to compile crp.cpp like this:

    bcc32 -ecrp.exe crp.cpp c.cpp

      

Problem solved, thanks to everyone who answered!

0


source







All Articles