February 11th, 2003, 07:27 PM
removing a source file from a project
I've never really understood the MS Visual C++ 6.0 compiler and how source files, projects, and workspaces are related.
1) I find that if my source file is not the same name as the project, it won't compile. For instance, if I name my project "ProgramA", then I have to name the source file for that project "ProgramA.cpp" . Why?
2)I was having some problems with a program, and I wanted to test out something, so I inserted another source file into the project, and I wrote a few lines of simple code and then tried to compile that source file, but I got a bunch of errors. Why?
3) Then I tried to delete my test file from the project to get back to square one, but I couldn't find a command to do that, so I used Windows Explorer to navigate to the folder and delete the test file. But, when I went back into Visual C++ the test file is still listed under File View for the project, and I can't compile the original source file without errors saying the test file can't be found.
Last edited by 7stud; February 11th, 2003 at 07:31 PM.
February 11th, 2003, 08:00 PM
To remove a file from the project, simply ckick on it once to highlight it and press the Delete key.
I had to stumble on this solution myself. I would agree that they should have made it available as a menu item.
February 11th, 2003, 08:19 PM
Thanks. I guess they want to make it difficult, so that you don't accidentally delete anything. Also, when you do delete it from the project, it still exists on your computer, so you can retrieve it that way if you ever need to.
Any answers to my other questions?
February 12th, 2003, 02:00 PM
Actually, I had written a response to them before, but the forum timed out and logged me off automatically, so when I tried to post I just got an error page and my message was lost. Second time it's happened to me; I guess I'm a slow learner.
I'm not having that problem. But first, what kind of project are you doing? MFC, Win32, console?
Most of my sockets test programs are console apps which I create as an empty project and then add my source files later. None of the source files are the same name as the project and they make just fine. Of course, the executable is the same name as the project, but that can be changed after-the-fact in DOS.
For MFC and Win32 projects, I don't believe that there is an option to create an empty project. Instead, VC6 creates the application's overall structure -- its skeleton -- which you then flesh out and add to in order to design the application itself. In automatically creating those files and classes, VC6 defaults to using the project name, as you have observed.
When I read your post, I ran a couple quick tests. I created a Win32 project and an MFC project. I copied the main CPP files to files of a different name. Then I removed the original files from their projects and added the renamed source files. In both cases, they compiled and linked just fine -- they even executed properly.
So that means that the name of the file is not important. However, the rest of the project will still expect to find the classes, variables, functions, and defines that were created in that file and you would need to preserve that content as I had in my tests. If you also wanted to change the name of any header files, then you could, but you would need to edit all the appropriate #include statements throughout the project. Similarly, if you wanted to change the name of a class, then you could, but you would have to change all references to that class throughout the project -- plus you might probably need to re-educate the Class Wizard.
Way before that point, I would want to ask whether having that different file/class name would be worth the pain.
On the positive side, I have found Visual C++'s "visual" approach to be much more open and easier to work with than in Visual BASIC or Visual FoxPro. I have had something break in a VC application and I was able to go into the source code and fix it. OTOH, in VB and in VFP the project was just plain hosed and impossible to fix, so I had no other choice but to start all over again.
Without some vital information like what kind of errors you got and what you were trying to do, the best I could offer here would be wild guesses. Also, I don't know what your C/C++ experience is, so I might insult your intelligence by suggesting some really dumb mistakes.
I mean, you should know that you must include the right header files for the library functions that you use. And similarly that if you reference a variable or function in another module of your program that you must include its header file as well.
Other than that, the only thing I can think of would be if that second source file was written as an independent program with its own main() function. In that case, then the project would have duplicate declarations and duplicate entry points.
If neither of those are the problem, then you will need to describe what you were trying to do and what kinds of errors you got.
February 12th, 2003, 04:33 PM
First of all, thanks a lot for all the time you spent writing your detailed response twice, I guess . As for my experience, I worked my way through "Ivor Horton's Beginning C++", so I'm a beginner with only about enough knowledge to understand what you're saying, so no advice would be an insult. Currently, I'm working exclusively on simple console applications in VC 6.0 as I work my way through Ivor Horton's Beginning Visual C++, which gets into MFC/Windows programming in later chapters. I create my console applications with an empty project, and then I add a source file.
As for the naming problem I was having, I now seem to be able use a different name for the source file and project name and get it to compile. I'm at a loss as to why it wouldn't work before.
The multiple source file problem gave me the following errors:
testC.obj : error LNK2005: _main already defined in testB.obj
Debug/testA.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.
My thought was while coding in one source file(testB.cpp), I could test an idea in another source file(testC.cpp). So, I created a new file and inserted it into the project and tried to test a snippet of code.
February 12th, 2003, 08:16 PM
OK, there you have it. There can only by one function by a given name in the entire program, otherwise when you call that function then the compiler wouldn't know which one to use.
Even worse, the main() function is the entry point -- where the program actually starts (well, first everything gets set up by the start-up code, but that's transparent to you).
It looks like you had a main() function defined in both source files. Solution: remove the first file while you're doing the testing, then when you're done remove the second and add the first back again.
Or just create a new project for the test case and switch between the two.
Have fun and enjoy! I really like C++ but don't work in it enough. Most of my work is in C and my quickie text-manipulation utilities in Perl.