Archived

This topic is now archived and is closed to further replies.

multiple files in MSVC++

This topic is 5840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I''m trying to get VC++ so that it compiles multi-file projects with only one decleration of constants and headers and as of yet, have had no luck. To clarify what i''m working towards, here is some working test code and the revised test code showing how I''d rather have it structured. The test project contains main.cpp, functions.cpp, globals.h, and prototypes.h. Working code: //in main.cpp: #include #include "globals.h" #include "prototypes.h" int main() { world(); bye(); cout << "number = " << NUMBER << endl; return 0; } //in globals.h: #ifndef _GLOBALS_H_ #define _GLOBALS_H_ #define NUMBER 1 #endif //in prototypes.h: #ifndef _PROTOTYPES_H_ #define _PROTOTYPES_H_ void world(); void bye(); #endif //in functions.cpp #include #include "globals.h" void world() { cout << "hello world" << endl; } void bye() { cout << "goodbye " << NUMBER << endl; } //revised functions.cpp void world() { cout << "hello world" << endl; } void bye() { cout << "goodbye " << NUMBER << endl; } If you''ll notice, the change i''m trying to make is taking #include and #include "globals.h" OUT of functions.cpp which seems like it would work since they should be global. However, it does not. If i take these two headers out i get compiler errors stating that cout is an undeclared identifier, << is illegal, NUMBER is an undeclared identifier, and endl is an undeclared identifier, in functions.cpp. I''ve tried doing this as a project and by just opening main.cpp and compiling it, letting VC++ assign a default project for compilation. Neither work. I''m surprised that making these changes doesn''t work in VC++...it works fine in gcc and g++. Anyway, any help is greatly appreciated. Thanks, Crimson

Share this post


Link to post
Share on other sites
(A file with the .h extention or no extension is a header file, and a file with the .cpp extension is a source file.)

In C++, you must define things before you can use them. The purpose of a header file is to insert definitions of constants and function prototypes into source files, so that those source files can use the things referred to by the header file.

Only source files are compiled; header files are just text that are inserted into the source files by the preprocessor just before compilation.

Here is how your main.cpp file looks after preprocessing:



// Text inserted from globals.h:
// (nothing after preprocessing)

// Text inserted from prototypes.h:
void world();
void bye();

int main()
{
world();
bye();
cout << "number = " << 1 << endl;
return 0;
}


As you can see, all directives have been eliminated, the text substitutions indicated by #define have been performed, and the text insertions indicated by #include have also been performed.

The function prototypes tell the compiler what the type of the function is; in other words how many parameters it takes, what type each of the parameters is, what the return value type is, how the function is to be called, and so on - all the details necessary for the compiler to generate machine code to call the function.

The #define statement simply tells the compiler to replace all following instances of "NUMBER" with "1". That is why "1" appears to be hard-coded after preprocessing.

What I am saying is that in C++ nothing is "global" in the sense that you can automatically use it anywhere without including a header file.

That is why your functions.cpp file (shown after preprocessing) will not work:

void world()
{
cout << "hello world" << endl;
}

void bye()
{
cout << "goodbye " << NUMBER << endl;
}


The compiler will tell you that "NUMBER" was never defined, since you did not include the header file that defined "NUMBER". It will also tell you that "cout" and "endl" are undefined because you did not include the header file that defines those symbols (names).

Share this post


Link to post
Share on other sites