Menu

Where to put auxilary files?

2008-08-16
2012-09-26
  • Chris Hammond

    Chris Hammond - 2008-08-16

    I'm trying to learn C++ on my own. I am using Dev C++ 4 with Windows XP. I am working through Mark Joshi's book "C++ Design Patterns and Derivatives Pricing." The book opens with a sample program for using Monte Carlo simulation, SimpleMCMain.cpp. The program SimpleMCMain.cpp has a header Random1.h telling it that certain functions in SimpleMCMain.cpp are defined. The functions are actually defined in Random1.cpp. (All of these are online at http://www.markjoshi.com/design/). I put the file Random1.h with the include files in Dev C++. However, I don't know where to put the file Random1.cpp. I tried saving a copy of it with the include files. It is also open with SimpleMCMain.cpp in the same project. SimpleMCMain.cpp compiles, and it runs fine until it is supposed to use the functions defined in Random1.cpp. Since Joshi's code is supposed to work, I think it is just that Random1.cpp is not in the right place, and I don't know where to put it.

    I take back my earlier statement. I tried to compile it again, and I got this error:

    C:\DOCUME~1\Owner\LOCALS~1\Temp\ccOmcaaa.o(.text+0xa2):simple~1.cpp: undefined reference to `GetOneGaussianByBoxMuller(void)'

    Honestly, I'm having a bit of trouble understanding how header files work. I gather that a header file tells your program that certain things it needs are available somewhere. But then . . .

    Any help would be appreciated.

     
    • Chris Hammond

      Chris Hammond - 2008-08-16

      Thank you!

       
    • Chris Hammond

      Chris Hammond - 2008-08-16

      I fixed the problem. If I start a project that contains both SimpleMCMain.cpp and Random1.cpp, then everything works fine. Is there someplace that I can put Random1.cpp so that I don't have to include the file in every project where I want to use it. For instance, the header file Random1.h is in a folder where if I type #include<Random1.h> in any new program, it knows where to look. Can I do a similar thing for Random1.cpp?

       
    • cpns

      cpns - 2008-08-16

      "Dev C++ 4" is very old, and uses GCC 2.95 which pre-dates GCC's strong ISO compliance. You should dump it and use 4.9.9.2. That is the version that you will get support for here.

      > I put the file Random1.h with the include files in Dev C++

      Bad idea. Imagine how 'polluted' that folder is going to get with project specific headers. How are you ever going to move your project to a different machine or use a different compiler!?

      > I don't know where to put the file Random1.cpp.

      Same place as SimpleMCMain.cpp, which is also where you should put Random1.h

      > SimpleMCMain.cpp compiles, and it runs fine

      If that is the case then whatever you did regardless of how ill advised, the code must have compiled and linked. The location of any source files after a successful build is irrelevant, the executable is a stand alone file. If it fails, it is because the code is flawed.

      > I take back my earlier statement. I tried to compile it again, and I got this error

      Ok that makes more sense, it never did build successfully then?

      I wrote this a while back, it may help you understand how the toolchain works: http://sourceforge.net/forum/message.php?msg_id=2670009 (excuse the typo's, the forum has no edit facility)

      Now, it may not be essential but I strongly recommend that you upgrade to 4.9.9.2 ( http://downloads.sourceforge.net/dev-cpp/devcpp-4.9.9.2_setup.exe ). Be sure however to follow the uninstall directions in the "PLEASE READ BEFORE POSTING A QUESTION" thread first to avoid serious problems with incompatible configuration files. Incidentally read that thread for a number of other solutions to common problems.

      OK, whether you choose to upgrade or not, what you need to do is place all of the source files in one folder. A folder without spaces in the path, and not a subfolder of c:\dev-cpp. Say c:\devproj\joshi\SimpleMC\ for example. On larger projects you might want a more comples file organisation, but this will give you fewer problems for small projects, and let's walk before we run here!

      Then you need to use the Dev-C++ project facility: File->New->New project (that is correct at least for 4.9.9.2, if you are using 4, it may be different, I have never used it). I guess that this is a console mode project, so seleted the console template, and set the project language to C++, give the project a name "SimpleMC" for example.

      When you select OK in the new project dialog, you will be prompted to save it. Do so to the same folder as the source files.

      Dev-Cpp generates a main.cpp file for you with some basic starter code. You don't need this. Close the file without saving it, and remove it from teh project pane on the right (right click the file and use the context menu to do that). Then add your sources to the project (either with the project pane context menu or the meny bar's Project menu. Adding teh header file is optional (and convenient for editing as you can open it from the project pane), but you must add both .cpp files, so that Dev-C++ knows that they must both be separately compiled and then linked into one executable.

      Then build your project (from the Execute menu or the toolbar buttons). If it fails, and you are not able to fix it, post the complete log as directed in the "PLEASE READ BEFORE POSTING A QUESTION" thread.

      Now with respect to header files:

      include <Random1.h>

      causes the pre-processor to search in paths specified by -I<path> compiler options, wheras:

      include "Random1.h"

      causes it to look in the same folder as the including source file before resorting to the include path list.

      You can set the include file path list in the project dialog. If you want to make it a standard path, you can set it in the Tools->Compiler options dialog path list. However if you want to re-use Random1.cpp you have to add it to each project (although it can be the same copy in a common location). One way around that is to build a library. Now a library of just one object file is pointless (because you still have to add the library to the project), so I suggest that you leave that until you have learned the basics. Besides it is hardly much of a chore.

      You might want to organise your folder like this:

      c:\devproj\joshi\SimpleMC\ c:\devproj\joshi\commonsrc
      c:\devproj\joshi\include

      Then you add c:\devproj\joshi\include to the include paths list, and each project shares sources from c:\devproj\joshi\commonsrc with project specific files in the project folder "c:\devproj\joshi\SimpleMC\&quot; in this example.

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.