Error Messages When Compiling

I've been trying to compile the following program:

Server.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
26
#ifndef SERVER_H
#define SERVER_H

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>

class Server {
	int status;
	struct addrinfo hints, *serverinfo;																//serverinfo will be used to bind
	int s;
	public:
		Server();
		int run();
};

#endif 																								//SERVER_H 


Server.cpp
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
#ifndef SERVER
#define SERVER

#include "Server.h"
#define PORT "3260"

int Server::run() {

	//Sets up the hints addrinfo struct
	memset(&hints, 0, sizeof hints);																//Make sure the struct is empty
	hints.ai_family = AF_UNSPEC;																	//IPv4 or IPv6
	hints.ai_socktype = SOCK_STREAM;																//Use TCP stream sockets
	hints.ai_flags = AI_PASSIVE;																	//Use my IP address and fill it in for me

	if((status = getaddrinfo(NULL, PORT, &hints, &serverinfo)) != 0){
		fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));							//Print an error message if getaddrinfo fails
		exit(1);
	}

	s = socket(serverinfo->ai_family, serverinfo->ai_socktype, serverinfo->ai_protocol);			//Sets up the socket and stores it in s
	if(s == -1) {																					//Prints an error message if socket returns -1
		fprintf(stderr, "socket error");
		exit(2);
	}

	if(bind(s, serverinfo->ai_addr, serverinfo->ai_addrlen) == -1) {								//Binds socket s
		fprintf(stderr, "bind error");
		exit(3);
	}

	freeaddrinfo(serverinfo);
	return 0;
}

#endif //SOCKET 


main.cpp
1
2
3
4
5
6
7
#include "Server.cpp"

int main() {
	Server server;
	server.run();
	return 0;
}


I am running the following command in Ubuntu console:


g++ Server.h Server.cpp main.cpp


I get the following output:


/tmp/ccGPa7wT.o: In function `Server::run()':
main.cpp:(.text+0x0): multiple definition of `Server::run()'
/tmp/ccs3sspi.o:Server.cpp:(.text+0x0): first defined here
/tmp/ccGPa7wT.o: In function `main':
main.cpp:(.text+0x19a): undefined reference to `Server::Server()'
collect2: error: ld returned 1 exit status



I thought that the problem originally was that I didn't have a header file, but even after I created one I'm still getting the problem. I'm used to programming in Visual Studio so I'm not too familiar with compiling on a Unix box from the command line. I did some googling but couldn't find anything helpful.
You accidentally included Server.cpp instead of Server.h in your main.cpp
Also, don't pass Server.h to the compiler command line - it doesn't do anything.
Last edited on
Topic archived. No new replies allowed.