A problem with "include" definition and makefile

Hello everybody.

Help please to write makefile with simple program that is divided into different files.

MyConnection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "MyConnection.h"
#include "MyString.h"

// function for working with net
int Connection::m_CreateSocket()
{
   int retsocket;
   if((retsocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
   {
      String str;
      str.m_Error("UDP server - socket() error");
      return -1;
   }
   return retsocket;
}


MyConnection.h

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
#ifndef EXPERIMENTAL_SERVER_CONNECTION
#define EXPERIMENTAL_SERVER_CONNECTION

#include <netinet/in.h>
#include <stdio.h>
#include <string.h>


class Connection
{
public:
   Connection();
   virtual ~Connection();

protected:

   int m_fdSocket;

   sockaddr_in m_serverAddress;

   int m_CreateSocket();
   

};
#endif 




MyString.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "MyString.h"


void String::m_Error(char *buffer)
{   
   printf(buffer);
}

// services function for working with strings
// print char of string to display
void String::m_PrintString(char *buffer)
{   
   printf(buffer);
}


MyString.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef EXPERIMENTAL_SERVER_STRING
#define EXPERIMENTAL_SERVER_STRING

#include <stdio.h>

class String
{
   public:
      String();
      virtual ~String();

      void m_Error(char *buffer);

      void m_PrintString(char *buffer);

};
#endif 




Main.cpp

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <stdio.h>
#include "MyConnection.h"

int main(int argc, char *argv[])
{

   printf("Succ\n");
   return 0;
}


makefile


1
2
3
4
5
serverTest: main.cpp MyString.o MyString.h MyConnection.cpp MyConnection.h
   g++ -o serverTest main.cpp MyString.o MyConnection.cpp

MyString.o: MyString.h MyString.cpp
   g++ -c -g -Wall MyString.cpp



During compilation errors happend


make
g++ -c -g -Wall MyString.cpp
g++ -o serverTest main.cpp MyString.o MyConnection.cpp
/tmp/cc5Aqhjh.o: In function `Connection::m_CreateSocket()':
MyConnection.cpp:(.text+0x37): undefined reference to `String::String()'
MyConnection.cpp:(.text+0x5c): undefined reference to `String::~String()'
MyConnection.cpp:(.text+0x6f): undefined reference to `String::~String()'
collect2: ld returned 1 exit status
make: *** [serverTest] Error 1
the easiest way to create makefiles is to use autoconf and automake utility.
you can learn it in just a couple of hour's and then creating makefiles for any number of .cpp/.h files will be child's play...

it will be something like this:

1
2
3
4
5
6
7
8
9
10
11
serverTest: MyString.o MyConnection.o main.o
g++ -o serverTest MyString.o MyConnection.o main.o

MyString.o: MyString.h MyString.cpp
   g++ -c -g -Wall MyString.cpp

MyConnection.o: MyConnection.cpp MyConnection.h
g++ -c -g -Wall  MyConnection.cpp

main.o: Main.cpp MyConnection.h
g++ -c -g -Wall  Main.cpp


i hope this should work.
It doesn't work because MyConnection.cpp called class that is in MyString.cpp
oh.. i didnt see that..
modify it:

serverTest: MyString.o MyConnection.o main.o
g++ -o serverTest MyString.o MyConnection.o main.o

MyString.o: MyString.h MyString.cpp
g++ -c -g -Wall MyString.cpp

MyConnection.o: MyConnection.cpp MyConnection.h Mystring.h
g++ -c -g -Wall MyConnection.cpp

main.o: Main.cpp MyConnection.h
g++ -c -g -Wall Main.cpp

now try.
thanks a lot
Topic archived. No new replies allowed.