C++ problem - unresolved externals

evaD

Member
Jul 26, 2005
54
0
0
I'm writing a program, with header files and .cpp files.

However, when I call a function that is declared in the header
and defined in the .cpp file, I get a linker error: unresolved external

I can "fix" the problem by putting all the code for the functions in the .h
file. Then, the linker sees the function definition, and links OK.

But, I want to keep the interface and the implementation separate.

i.e. - I want to put the function definitions inside the .cpp file, but without the
unresolved extenal error. I'm using a Borland 5.5 command-line compiler.

Any clues?
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
that's probably because you didn't link your implementation .cpp with the client .cpp. try compiling them together, or first compile the implementation .cpp into an object file, then pass it to the compiler while you are compiling the main code

or just get some kind of IDE to save yourself the headache
 
Sep 29, 2004
18,656
67
91
An easy workaround is to put and include X.cpp statement at the bottom of hte header. That's a hack though.

Or look up the extern statement. Another hack as far as I'm concerned.

good luck.
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
Originally posted by: IHateMyJob2004
An easy workaround is to put and include X.cpp statement at the bottom of hte header. That's a hack though.

Or look up the extern statement. Another hack as far as I'm concerned.

good luck.

don't do these workarounds. the unresolved externals are probably coming up because your linking step isn't linking the object files of all of the pieces at once.
 

itachi

Senior member
Aug 17, 2004
390
0
0
Originally posted by: oog
Originally posted by: IHateMyJob2004
An easy workaround is to put and include X.cpp statement at the bottom of hte header. That's a hack though.

Or look up the extern statement. Another hack as far as I'm concerned.

good luck.

don't do these workarounds. the unresolved externals are probably coming up because your linking step isn't linking the object files of all of the pieces at once.
that's exactly what it is.

including the cpp file at the bottom of the header for non-templated features is a horrible idea. the references that the linker is looking for are static.. if he's getting an "undefined reference" error then it's cos the linker can't find the symbols defined in any of the object files.
there is no reason to lookup the extern statements.. it won't tell him where the symbols are.. it'll just let him know that the linker expects it to be defined in one of the object files.
 

evaD

Member
Jul 26, 2005
54
0
0
Ok - I got Borland's Sc1 editor - this is supposed to take care of compiling and linking together with the "Build" command that runs a make file.

Here's some code:

 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Do you actually have the source file added to the project or have you just compiled the file independently?

It seems as if the main has been complied but the linker does not know about the external object.
 

homercles337

Diamond Member
Dec 29, 2004
6,340
3
71
Originally posted by: oog
Originally posted by: IHateMyJob2004
An easy workaround is to put and include X.cpp statement at the bottom of hte header. That's a hack though.

Or look up the extern statement. Another hack as far as I'm concerned.

good luck.

don't do these workarounds. the unresolved externals are probably coming up because your linking step isn't linking the object files of all of the pieces at once.

Can you explain why using EXTERN is a bad idea in this case, or any case for that matter? Its what i do, but im a hack. ;)
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
extern is normally used to declare that you want to use a global variable that is defined in a different file. if you don't have the extern keyword, then the compiler is going to think that you want a second global variable of the same name, and it will probably complain about the name conflict.
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
Originally posted by: evaD
Ok - I got Borland's Sc1 editor - this is supposed to take care of compiling and linking together with the "Build" command that runs a make file.

Here's some code:

The code itself is probably fine. I haven't really looked at it, but if it compiles, then there probably isn't a problem with it. I'm not familiar with Borland's linker. Do you know what command it is using to link the various obj files together? It should be a single command that specifies Test.obj and SomeObj.obj.
 

evaD

Member
Jul 26, 2005
54
0
0
I don't know the specific command to link. I have all the files in the same directory, and just use

bcc32 test.cpp

which should compile and link them all at once. The object files are produced, but no .exe file.
So, it compiles, but doesn't link. I've tried it with the IDE as well, just using the "Build" command,
but the same thing happens - compiles, but doesn't link.
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
sorry. i'm not familiar with borland's compiler. i would guess that bcc32 test.cpp would either just produce test.obj, or it may possibly try to create a test.exe out of the only file you gave it: test.cpp. since you've defined additional code in SomeObj.cpp, you need to somehow supply that as well so that it can be linked with test.obj. with some compilers, you can do this by specifying multiple *.cpp files on the command you use for compiling. it would be something like: bcc32 test.cpp SomeObj.cpp. but again, i've never used this particular compiler.