C++ Compilation errors

Steelerz37

Senior member
Feb 15, 2003
693
0
0
I am trying to do seperate compilation with 2 files, 1 is code given to us, the 2nd is our part of the project. It holds one function, handle_requests. Now when handle_requests is in the same file as the rest of the code it works fine, but when seperate I get this error:


[bal8784@granite CPSC423]$ g++ -o rockserver5 stripped_rockserver.cpp initechHandleRequests.cpp
/tmp/ccgkqO6F.o(.text+0x480): In function `handle_requests(int&)':
: multiple definition of `handle_requests(int&)'
/tmp/ccNEMnpC.o(.text+0x480): first defined here
collect2: ld returned 1 exit status


This is after I have fixed a ton of other errors which included errors of variables not declared in this scope, and weird missing ; errors and others. I really have no clue whats wrong.

Also I forgot to mention both of the files in /tmp change each time I try to compile.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Post the example of the code.

You may not be defining the variable properly for external referencing
 

Steelerz37

Senior member
Feb 15, 2003
693
0
0
Ok, now I have a new problem. I'll try explain

First here is the 1st file's includes and whatnot right up to main, file1.cpp:

//Last Modification:
// September 2, 2006

#include <netinet/in.h> //needed for htonl(), ntohl()
#include <sys/socket.h> //needed for socket(), PF_INET
#include <sys/types.h> //needed for sockaddr_in, stat
#include <cstdlib> //needed for EXIT_FAILURE
#include <iostream> //needed for cout
#include <fstream> //needed for file input
#include <cstring> //needed for memset(), strstr(), strlen(), etc.
#include <netdb.h> //needed for gethostbyname()
#include <unistd.h> //needed for close(), stat(), fork()
#include <ctime> //needed for ctime(), time()
#include <sys/stat.h> //needed for stat()
#include <cstdio> //needed for sprintf()
#include "HandleRequests.cpp" //IF THIS IS COMMENTED OUT I HAVE PROBLEMS COMPILING A CERTAIN WAY

using namespace std;

enum Method {GET = 0, HEAD = 1, POST = 2};

int errno;


const int PORTBASE = 2200, //port number offset.
//Set to zero when testing is done and this
//becomes the default webserver.
QLEN = 10, //Number of outstanding connection requests
INBUF_SIZE = 4096, //Size of input buffer
OUTBUF_SIZE = 4096, //Size of output buffer
FN_SIZE=1024; //Maximum size of URL filename and path

//Function prototypes

int passivesock(const char *service, const char *transport, int qlen);
extern int handle_requests(int& ssock);



Here is the 2nd files, right up to handle_requests, file2.cpp:

#include <netinet/in.h> //needed for htonl(), ntohl()
#include <sys/socket.h> //needed for socket(), PF_INET
#include <sys/types.h> //needed for sockaddr_in, stat
#include <cstdlib> //needed for EXIT_FAILURE
#include <iostream> //needed for cout
#include <fstream> //needed for file input
#include <cstring> //needed for memset(), strstr(), strlen(), etc.
#include <netdb.h> //needed for gethostbyname()
#include <unistd.h> //needed for close(), stat(), fork()
#include <ctime> //needed for ctime(), time()
#include <sys/stat.h> //needed for stat()
#include <cstdio> //needed for sprintf()


using namespace std;

extern const int INBUF_SIZE;
extern const int OUTBUF_SIZE;


Now if I leave the code as is I can compile using this command:
g++ -o rockserver5 file1.cpp and get no errors and it seems to work fine.

But unless I misunderstood the professor he is going to compile our program like this:
g++ -o rockserver5 file1.cpp file2.cpp This gives me the error listed above, now if I comment out the #include "HandleRequests.cpp" I get these errors:

/tmp/ccDFw5zq.o(.text+0x4b2): In function `handle_requests(int&)':
: undefined reference to `INBUF_SIZE'
/tmp/ccDFw5zq.o(.text+0x4c0): In function `handle_requests(int&)':
: undefined reference to `INBUF_SIZE'
/tmp/ccDFw5zq.o(.text+0x522): In function `handle_requests(int&)':
: undefined reference to `OUTBUF_SIZE'
/tmp/ccDFw5zq.o(.text+0x530): In function `handle_requests(int&)':
: undefined reference to `OUTBUF_SIZE'
 

CuriousMike

Diamond Member
Feb 22, 2001
3,044
543
136
Your compile error is saying that "handle_requests" is defined twice.
Verify the implementation for that method is defined exactly once.

Also, "#include"-ing other CPP files is usually a no-no.
I'm not totally hip to gcc, but I think your instructors is essentially saying "compile both of these files"

* Verify the mentioned method has only one body of code.
 

Steelerz37

Senior member
Feb 15, 2003
693
0
0
Originally posted by: CuriousMike
Your compile error is saying that "handle_requests" is defined twice.
Verify the implementation for that method is defined exactly once.

Also, "#include"-ing other CPP files is usually a no-no.
I'm not totally hip to gcc, but I think your instructors is essentially saying "compile both of these files"

* Verify the mentioned method has only one body of code.

Yea I found that out after some trial and error. It is because i am #including the cpp, and I also tried to compile the cpp from the command line. How do I go about not #including it and getting it to compile.
 

Dark4ng3l

Diamond Member
Sep 17, 2000
5,061
1
0
Your probably are having a sort of circular include problem. If a.cpp uncludes b.cpp and b.cpp includes c.cpp and c.cpp includes a.cpp.... You get the point. Some times these kind of errors can be so weird...

edit

also if your header files dont include the ifndef stuff and they are included multiple times it will cause these kind of problems.
 

Steelerz37

Senior member
Feb 15, 2003
693
0
0
What do I have to do in file1.cpp to be able to use a function in file2.cpp, and then I also need to be able to use global variables in file1.cpp in file2.cpp, while compiling with this command

g++ -o output file1.cpp file2.cpp

I just dont know exactly what needs to be done to link the files together so each one knows where the function and global variables are defined. I'm not having the circular include problems, as I found out why I was getting that error. I would just leave the #include HandleRequests.cpp, since I can make that compile, but I know the instructor wont like that.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: Steelerz37
What do I have to do in file1.cpp to be able to use a function in file2.cpp, and then I also need to be able to use global variables in file1.cpp in file2.cpp, while compiling with this command

g++ -o output file1.cpp file2.cpp

I just dont know exactly what needs to be done to link the files together so each one knows where the function and global variables are defined. I'm not having the circular include problems, as I found out why I was getting that error. I would just leave the #include HandleRequests.cpp, since I can make that compile, but I know the instructor wont like that.

As others have said, you don't #include .cpp files. What you need to do to use functions from another file is use a function prototype. This basically tells the compiler "there is a function somewhere that looks like this". The compiler accepts that and lets the code compile. Then the linker has to find the implementation of that function somewhere when it builds the executable from the separate object files.

Ussually a function prototype is put in a header file (.h) which is then included with an #include staement, but you can put the prototypes in a cpp file also.

About global variables - first, be really sure thats what you need. I personally don't like to use alot of globals - makes things messy and hard to follow. If you do need one then just declare it outside of any functions in one of the cpp files. Then in any file or function that needs it use the extern keyword. So like this:

file1.cpp
int GlobalInt;

file2.cpp
extern int GlobalInt;

This is like a function prototype - it tells the compiler that it's ok to use this variable because it is declared somewhere. It's then the linkers responsibility to make sure that it actually is declared somewhere.

 

Steelerz37

Senior member
Feb 15, 2003
693
0
0
I got it working, thanks for all the help guys. I have 1 other question.

Is there anyway to time how long I spend on a certain function call, namely read(), and if it sits idle for too long to timeout?

If you havent guessed/noticed we are writing a web server and in order to serve clients correctly we need to loop until we get a "connection: close" in a request, then we can break out. Problem is the whole time I have been working on this I have not once seen a "connection: close" only keep-alive. So this means I am stuck in an infinite loop long after the client has finished talking to my server. currently my code is like this

for(;
{
read(socket, buffer, length); //Is there anyway to time this out and set code to break out of the loop?

/* Code to handle clients request

.....
......
etc.... */

if(connection == "close")
{break;}
}


Thanks again for all the help.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Is there anyway to time how long I spend on a certain function call, namely read(), and if it sits idle for too long to timeout?

When you compile it you can have gcc add profiling information and then use something like gprof to analyze the binary at runtime.

As for timing out read(), I don't think that's possible. read() just happens to work on sockets because on most unix systems they're the same as file descriptors but regular files don't have a reason to have to timeout. You'll probably have to switch to O_NONBLOCK and poll the socket for data to get a decent timeout.

If you havent guessed/noticed we are writing a web server and in order to serve clients correctly we need to loop until we get a "connection: close" in a request, then we can break out.

I haven't looked at any packet traces from web browsing recently, but I would assume that a browser just closes the socket it's using when it finishes loading the page. And even if some do issue a "connection: close" I wouldn't rely on it always being there. If the teacher think it's ok to assume that, then you need to find out what browser he's using so that your testing can be as accurate as possible.

 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |