Baloff wrote:
Hello
I have this linker error which makes me think that the definition file
is not being seen by the linker, this code is taken from "Thinking in
C++, 2nd Edition, Volume 1, Annotated Solutions Guide"
Using Dev-C++ 4.9.9.2, The error is:
[Linker error] undefined reference to ‘f(int)’
all the following files are in the same dir.
thanks
//: S03
rototypes.h
void f(int);
///:~
//: S03
rototypes.cpp {O}
// Implements functions declared in Prototypes.h
#include <iostream>
using namespace std;
void f(int i) {
cout << "f(" << i << ") returning void\n";
}
///:~
//: S03
rotoTest.cpp
//{L} Prototypes
#include <iostream>
#include "Prototypes.h"
int main() {
f(1);
getchar();
return 0;
}
I think, maybe, you cann't add the file "Prototypes.cpp" to the project.
So the devcpp do it like this:
g++ ProtoTest.cpp -o ProtoTest.exe,
In fact, it should be:
g++ ProtoTest.cpp Prototypes.cpp -o ProtoTest.exe
So, you can add the "Prototypes.cpp" to the project, and try it again.
Good luck~~
I have to admit I am getting a bit confused. I figured that many
people can't be on the wrong track so I moved the code to my Red Hat
box and the Linux people are correct in that environment. It will
compile and link to an executable without an include for Prototypes.h
in the Prototypes.cpp. However, the exact same code will not compile
and link on a Windows box with Visual Studio but generates the linker
error that initially started this thread.
I would venture a guess, and it is a guess, that one of these
compilers is not acting in proper fashion. Which compiler is
following the standard on this? Does anyone know what the standard
says about this?
What bothers me is every text I've ever read tells me when I have a
separate .h and .cpp that if I want into the contents of the .h from
my .cpp I had better put an include in. Or is g++ a little more lax
on this and will automatically make the connection between a .cpp and
a .h file with the same name? If so, what happens to my code if I
change the name of the .cpp file but not the .h file because the .h is
referenced in too many other files?
Thanks for your patience.
Ken Wilson
"Coding, coding, over the bounding main()"