jwsmpt question on linking

Hello Im taking an into to network class and the professor wants us to link files and make this demo program work....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Note that the only valid version of the GPL as far as jwSMTP
// is concerned is v2 of the license (ie v2, not v2.2 or v3.x or whatever),
// unless explicitly otherwise stated.
//
// This file is part of the jwSMTP library.
//
//  jwSMTP library is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; version 2 of the License.
//
//  jwSMTP library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with jwSMTP library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// jwSMTP library
//   http://johnwiggins.net
//   smtplib@johnwiggins.net
//
// http://www.boost.org
//#include <boost\thread\thread.hpp>

#include <iostream>
#include "jwsmtp/jwsmtp.h"

using std::cout;
using std::cin;
using std::string;

void Usage() {
   cout << "jwSMTP library demo program\n"
           "demo2 <email toaddress> <email fromaddress> <smtpserver>\n"
           "   e.g.\n"
           "      demo2 recipient@there.com me@server.com mail.server.com\n";
}

int main(int argc, char* argv[])
{
   if(argc != 4) {
      Usage();
      return 0;
   }

   cout << "jwSMTP library demo program\n\n";
   string to(argv[1]);
   string from(argv[2]);
   string smtpserver(argv[3]);

   if(to.length() < 2 || from.length() < 2 || smtpserver.length() < 2) {
      Usage();
      return 0;
   }

   char str[2048];
   cout << "Please enter the subject of the mail\n";
   cin.getline(str, 500);	
   string subject(str);
   strcpy(str, "");

   cout << "Please enter the message body end with \".\" on a line by itself\n";
   string mailmessage;
   while(true) {
      cin.getline(str, 2048);
      if(!strcmp(str, "."))
         break;
		
      mailmessage += str;
      mailmessage += "\r\n";
      strcpy(str, "");
   }

   cout << "\nPlease wait sending mail\n";
   // This is how to tell the mailer class that we are using a direct smtp server
   // preventing the code from doing an MX lookup on 'smtpserver' i.e. passing
   // false as the last parameter.
   jwsmtp::mailer mail(to.c_str(), from.c_str(), subject.c_str(), mailmessage.c_str(),
                       smtpserver.c_str(), jwsmtp::mailer::SMTP_PORT, false);

   // using a local file as opposed to a full path.
   mail.attach("attach.png");

   // add another recipient (carbon copy)
   //mail.addrecipient("someoneelse@somewhere.net", mailer::Cc);

   // set a new smtp server! This is the same as setting a nameserver.
   // this depends on the constructor call. i.e. in the constructor
   // If MXLookup is true this is a nameserver
   // If MXLookup is false this is an smtp server
   //mail.setserver("mail.somewherefun.com");
   // same again except using an IP address instead.
   //mail.setserver("192.168.0.1");

   // boost::thread thrd(mail);
   // thrd.join(); // optional
   // or:-

   // Use authentication
   //mail.username("testuser");
   //mail.password("secret");
   // LOGIN authentication by default
   // if you want plain as opposed to login authentication
   //mail.authtype(jwsmtp::mailer::PLAIN);

   // mail.send();
   mail.operator()();
   cout << mail.response() << "\n";

   //mail.reset(); // now we can mail someone else.
   //mail.setmessage("flibbletooting");
   //mail.setsubject("another message same object");
   //mail.attach("/home/user1/image.gif");
   // or a win example
   //mail.attach("C:\\image.gif");
   //mail.addrecipient("someoneelseagain@foobar.net");

   //mail.operator ()();
   //cout << mail.response() << "\n";
   return 0;
}


when i try to compile it it says that all my mailer functions are not defined even though i have my mailer.cpp and mailer.h and such in the same directory...
the exact error is...

1
2
3
4
5
6
7
8
In function `main':
demo2.cpp:(.text+0x45f): undefined reference to `jwsmtp::mailer::mailer(char const*, char const*, char const*, char const*, char const*, unsigned short, bool)'
demo2.cpp:(.text+0x499): undefined reference to `jwsmtp::mailer::attach(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
demo2.cpp:(.text+0x4de): undefined reference to `jwsmtp::mailer::operator()()'
demo2.cpp:(.text+0x50d): undefined reference to `jwsmtp::mailer::response() const'
demo2.cpp:(.text+0x545): undefined reference to `jwsmtp::mailer::~mailer()'
demo2.cpp:(.text+0x561): undefined reference to `jwsmtp::mailer::~mailer()'
collect2: ld returned 1 exit status 
Do you know what "linking" means? You've got a bunch of declared functions, but they aren't defined in that that file. You should have another file in which these functions are defined.

(I think you're using gcc so I'll use that here)

g++ -c file1.cpp file2.cpp

compiles file1.cpp and file2.cpp. You then get file1.o and file2.o

g++ -o program file1.o file2.o

links file1.o and file2.o to the executable 'program'. When you declare functions without defining them, the compiler will not fail compiling, instead it will assume that the missing definitions are in another file. When linking, undefined references need to be cleared up, else the linking fails and you don't get your shiny new executable (I think undefined references will be ignored by most compilers if you never use them AND they're not class methods, but don't rely on that).

Frankly you should have another file that defines the functions declared in the code.
you may have a library, it will be called something like
libjwsmtp.a or .so

when you link a library you use

g++ -ljwsmtp

to get it to look in the right place if it doesn't find it you use

g++ -L /where/my/lib/lives -ljwsmtp your.cpp
Topic archived. No new replies allowed.