C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem with writing my own header file...

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
WW
Guest





PostPosted: Sat Sep 27, 2003 10:20 am    Post subject: Re: [OT]Problem with writing my own header file... Reply with quote



Markus wrote:
Quote:
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

using namesapce std;

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

func1.cpp:
#include

using namesapce std;

Quote:
#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++.

--
WW aka Attila



Back to top
Markus
Guest





PostPosted: Sat Sep 27, 2003 10:22 am    Post subject: Problem with writing my own header file... Reply with quote



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



Back to top
Agent Mulder
Guest





PostPosted: Sat Sep 27, 2003 8:21 pm    Post subject: Re: Problem with writing my own header file... Reply with quote



<Markus>
Quote:
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. Sad 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 f;
Second s;
Third(){cout<<"well-well";}
};

//file main.cpp
#include #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





Back to top
Frank Schmitt
Guest





PostPosted: Mon Sep 29, 2003 7:05 am    Post subject: Re: Problem with writing my own header file... Reply with quote

"Markus" <Markus.Eckelt (AT) nospam (DOT) de> writes:

Quote:
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 #include "func1.h" //so it looks in the same dir, right?
int main(){
int a=42;
func1(a); }

func1.cpp:
#include #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

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com

Back to top
Frank Schmitt
Guest





PostPosted: Tue Sep 30, 2003 12:03 pm    Post subject: Re: Problem with writing my own header file... Reply with quote

"Markus" <Markus.Eckelt (AT) nospam (DOT) de> writes:

Quote:
"Frank Schmitt" <invalid (AT) seesignature (DOT) info> wrote in message
news:4cwubsgjmr.fsf (AT) scxw21 (DOT) 4sc...
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!

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

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com

Back to top
Markus
Guest





PostPosted: Tue Sep 30, 2003 12:07 pm    Post subject: Re: Problem with writing my own header file... Reply with quote

"Agent Mulder" <mbmulder_remove_this_ (AT) home (DOT) nl> wrote

Quote:
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 f;
Second s;
Third(){cout<<"well-well";}
};

//file main.cpp
#include #include #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?



Back to top
Markus
Guest





PostPosted: Tue Sep 30, 2003 12:17 pm    Post subject: Re: Problem with writing my own header file... Reply with quote

"Frank Schmitt" <invalid (AT) seesignature (DOT) info> wrote

Quote:
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!



Back to top
Markus
Guest





PostPosted: Tue Sep 30, 2003 2:38 pm    Post subject: Re: Problem with writing my own header file... Reply with quote

"Frank Schmitt" <invalid (AT) seesignature (DOT) info> wrote

Quote:
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. ;-)



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.