Problem with writing my own header file...

W

WW

Markus said:
Hi,
Well, I'm still a newbie to C++, so excuse my maybe (probably???)
stupid question...btw, I use gcc 2.95.2.

Here are my (test) funtions:

mainfunc.cpp:
#include <iostream>

using namesapce std;
#include "func1.h" //so it looks in the same dir, right?
int main(){
int a=42;
func1(a); }

func1.cpp:
#include <iostream>

using namesapce std;
#include "func1.h" /* I read you should also include it here... */
void func1(int i) {
cout << i << endl; }

func1.h:
void func1(int i);

My Problem is that when I try to compile it, it gives me an "undefined
reference to 'func1(int)' error and I don't know how to get rid of
it. I tried to put the files everywhere, like in the "include"-folder
or others folders that I set with path.
What is my mistake??? Thank you in advance, I really appreciate it!

You need to LINK func1.o into your executable! Please get a gcc/g++ manual
and read about it. It is _using_ your compiler and not about the language
so - strictly speaking - it is off topic here. Byt I am pretty sure that a
million tutorials exist out there for gcc/g++.
 
M

Markus

Hi,
Well, I'm still a newbie to C++, so excuse my maybe (probably???) stupid
question...btw, I use gcc 2.95.2.

Here are my (test) funtions:

mainfunc.cpp:
#include <iostream>
#include "func1.h" //so it looks in the same dir, right?
int main(){
int a=42;
func1(a); }

func1.cpp:
#include <iostream>
#include "func1.h" /* I read you should also include it here... */
void func1(int i) {
cout << i << endl; }

func1.h:
void func1(int i);

My Problem is that when I try to compile it, it gives me an "undefined
reference to 'func1(int)' error and I don't know how to get rid of it. I
tried to put the files everywhere, like in the "include"-folder or others
folders that I set with path.
What is my mistake??? Thank you in advance, I really appreciate it!

Regards
Markus
 
A

Agent Mulder

My Problem is that when I try to compile it, it gives me an "undefined
reference to 'func1(int)' error and I don't know how to get rid of it. I
tried to put the files everywhere, like in the "include"-folder or others
folders that I set with path.
What is my mistake??? Thank you in advance, I really appreciate it!
</>

JT> I really don't want to define all the hundreds of error message strings
in
JT> every .cpp file. :-( What do i wrong?
JT>
JT> Any hint appreciated.

You can use this very simple scheme if you like:

//file First.cpp
class First{public:First(){cout<<"phew";};

//file Second.cpp
class Second:public First{public:Second():First(){cout<<"gosh";}};

//file Third.cpp
class Third
{
public:
vector<First>vf;
First f;
Second s;
Third(){cout<<"well-well";}
};

//file main.cpp
#include <iostream.h>
#include <vector.h>
#include "First.cpp"
#include "Second.cpp"
#include "Third.cpp"

int main(int argc,char**argv)
{
First f;
Second s;
Third t;
return 0;
}

All the includes are summarized in file main.cpp. The only
file that gets compiled is main.cpp and consequently all
other cpp files. What actually happens is that the precompiler
expands all the included files into main.cpp and then that expanded
file is compiled. So no complicated makefiles and no double
includes, at the cost of longer compilation time. Stale .obj files
can be a serious source of bugs. With this approach you are
sure to always work on your most recent version.

-X
 
F

Frank Schmitt

Markus said:
Hi,
Well, I'm still a newbie to C++, so excuse my maybe (probably???) stupid
question...btw, I use gcc 2.95.2.

Here are my (test) funtions:

mainfunc.cpp:
#include <iostream>
#include "func1.h" //so it looks in the same dir, right?
int main(){
int a=42;
func1(a); }

func1.cpp:
#include <iostream>
#include "func1.h" /* I read you should also include it here... */
void func1(int i) {
cout << i << endl; }

func1.h:
void func1(int i);

My Problem is that when I try to compile it, it gives me an "undefined
reference to 'func1(int)' error and I don't know how to get rid of it.

Undefined reference means that the *linker* could not find your function -
you have to link in every source file in your project.
compile it with

g++ -Wall -ansi -pedantic mainfunc.cpp func1.cpp

and it should work.

HTH & kind regards
frank
 
F

Frank Schmitt

M

Markus

Agent Mulder said:
You can use this very simple scheme if you like:

//file First.cpp
class First{public:First(){cout<<"phew";};

//file Second.cpp
class Second:public First{public:Second():First(){cout<<"gosh";}};

//file Third.cpp
class Third
{
public:
vector<First>vf;
First f;
Second s;
Third(){cout<<"well-well";}
};

//file main.cpp
#include <iostream.h>
#include <vector.h>
#include "First.cpp"
#include "Second.cpp"
#include "Third.cpp"

int main(int argc,char**argv)
{
First f;
Second s;
Third t;
return 0;
}

All the includes are summarized in file main.cpp. The only
file that gets compiled is main.cpp and consequently all
other cpp files. What actually happens is that the precompiler
expands all the included files into main.cpp and then that expanded
file is compiled. So no complicated makefiles and no double
includes, at the cost of longer compilation time. Stale .obj files
can be a serious source of bugs. With this approach you are
sure to always work on your most recent version.

-X

Thank you! Yes, your solution does work, even though it is not the approach
I was using. This way I don't need the header file at all, but the way I see
it (from my newbie point of view), I can't pass on values that easily,
right?
 
M

Markus

Frank Schmitt said:
Undefined reference means that the *linker* could not find your function -
you have to link in every source file in your project.
compile it with

g++ -Wall -ansi -pedantic mainfunc.cpp func1.cpp

and it should work.

HTH & kind regards
frank

It compiled alright but did not create an executable??? Usually I use
Textpad, so I don't really know the command settings. WW said there should
be millions of tutorials about that, couldn't find one so far but I'll keep
looking. Thanks!
 
M

Markus

Frank Schmitt said:
I find this hard to believe - searching for "gcc tutorial" on google turns up
zillions of links, among them

http://users.actcom.co.il/~choo/lupg/tutorials/c-on-unix/c-on-unix.html#running_the_exe

regards
frank

Oh, no - you're absolutly right! There are plenty of tutorials out there,
what I meant was a tutorial for gcc in combination with textpad. I got it as
far as compiling and all, but I don't know how to do it with including other
funtions...

The class solutions works, but I'd believe there should be an easier way.
But on the other hand, I don't have a clue, so I'm probably wrong anyways.

Thank you again for the reply, though. ;-)
 

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,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top