Apple
Apple

Reputation: 351

Header files c++ windows to linux conversion

I've copied and pasted some files from my windows machine to my linux one and ran into a few understandable problems with the conio.h and Windows.h headers, but I can't remember what they're needed for.

They're only in the main function so I commented them out and ran the program again thinking the errors would show me where they're needed so I could then google what headers would work similarly for linux.

The problem I have is that all the errors I get are in header files I've included in the main function, but don't have the Windows.h or conio.h headers included.

The question I have is how/why: - Does the compiler look at each header file in turn, get to the windows.h header and stop then when that's commented out it get's to my "Chapter_7.h" header and find all the problems in there.

Or could the commenting out of the headers in the main.cpp somehow affect the headers I wrote.

Or (possibly more likely) Is there another option I've overlooked?

Thanks for any answers.

Upvotes: 0

Views: 3514

Answers (3)

Mats Petersson
Mats Petersson

Reputation: 129374

The first step of fixing "I'm using headerfiles that I can't use on this machine" is to remove those files, compile the code and see where things break - it's nearly always making compiler errors, so you should be able to find it quite easily.

conio.h allows console io, and it may require a bit of work to fix that into Linux. Depends on exactly what you are using.

windows.h is much more complex to explain, as it essentially gives you all of the Windows API functions, and a huge number of type declaration, and there are many of those. Most of which your program probably never uses [directly].

One of the common problems with "using a headerfile that doesn't exist somewhere else" is that you end up using some types that are declared in the headerfile. So for example, windows.h will declare LONG, HANDLE, and a vast number of other types. These types are then used in structures and function declarations elsewhere, and when you remove windows.h it will cause all manner of follow-on problems.

My approach is always to fix the first problem first, then compile again. The reason for this is that the compiler often gets "confused" if something strange appears. It's a bit like if you were to tell a friend how to drive to your house, and you say "When you see the big-oaktree on the left, you take the next right". Unknown to you, the oak-tree has been cut down because it was dieing, and is no longer there. So your friend drives where the oak-tree used to be, and turns right after another oak-tree later on along the road. Of course, now all OTHER instructions are completely uselss, because your friend is miles away from the correct road, and is never going to find your house without new instructions. The same thing happens when the compiler finds "surprising" or "missing" bits in the source-file. So fixing the first error, then compiling, fixing, compiling, usually gets you to a point where your code compiles. Sometimes this involves writing new functions like this:

int GetTimeOfDay(void)
{
    printf("%s:%d:%s: doesn't work yet, exiting...", __FILE__, __LINE__, __func__);
    exit(1);
    return 0;  // to keep compiler happy. 
}

Once you got all the code compiling, you start working out how to implement/replace those functions that you added to make it compile.

Upvotes: 1

Karthik T
Karthik T

Reputation: 31952

It is due to the way .h files are "INCLUDED" in the .cpp. During compilation, all the text in the header files is copy pasted into the including .cpp file. This means later .h files will have access to earlier ones.

This looks to be what is happening in your case, windows.h would have been the first include, and subsequent headers have been using it all along.

//#include <window.h>  
//#include <conio.h>
#include "myheader.h"    // can no longer use windows.h or conio.h
#include "myheader2.h"

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258618

You can say header contents are copy-pasted inside the file they're included in, if that's what you mean.

Or could the commenting out of the headers in the main.cpp somehow affect the headers I wrote.

Definitely. Assume:

//header1.h
#define X 1

//header2.h
#ifdef X
int a = 0;
#else
int a = 1;
#endif

a will be defined differently if header1.h is included before or after header2.h.

Upvotes: 1

Related Questions