Advertisement

MS VC++ 5.0 compiling question

Started by March 28, 2000 02:34 PM
4 comments, last by Intangir 24 years, 5 months ago
Until now I have always had to #include multiple files as External Dependancies. When I compile my projects with multiple source files, I always come up with errors. If I don''t #include any of the files within my project (ie, if main.cpp and second.cpp are two files in my project, I do not have the line #include "second.cpp" within main.cpp) MS VC++ compiles the source files in its own order, and then gives the error that certain variables and functions haven''t been defined yet. If i knew which piece of code was being compiled first i would be glad to place all my prototypes in it. If I do #include the files as stated above, then I get the error that certain functions have been previously defined. I attempted to remedy this error with the following lines of the included file. Another example would be: -main.cpp #include "second.cpp" void main(void) { //the main program } -second.cpp #ifndef SECOND_CPP #define SECOND_CPP //the functions and variables declared in second.cpp #endif I thought that the above code would prevent the included files from being compiled twice, but I still get the error that the functions within the included file have been compiled twice. Any ideas how to solve this problem?
*Growl*
hmm...this has never happened to me before...for me, i just have one cpp file, and all the others i make into h files and include them into the main cpp.
if i guess right, you''d have to do #include "second.cpp" to make it compile "second.cpp" first, and "first.cpp" second (whoa, dizzy now...) since cpp and h files are virtually the same except for their extensions, and can (confusingly) be used interchangably.
Advertisement
Thanks for the help! I found what I was doing wrong. I''ll post it, just in case someone else runs into a similar problem:

As I said before, I was including another source file (lets call it second.cpp) when that file was already in my project. Because of which, MS VC++ would compile it twice. To remedy the solution I created a header file (second.h) with all the predefinitions of the classes and functions used in second.cpp. Then, within main.cpp, I #included "second.h" instead of "second.cpp". This fixed it all.

Thanks for your help
*Growl*
You really shouldn''t be including .cpp files... The whole point behind having header file (*.h) and source files (*.cpp) is to seperate the interface from the implementation . Here is what I mean:

Any function, class, variable, or anything in C++ must be declared before you use it. However, functions and classes must be declared, but they don''t have to be defined. Here is how your file organization is designed to work:

Say you have three files: main.cpp, other.cpp, other.h
It would be organized like this:

other.h:

#ifndef OTHER_H
#define OTHER_H

int SomeFunc(int, int); // or you could supply var names...

#endif // OTHER_H

other.cpp:

#include "other.h"

int SomeFunc(int int1, int int2)
{
return int1 + int2; // or whatever
}


main.cpp:

#include "other.h"

int main()
{
int i = 6, j = 10, x;
x = SomeFunc(i,j);

return 1;
}


Or something like that. You just need to know that the function only must be declared, not defined, before you use it. You are only supposed to include the interface to a class or function, and that way it doesn''t matter what order the cpp files are compiled in, they all have the same access to functions. The linker is what takes care of the actual calls, so it doesn''t matter if the compiler doesn''t know where to find a function, the linker will take care of that.

I hope this was clear! Good luck!

------------------------------
Jonathan Little
invader@hushmail.com
http://www.crosswinds.net/~uselessknowledge
It looks like I was typing that while you posted your fix. I''ll just leave it up in case it helps anyone though
Thanks for posting that info - even after I solved the problem, iit was still helpful.

The problem originated for me when I had the impression that the #definitions would carry through when compiling the multiple .cpp files - i thought if I defined something within the first compiled .cpp file that it would still be defined in the second. . . I guess it doesn''t work that way =)
*Growl*

This topic is closed to new replies.

Advertisement