linker error

V

Victor Bazarov

Baloff said:
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.

It's not enough to have files in the same directory. You need to feed
all C++ files to the compiler so that they are processed into "object"
files and then feed all "object" files to the linker to produce the
final executable (and sometimes it can be done in the same command).

As to how to do that, look in your compiler manual, it falls outside
the topic of this newsgroup.

V
 
J

Jonathan Mcdougall

Baloff said:
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:prototypes.h
void f(int);
///:~

//: S03:prototypes.cpp {O}
// Implements functions declared in Prototypes.h
#include <iostream>
using namespace std;

void f(int i) {
cout << "f(" << i << ") returning void\n";
}
///:~

//: S03:protoTest.cpp
//{L} Prototypes
#include <iostream>
#include "Prototypes.h"

int main() {
f(1);

getchar();
return 0;
}

There's no problem with the code. Check your setup, prototypes.cpp is
probably not included in your project. For further questions concerning
the environment (as opposed to the language itself), please post in an
appropriate newsgroup.


Jonathan
 
M

Manfred

Baloff said:
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:prototypes.h
void f(int);
///:~

//: S03:prototypes.cpp {O}
// Implements functions declared in Prototypes.h
#include <iostream>
using namespace std;

void f(int i) {
cout << "f(" << i << ") returning void\n";
}
///:~

//: S03:protoTest.cpp
//{L} Prototypes
#include <iostream>
#include "Prototypes.h"

int main() {
f(1);

getchar();
return 0;
}

This Code works for me.
When compiling on the comandline you should call something like
'g++ Prototypes.cpp ProtoTest.cpp' or adjust your projectsetup.

best regards

Manfred
 
K

Ken Wilson

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:prototypes.h
void f(int);
///:~

//: S03:prototypes.cpp {O}

In the code below you need to also include your header
so this file knows where to find the function.
// Implements functions declared in Prototypes.h
#include <iostream>

#include "Prototypes.h"
using namespace std;

void f(int i) {
cout << "f(" << i << ") returning void\n";
}
///:~

//: S03:protoTest.cpp
//{L} Prototypes
#include <iostream>
#include "Prototypes.h"

int main() {
f(1);

getchar();
return 0;
}

Ken Wilson
"Coding, coding, over the bounding main()"
 
J

Jonathan Mcdougall

Ken said:
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:prototypes.h
void f(int);
///:~

//: S03:prototypes.cpp {O}

In the code below you need to also include your header
so this file knows where to find the function.

No. The "file" does not need to have a forward declaration for f(). You
need a forward declaration when you want to use a name which is not
declared. In this file, you are not using any undeclared name, you are
just defining a function.
#include "Prototypes.h"


Jonathan
 
K

Ken Wilson

Baloff said:
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:prototypes.h
void f(int);
///:~

//: S03:prototypes.cpp {O}
// Implements functions declared in Prototypes.h
#include <iostream>
using namespace std;

void f(int i) {
cout << "f(" << i << ") returning void\n";
}
///:~

//: S03:protoTest.cpp
//{L} Prototypes
#include <iostream>
#include "Prototypes.h"

int main() {
f(1);

getchar();
return 0;
}

This Code works for me.
When compiling on the comandline you should call something like
'g++ Prototypes.cpp ProtoTest.cpp' or adjust your projectsetup.

best regards

Manfred

He's using Visual Studio. It doesn't work quite that way, at least
not if your new to it. I ran his code on my Visual C++ here and it
was missing the include for the Prototypes header file in the
Prototypes.cpp file. I am assuming he's just running the default
compile which automatically does the whole project, no separate
compilation. It compiles alright. Of course it should. It just won't
link because the .cpp doesn't know there's an .h file with the rest of
the answer.

Ken Wilson
"Coding, coding, over the bounding main()"
 
J

Jonathan Mcdougall

Ken said:
Baloff said:
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:prototypes.h
void f(int);
///:~

//: S03:prototypes.cpp {O}
// Implements functions declared in Prototypes.h
#include <iostream>
using namespace std;

void f(int i) {
cout << "f(" << i << ") returning void\n";
}
///:~

//: S03:protoTest.cpp
//{L} Prototypes
#include <iostream>
#include "Prototypes.h"

int main() {
f(1);

getchar();
return 0;
}

This Code works for me.
When compiling on the comandline you should call something like
'g++ Prototypes.cpp ProtoTest.cpp' or adjust your projectsetup.

best regards

Manfred

He's using Visual Studio.

No, he's using Dev-C++ 4.9.9.2.
It doesn't work quite that way, at least
not if your new to it. I ran his code on my Visual C++ here and it
was missing the include for the Prototypes header file in the
Prototypes.cpp file.

How did you reach that conclusion?
I am assuming he's just running the default
compile which automatically does the whole project, no separate
compilation. It compiles alright. Of course it should. It just won't
link because the .cpp doesn't know there's an .h file with the rest of
the answer.

That's plain wrong. Read my other post.


Jonathan
 
K

Ken Wilson

Ken said:
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:prototypes.h
void f(int);
///:~

//: S03:prototypes.cpp {O}
// Implements functions declared in Prototypes.h
#include <iostream>
using namespace std;

void f(int i) {
cout << "f(" << i << ") returning void\n";
}
///:~

//: S03:protoTest.cpp
//{L} Prototypes
#include <iostream>
#include "Prototypes.h"

int main() {
f(1);

getchar();
return 0;
}

This Code works for me.
When compiling on the comandline you should call something like
'g++ Prototypes.cpp ProtoTest.cpp' or adjust your projectsetup.

best regards

Manfred

He's using Visual Studio.

No, he's using Dev-C++ 4.9.9.2.
It doesn't work quite that way, at least
not if your new to it. I ran his code on my Visual C++ here and it
was missing the include for the Prototypes header file in the
Prototypes.cpp file.

How did you reach that conclusion?

Because I physically recreated his code from those files he posted and
the three wouldn't compile until I told Prototypes.cpp where to find
the definition for the function. That definition is in the .h file.

And yes. They will *compile* separately. I tried that. But they
won't link. That tells me something couldn't find what it was looking
for. And the error told me exactly which function to trace. That
header file is what glues the two cpp files together so they both need
to know where it is.
That's plain wrong. Read my other post.

I did.

Ken Wilson
"Coding, coding, over the bounding main()"
 
B

Baloff

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:prototypes.h
void f(int);
///:~

//: S03:prototypes.cpp {O}
// Implements functions declared in Prototypes.h
#include <iostream>
using namespace std;

void f(int i) {
cout << "f(" << i << ") returning void\n";
}
///:~

//: S03:protoTest.cpp
//{L} Prototypes
#include <iostream>
#include "Prototypes.h"

int main() {
f(1);

getchar();
return 0;
}
 
X

Xie Yubo

Baloff said:
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:prototypes.h
void f(int);
///:~

//: S03:prototypes.cpp {O}
// Implements functions declared in Prototypes.h
#include <iostream>
using namespace std;

void f(int i) {
cout << "f(" << i << ") returning void\n";
}
///:~

//: S03:protoTest.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~~ :)

--
Best Regards

Xie Yubo
Email:[email protected] Website: http://xieyubo.cn/
Harbin Institute of Technology
Phone: 86-451-86416614 Fax: 86-451-86413309
 
K

Ken Wilson

Baloff said:
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:prototypes.h
void f(int);
///:~

//: S03:prototypes.cpp {O}
// Implements functions declared in Prototypes.h
#include <iostream>
using namespace std;

void f(int i) {
cout << "f(" << i << ") returning void\n";
}
///:~

//: S03:protoTest.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()"
 
X

Xie Yubo

Ken said:
Baloff said:
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:prototypes.h
void f(int);
///:~

//: S03:prototypes.cpp {O}
// Implements functions declared in Prototypes.h
#include <iostream>
using namespace std;

void f(int i) {
cout << "f(" << i << ") returning void\n";
}
///:~

//: S03:protoTest.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()"
I think you should know that there is no relation between the name of .h
and .cpp. Although people like make the same name of .h and .cpp if the
..h file contains the declaration of functions which are implemented in
..cpp. But it's not a must! In another word, you can declare the
functions in A.h and implement them in B.cpp, it doesn't matter.

The .h would be included in some .cpp. It only tells the compiler what
some identified words are. It doesn't tell the compiler or linker where
is the implementation.

If you use:
g++ A.cpp B.cpp -o A.exe
it may be completed as follow steps:
1. g++ -c A.cpp A.o ("-c" means: only compile not link)
2. g++ -c B.cpp B.o
3. ld A.o B.o -o A.exe

You can see that only the last step is to link. The linker will search
all the files you specified in the command-line to find where is the
implementation of functions. So the linker doesn't use .h file.


--
Best Regards

Xie Yubo
Email: (e-mail address removed) Website: http://xieyubo.cn/
Harbin Institute of Technology
Phone: 86-451-86416614 Fax: 86-451-86413309
 
K

Ken Wilson

Ken said:
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:prototypes.h
void f(int);
///:~

//: S03:prototypes.cpp {O}
// Implements functions declared in Prototypes.h
#include <iostream>
using namespace std;

void f(int i) {
cout << "f(" << i << ") returning void\n";
}
///:~

//: S03:protoTest.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()"
I think you should know that there is no relation between the name of .h
and .cpp. Although people like make the same name of .h and .cpp if the
.h file contains the declaration of functions which are implemented in
.cpp. But it's not a must! In another word, you can declare the
functions in A.h and implement them in B.cpp, it doesn't matter.

The .h would be included in some .cpp. It only tells the compiler what
some identified words are. It doesn't tell the compiler or linker where
is the implementation.

If you use:
g++ A.cpp B.cpp -o A.exe
it may be completed as follow steps:
1. g++ -c A.cpp A.o ("-c" means: only compile not link)
2. g++ -c B.cpp B.o
3. ld A.o B.o -o A.exe

You can see that only the last step is to link. The linker will search
all the files you specified in the command-line to find where is the
implementation of functions. So the linker doesn't use .h file.

I didn't ask what happens under g++. As I said, I ran the code with
both compilers and saw the results. It's not really whether something
is or isn't done here that bothers me at this point, it's the
inconsistency between the two tools that are essentially both supposed
to be doing exactly the same job.

What if you're trying to write portable code with thousands of files
missing header include statements and you bring that code over to my
Windows box? Things are going to break left, right and sideways and
be a long drawn out process to correct. I'm not seeking to apportion
blame on the guys who write these compilation I'm just trying to get a
clearer picture of what is standard and what is up to the compiler
vendor to implement.


Ken Wilson
"Coding, coding, over the bounding main()"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top