Hi there,
I have a question about header files/source files in Visual C++ (I use visual c++ 2005 express).

I used to put all my code in one single .cpp file but now I have a test program (done by my tutor at Uni) which has 3 files: 2 .cpp (one has the main function, and the second one has got the functions implementations) and a .h file with the function headers only.
Now, I know that all these files need to be in the same folder for the program to work, which is quite easy to do in Unix using g++, but with Visual is another story.

What I usually do when I write a program is to start a new empty project (for now still a Win32 console application) so that in Visual, in solution explorer, I have in order:
Header files
Resource files
Source files

What I have done with the 3 files that I have, after starting a new project, was to add my .h file into the "header file" section (by right clicking on header files, add, new item) and the 2 .cpp files into the "source files" section (again by right clicking on header files, add, new item: done it twice, one for each .cpp file). I run the program and it works fine, but I wonder whether this is the right way to deal with programs with multiple files or not.

as usual, any suggestion is highly appreciated
thanks

What you have done is correct. All those folders are is an organizational tool whose display is facilitated by an XML file called a "project file". If you look at your computer's actual file system, those folders don't exist. All 3 files are in a folder that is named after your project. You'll find a bunch of other files/folders there as well. These are all VS-specific settings files and folders that it uses "behind the scenes".

Brilliant, thanks a lot : ). I just thought that adding 2 files to the same "folder" could have caused some problems to the program, but I suppose then that I can add as many files as I need to each folder and then compile just the file with main in it.

No, if it's in the "source code" folder, it gets compiled when you compile the project. If there is a file you don't want compiled, you need to exclude (not Remove) it from the project.

It is possible to compile a specific file if you right-click the file and select "compile". But you still have to do any linking that's required before the program will be functional.

Ok I see but how do I determine whether a file needs to be compiled or not? I mean in my example I have 3 files if you remember: the header file which obviously doesn't need to be compiled because I assume it does what it does at linking time, the .cpp file with the main function which needs to compile and the second .cpp with the functions implementations which, again, needs to be compiled together and at the same time as the other .cpp one. So a .cpp file always need to be compiled then, even if it holds the functions implementations only. Is there any case when a .cpp file doesn't need to be compiled?
thanks

>>the header file which obviously doesn't need to be compiled because I assume it does what it does at linking time
That is an incorrect assumption. When you #include a file into another one (as you do a header into a source file, for example), that's like telling the compiler to do a copy/paste from the #included file to the location of the #include statement. A header (*.h) file does contain valid, and necessary, "source" code in it that is required at compile time as well. However, the files themselves are not compiled directly, they are compiled as part of the source (*.cpp) files because of the #include statement.

>>Is there any case when a .cpp file doesn't need to be compiled?
thanks

There really isn't. The only possible time is when you do not call any functions whose implementations are found in that source file, bet even that's not a given. You see, a source file is filled with "source" code (the high-level code you've written that follows a specific language's syntax). For the program to be executable, it must be translated from "source" code to "machine" (a.k.a. "object") code. This is what the compilation process does. Once the file is compiled, you have an "object" file (a *.o file) which the linker then pulls pieces of object code out of (such as the "object" code version of someFunction()) to combine with other object code to produce the final executable file (*.exe).

great, thanks for clarifying it, that's really helpful!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.